Docker Compose file cheatsheet

Docker Compose file: Environment and secrets

5 common Docker Compose file tasks.

Set environment variables

Set variables inline, as a list or a map.

environment:
  NODE_ENV: production
  LOG_LEVEL: info

Load variables from a file

Read variables from one or more env files. Keep them out of version control.

env_file:
  - .env

Use variables with defaults

Substitute values from the shell or .env, with a default or a required error.

image: myapp:${TAG:-latest}
environment:
  API_KEY: ${API_KEY:?API_KEY is required}

Use secrets

Mount a secret as a file under /run/secrets rather than an environment variable.

services:
  api:
    secrets:
      - db_password
secrets:
  db_password:
    file: ./secrets/db_password.txt

Mount a config file

Provide a configuration file to a service.

services:
  proxy:
    image: nginx:1.27
    configs:
      - source: nginx_conf
        target: /etc/nginx/nginx.conf
configs:
  nginx_conf:
    file: ./nginx.conf

Was this page helpful?