How do you manage per-environment data in Docker-based microservices?

Docker compose supports extending compose files, which is very useful for overriding specific parts of your configuration.

This is very useful at least for development environments and may be useful in small deployments too.

The idea is having a base shared compose file you can override for different teams or environments.

You can combine that with environment variables with different settings.

Environment variables are good if you want to replace simple values, if you need to make more complex changes then you use an extension file.

For instance, you can have a base compose file like this:

# docker-compose.yml
version: '3.3'
services:
  service-a:
    image: "image-name-a"
    ports:
     - "${PORT_A}"
  service-b:
    image: "image-name-b"
    ports:
     - "${PORT_B}"
  service-c:
    image: "image-name-c"
    ports:
     - "${PORT_C}"

If you want to change the ports you could just pass different values for variables PORT_X.

For complex changes you can have separate files to override specific parts of the compose file. You can override specific parameters for specific services, any parameter can be overridden.

For instance you can have an override file for service A with a different image and add a volume for development:

# docker-compose.override.yml
services:
  service-a:
    image: "image-alternative-a"
    volumes:
      - /my-dev-data:/var/lib/service-a/data

Docker compose picks up docker-compose.yml and docker-compose.override.yml by default, if you have more files, or files with different names, you need to specify them in order:

docker-compose -f docker-compose.yml -f docker-compose.dev.yml -f docker-compose.dev-service-a.yml up -d

For more complex environments the solution is going to depend on what you use, I know this is a docker question, but nowadays it’s hard to find pure docker systems as most people use Kubernetes. In any case you are always going to have some sort of secret management provided by the environment and managed externally, then from the docker side of things you just have variables that are going to be provided by that environment.

Leave a Comment