ptr
ptr

Reputation: 3384

docker-compose scaleable way to provide environment variables

I am searching for a scaleable solution to the problem of having numerous possible environments for a containerised app. Let's say am creating a web app and I have the usual deployment environments, develop, testing, production but I also have multiple instances of the app for different clients, client1, client2, client3 etc.

This quickly becomes a mess if I have to create separate docker-compose files:

docker-compose-client1-develop.yml
docker-compose-client1-testing.yml
docker-compose-client1-production.yml
docker-compose-client2-develop.yml
...

Breaking the client specific configuration into a .env file and dockers variable substitution gets me most of the way there, I can now have one docker-compose.yml file and just do:

services:
  webapp:
    image: 'myimage:latest'
    env_file:
     - ./clients/${CLIENT}.env  # client specific .env file
    environment:
     - DEPLOY  # develop, testing, production

so now I just need the CLIENT and DEPLOY environment variables set when I run docker-compose up which is fine, but I'm wondering about a convenient way to pass those environment variables in to docker-compose. There's the potential (at least during development) for a decent amount of context-switching. Is there a tidy way to pass in different CLIENT and DEPLOY env vars to docker-compose up every time I run it?

Upvotes: 3

Views: 632

Answers (2)

Mario Cianciolo
Mario Cianciolo

Reputation: 1261

What you are trying to achieve is to set environment variables per-command.

Are you running on Linux? Take a look at env command. Just prepend your docker-compose command line like this:

env CLIENT=client1 DEPLOY=production docker-compose ...

On Windows, you may have to do something more complicated (like this), but there could be simpler ways.

Upvotes: 3

Mike Doe
Mike Doe

Reputation: 17566

Have you tried docker-compose file extending?

For instance you can have base docker-compose.yml file which is the production one and multiple extending files where you only change what needs to be overloaded:

docker-compose.dev.yml

version: '2'
services:
  webapp:
    env_file: path_to_the_file_env

Then you simply use both:

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

To spin up the production it's as easy as:

docker-compose up

I personally use this technique a lot in many of my projects.

Upvotes: 3

Related Questions