I find it quite common that I have multiple environments (for example test and prod) but the Docker containers that I wish to start are the same in both environments. The only difference is the application configuration which I want to specify using an env-file
. Since I have multiple containers and dependencies between them I want to use docker-compose. But afaik I can only specify an env-file
inside the docker-compose.yml
file (see docs). If this is the case then I need to clone my original docker-compose.yml
to two different files (one for test and one for prod) just to point to different env files. This means that I have to maintain two docker-compose.yml
files instead of one and if I make any changes I need to update both files.
Is this really according to design? Why won't docker-compose
let me specify --env-file
when I do docker-compose up
or docker-compose run
?
Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.
Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.
Using Multiple Docker Compose Files Use multiple Docker Compose files when you want to change your app for different environments (e.g., dev, staging, and production) or when you want to run admin tasks against a Compose application.
Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.
See Update #2 below. This is now possible!
This is a much requested feature of Docker Compose. Unfortunately, the answer at the moment is that you can't. I'd recommend subscribing to these GitHub issues to get a better idea when and if this feature gets implemented:
Issue #495 is actually the most commented in their issue repository at the moment. You are definitely not alone in wanting to do this.
Update:
The latest issue tracking is at https://github.com/docker/compose/issues/1377.
Update #2:
This functionality has been merged and is available as of Docker Compose 1.5.0. See https://github.com/docker/compose/blob/129092b7/docs/yml.md#variable-substitution for usage information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With