Docker compose does not inject host environment variables in MacOS

When using docker-compose to orchestrate your container services it’s common practice to pass environment variables set in the host machine through to your containers. This is especially useful when you want to configure passwords/secrets for your applications running inside the container. By doing this you can avoid including such sensitive information in your docker-compose.yml file.

Let’s consider the following extract of a docker-compose.yml file which defines two environment variables required for a MySQL container service named ‘db’.  The highlighted lines shows how the variables are not given any values. This essentially tells docker-compose to inject the values using the corresponding environment variables in the host machine.

version: "2.0"

   image: mysql:5.6.26
     - '3306:3306'

Now usually what you would do in Linux or MacOS is use the “export” shell command to set values on the host machine before calling docker-compose up to bring up the containers.

export MYSQL_ROOT_PASSWORD=password
export MYSQL_DATABASE=moviedb
docker-compose up

This works on Linux, in my case an Ubuntu 16.04 droplet on DigitalOcean without any issue. But it doesn’t seem to work on my dev machine running MacOS (Sierra) with the latest version of Docker 17.03.1-ce (at the time of writing)

Strangely when running docker-compose up on MacOS the MySQL container complains that the MYSQL_ROOT_PASSWORD environment variable is not set. Further inspection revealed that both variables were empty in the container.

So it seems like the “export” shell command does not work with compose on MacOS.

The trick to solving this problem in MacOS is to instead use a one liner in the shell like so:

MYSQL_ROOT_PASSWORD=password docker-compose up

Note: I have removed the MYSQL_DATABASE=moviedb segment from the above command for brevity.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s