Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different env-file but same yml with Docker Compose?

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?

like image 319
Johan Avatar asked Mar 15 '15 15:03

Johan


People also ask

Can I use environment variables in Docker compose file?

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.

How do I override Docker compose environment variables?

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.

Can I have multiple Docker compose files?

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.

Do I need both Dockerfile and Docker compose yml?

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.


1 Answers

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:

  • https://github.com/docker/compose/issues/495
  • https://github.com/docker/compose/pull/76
  • https://github.com/docker/compose/pull/845

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.

like image 140
Andy Shinn Avatar answered Oct 13 '22 13:10

Andy Shinn