Using Variables in Docker Compose Files

Use variables to pass sensitive content like passwords or API keys in docker-compose file.

Variables in docker-compose.yml denoted by ${}can be used follows:

services:
  app:
    . . . #snipped
    environment:
	  - GITHUB_CLIENT_ID=${CLIENT_ID}
	  - GITHUB_CLIENT_SECRET=${CLIENT_SECRET}

These variables are basically environment variables passed when invoking the docker-compose command. This can be done in multiple ways:

.env file

Beside your docker-compose.yml file, create a new text file called .env. In it, add the following:

CLIENT_ID=yourclientid
CLIENT_SECRET=yourclientsecret

Set the enviroment variables in the shell before invoking docker-compose commands

Include KEY=value pairs in the command line right before calling docker-compose

GITHUB_CLIENT_ID=yourclientid GITHUB_CLIENT_SECRET=yourclientsecret docker-compose up -d