Docker Compose Environment Variables

Docker Compose Environment Variables

Environment variables are essential to the proper configuration of Docker. If you’re going to run a postgres docker image it relies on the environment variable ‘POSTGRES_PASSWORD’ to set the database password. The same is true for nearly every base image out there. They rely on environment variables to make the magic happen. In this short article we’ll go over how to inject docker compose environment variables first by hard coding into the docker compose file, second grabbing variables from your current bash environment, and finally from an env file.

Prerequisites

If you’re following along with the example for docker compose environment variables make sure you have the following requirements met:

  • Docker installed
  • Docker Compose installed

Setup a base docker-compose file

Before we begin let’s set up a docker-compose.yaml file that will be used in the next 3 sections.

1.) We’ll start by creating a simple docker compose file that will spit out it’s environment variables (command: /usr/bin/env).

version: '3'
services:
  alpine:
    image: 'alpine:latest'
    command: '/usr/bin/env'

2.) Perfect. Let’s run a `docker-compose up` to ensure that it will correctly show the environment variables.

$ docker-compose up
Creating alpine_1 ... done
Attaching to alpine_1
alpine_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
alpine_1  | HOSTNAME=7d4186af1e4e
alpine_1  | HOME=/root
alpine_1 exited with code 0

If you parse through that output you’ll see that it shows the variables PATH, HOSTNAME, and HOME. The following sections will help you add to this.

Hard Coded Docker Compose Env Variables

This is a great path to take if you just need to edit a few non-sensitive values in your docker compose environment.

1.) Let’s add a new docker compose environment variable and set ‘ENV’ to ‘testing’ on line 6 & 7. Note that the ‘environment’ section is a list, so you can add as many env vars there as you like.

version: '3'
services:
  alpine:
    image: 'alpine:latest'
    command: '/usr/bin/env' 
    environment:
      - ENV=testing

2.) Confirm that the docker compose env variable is working correctly by performing another `docker-compose up` and looking for `ENV=testing`.

$ docker-compose up
Recreating play_alpine_1 ... done
Attaching to play_alpine_1
alpine_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
alpine_1  | HOSTNAME=65fd40d09592
alpine_1  | ENV=testing
alpine_1  | HOME=/root
play_alpine_1 exited with code 0

Existing Shell Docker Compose Env Variables

This method is essentially a more flexible approach to hard coding variables into your docker compose file.

1.) Since our goal is to pass an environment variable from our current shell to the docker compose container, we’ll start by setting the environment variable ‘API_KEY’ in our existing shell.

$ export API_KEY='123456'
$ echo $API_KEY
123456

2.) Secondly, we need to add to our docker-compose file (line 8) so that the environment variable ‘API_KEY’ resembles our super secure bash environment API_KEY.

version: '3'
services:
  alpine:
    image: 'alpine:latest'
    command: '/usr/bin/env'
    environment:
      - ENV=testing
      - API_KEY=$API_KEY

3.) And finally, we’ll test our docker compose env variables out by running `docker-compose up` and verifying we see our API_KEY.

$ docker-compose up
Recreating alpine_1 ... done
Attaching to alpine_1
alpine_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
alpine_1  | HOSTNAME=b897ba1ef10b
alpine_1  | ENV=testing
alpine_1  | API_KEY=123456
alpine_1  | HOME=/root
alpine_1 exited with code 0

Fun fact: You can also use your shell’s environment variables in the image tag. For example instead of image: “alpine:latest”, you could have it set to “alpine:$IMAGE_TAG”.

Docker Compose env variables: using an env file

This is a great route to take if your team is already using env files for their configuration and to differentiate between environments.

1.) Let’s first create an env file named ‘.env’ and throw some variables in it.

USER=jack
PASSWORD=arrrr

2.) Next up we can add some additional lines (9 & 10) that tell docker compose to use our .env file. You’ll notice that I left the ‘environment’ section in. Docker compose environment variables can be configured in multiple ways at once. If there are conflicts it will first default to the environment section and then to the env_file section.

version: '3'
services:
  alpine:
    image: 'alpine:latest'
    command: '/usr/bin/env'
    environment:
      - ENV=testing
      - API_KEY=$API_KEY
    env_file:
      - .env

3.) Let’s hit our `docker-compose up` again to confirm that the variables from our .env file were injected as expected. You should see that USER & PASSWORD have both been set alongside the ENV and API_KEY variables that haven’t changed.

$ docker-compose up
Recreating alpine_1 ... done
Attaching to alpine_1
alpine_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
alpine_1  | HOSTNAME=f6bacd231fdf
alpine_1  | USER=jack
alpine_1  | PASSWORD=arrrr
alpine_1  | ENV=testing
alpine_1  | API_KEY=123456
alpine_1  | HOME=/root
alpine_1 exited with code 0

Conclusion

Thanks for following through this article on Docker Compose environment variables. If this guide was useful for you be sure to bookmark it or add it to your team’s documentation. And if you’re a consistent user of Docker be sure to check out our Docker CLI Cheat Sheet.

1 thought on “Docker Compose Environment Variables”

  1. Pingback: Docker Image vs Container | The Difference - Swiss Army DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *