Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share global env vars in Docker?

I have two environments development and production, I use two files with respective vars of each environment: .env.development and .env.production. I'm use, too, docker-compose to load this variables.

.env.development

COMPOSE_PROJECT_NAME=luna
RAILS_ENV=development

DATABASE_URL=postgresql://user:pass@lunapostgres:5432/luna?encoding=utf8&pool=5&timeout=5000
REDIS_CACHE_URL=redis://:pass@redis:6379/0/cache
ACTIVE_JOB_QUEUE_PREFIX=luna:jobs
ACTIVE_JOB_URL=redis://:pass@redis:6379/0

AUTH_BASE_URL=auth.com
SOLAR_BASE_URL=http://test.url
# SOLAR_BASE_URL=http://api/api/v1/
BUNDLE_PATH=/box

BIND_ON=0.0.0.0:3000
SENTRY_DSN=http://xxxxxxx

PAGER=more

ACCESS_TOKEN=xxx
VERIFY_TOKEN=xxx
DIALOGFLOW_CLIENT_ACCESS_TOKEN=xxx
DIALOGFLOW_DEV_ACCESS_TOKEN=xxx

RAILS_MAX_THREADS=1
WEB_CONCURRENCY=1
REQUEST_TIMEOUT=5

DOMAIN=localhost:3000
BASE_URL=localhost:300
SECRET_TOKEN=xxx
LOG_LEVEL=debug
SOLAR_MENTOS_DEBUG=true

.env.production

COMPOSE_PROJECT_NAME=luna
RAILS_ENV=production
RACK_ENV=production

DATABASE_URL=postgresql://user:pass@lunapostgres:5432/luna?encoding=utf8&pool=5&timeout=5000
REDIS_CACHE_URL=redis://:pass@redis:6379/0/cache
ACTIVE_JOB_QUEUE_PREFIX=luna:jobs
ACTIVE_JOB_URL=redis://:pass@redis:6379/0

AUTH_BASE_URL=auth.com
SOLAR_BASE_URL=http://test.url
# SOLAR_BASE_URL=http://api/api/v1/
BUNDLE_PATH=/box

BIND_ON=0.0.0.0:3000
SENTRY_DSN=http://xxxxxxx



ACCESS_TOKEN=yyy
APP_SECRET=yyy
VERIFY_TOKEN=yyy
DIALOGFLOW_CLIENT_ACCESS_TOKEN=yyy
DIALOGFLOW_DEV_ACCESS_TOKEN=yyy

RAILS_SERVE_STATIC_FILES=true
RAILS_LOG_TO_STDOUT=true
WEB_CONCURRENCY=5
REQUEST_TIMEOUT=5
RAILS_MAX_THREADS=5



DOMAIN=production.com
BASE_URL=https://production.com

SECRET_TOKEN=yyy
LOG_LEVEL=info

# ----------------------------------------
DEVISE_SECRET_KEY='yyy'
GOOGLE_ANALYTICS_UA='yyy'

docker-compose.override.yml

version: '2'

services:
  app:
    env_file:
      - '.env.development'

docker-compose.production.yml

version: '2'

services:
  app:
    env_file:
      - '.env.production'

I would like share equivalents environment variables between my containers, and keep different variables in your respective environment.

like image 863
Luiz Carvalho Avatar asked Jun 28 '18 14:06

Luiz Carvalho


1 Answers

Use extendable environment files.

version: '2'

services:
  app:
    env_file:
      - 'base.env'
      - 'production.env'

From the Docs

When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:

  1. Compose file,
  2. Environment file
  3. Dockerfile
  4. Variable is not defined

The docs are not clear on the use of multiple files so I ran a test. The last environment files overrides previously set variables. If you want to override default values, do so in the last file or in the compose file.

base.env

TEST_VARIABLE=base

production.env

TEST_VARIABLE=production

docker-compose.yml

version: '2.1'

services:
  test:
    image: alpine
    env_file:
      - 'base.env'
      - 'production.env'

Running docker-compose run --rm test env gives us TEST_VARIABLE=production. Thus, the second file overrides the first.

like image 185
Tom Rijntjes Avatar answered Sep 21 '22 23:09

Tom Rijntjes