Docker compose for multiple environments
Docker is amazing. When you have multiple services, Docker compose just adds on that. Here I present a few functionalities that Docker compose uses and I find them important to know when you are architecting your Docker configurations.
Compose reads 2 files by default - not one #
Docker-compose reads two files by default - docker-compose.yml and docker-compose.override.yml. This is useful in order for you to be able to keep the basic configuration in docker-compose.yml and the dev specifics in the override file. Since Docker compose reads both by default, developer can use the shortest command:
docker compose up
However, when the command is used with 'file' attribute (-f) only the specified files are read:
docker compose up -f docker-compose.yml -f docker-compose.prod.yml
This saves you some keystrokes as you will start and stop often times using it locally, but can have a script on the server to execute a longer command.
Single value options are overridden #
When using image, command or mem_limit and others, the new file overrides the original value.
Multi value options #
When using options like ports, expose, external_links, dns, dns_search, and tmpfs the values are concatenated.
For options like environment, labels, volumes, and devices values are merged with locally defined values having higher priority. Volumes and devices are merged using the path in the container.
Original service:
environment:
- FOO=original
- BAR=original
Local service:
environment:
- BAR=local
- BAZ=local
Result:
environment:
- FOO=original
- BAR=local
- BAZ=local
Meaning, if the same value is defined in the original file and local, then the local one overrides the original, otherwise the value in the local file is added to the list.
Reference and using docker in production
- Previous: Yarn VSTS
- Next: Git return to previous branch