Overriding image version in docker-compose file

Docker is already having that feature. I tried to override image name with simple docker-compose, it is working.

For example,

docker-compose.yml with content,

my-httpd:
  image: httpd:latest
  ports:
    - "1110:80"  

And docker-compose.override.yml with content,

my-httpd:
  image: httpd:2.4

After the execution of docker-compose -d, here is the docker ps info,

enter image description here

It uses ports from docker-compose.yml (not overrided) and image from docker-compose.override.yml as it is getting overridden here.

Note: It you have different names and location, you could use it like the following command instead of docker-compose -d,

docker-compose -f <DOCKER COMPOSE LOCATION> -f <OVERRIDE FILE LOCATION> up -d

Edit 2:

Overriding in the above manner will only replace the non array values and the array variables will be merged.

For example, if I have ports in both files which is an array, it will bind both the ports instead of taking ports value just from the overriding file unlike the image part (non array).


There is (now?) a better option for what you want to do. From https://docs.docker.com/compose/environment-variables/

It’s possible to use environment variables in your shell to populate values inside a Compose file:

web:
  image: "webapp:${TAG}"