Docker multiple environments

maciejmatu picture maciejmatu · Feb 17, 2017 · Viewed 13.7k times · Source

I'm trying to wrap my head around Docker, but I'm having a hard time figuring it out. I tried to implement it in my small project (MERN stack), and I was thinking how do you distinct between development, (maybe staging), and production environments.

I saw one example where they used 2 Docker files, and 2 docker-compose files, (each pair for one env, so Dockerfile + docker-compose.yml for prod, Dockerfile-dev + docker-compose-dev.yml for dev).

But this just seems like a bit of an overkill for me. I would prefer to have it only in two files.

Also one of the problem is that e.g. for development I want to install nodemon globally, but not for poduction.

In perfect solution I imagine running something like that

docker-compose -e ENV=dev build
docker-compose -e ENV=dev up

Keep in mind, that I still don't fully get docker, so if you caught some of mine misconceptions about docker, you can point them out.

Answer

VonC picture VonC · Feb 17, 2017

You could take some clues from "Using Compose in production"

You’ll almost certainly want to make changes to your app configuration that are more appropriate to a live environment. These changes may include:

  • Removing any volume bindings for application code, so that code stays inside the container and can’t be changed from outside
  • Binding to different ports on the host
  • Setting environment variables differently (e.g., to decrease the verbosity of logging, or to enable email sending)
  • Specifying a restart policy (e.g., restart: always) to avoid downtime
  • Adding extra services (e.g., a log aggregator)

The advice is then not quite similar to the example you mention:

For this reason, you’ll probably want to define an additional Compose file, say production.yml, which specifies production-appropriate configuration. This configuration file only needs to include the changes you’d like to make from the original Compose file.

docker-compose -f docker-compose.yml -f production.yml up -d

This overriding mechanism is better than trying to mix dev and prod logic in one compose file, with environment variable to try and select one.

Note: If you name your second dockerfile docker-compose.override.yml, a simple docker-compose up would read the overrides automatically.
But in your case, a name based on the environment is clearer.