Docker Compose: Override configuration via an environment var

Here is a simple way to override a variable in your docker-compose.yml file.

Using this syntax it will first try to get the environment variable named “MY_PROFILE” from your computer and pass it to the container, if it’s not set, it will use the value “dev”.

docker-compose.yml

 environment:
    - SPRING_PROFILES_ACTIVE=${MY_PROFILE-dev}

Environment variable “MY_PROFILE” not set

> docker-compose config
 environment:
    - SPRING_PROFILES_ACTIVE=dev

Environment variable “MY_PROFILE” set

> set MY_PROFILE=prod
> docker-compose config
 environment:
    - SPRING_PROFILES_ACTIVE=prod

Versions used

  • Docker: 17.06.0-ce
  • Docker Compose: 1.14.0
  • docker-compose.yml file format: 2.1
  • OS: Windows 10

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s