Variables, env files and profiles

Intermediate · 5 min

One Compose file, many machines: ${VAR} interpolation, the .env file versus env_file, which value wins, and profiles for optional services.

Before thisThe Compose file: services, networks and volumesConfiguring containers with environment variables

VirtualizationHowto · 9 min · from 4:11

Variables, env files and profiles

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by VirtualizationHowtoReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Compose does two different things with variables. Interpolation fills `${VAR}` in the file itself. The `environment` and `env_file` attributes set variables inside the container.
  • The `.env` file next to compose.yaml feeds interpolation only. Its values reach a container only if the file references them, or if you list it under `env_file`.
  • For interpolation, your shell wins over `--env-file`, which wins over the project's `.env`.
  • Inside the container, `environment` beats `env_file`, which beats the image's `ENV`. A `docker compose run -e` beats them all.
  • `${VAR:-default}` supplies a fallback, `${VAR:?message}` stops Compose with an error when the value is missing, and `$$` passes a literal dollar sign through.
  • Services with `profiles` start only when one of their profiles is active, through `--profile` or `COMPOSE_PROFILES`. Services without profiles always start.
  • `docker compose config` shows the file after interpolation and merging, and `config --environment` shows which variables were used.

The same file on every machine

A Compose file in a repository runs on every developer’s laptop, in CI, and maybe on a server. The image tag differs per release. The database password differs per environment. One person needs a database admin UI and nobody else does. Copying the file per environment means fixing every bug three times.

Compose has two mechanisms for this: variables change values in the file, and profiles switch services on and off.

Two jobs called “environment”

Most confusion comes from mixing up two separate things.

Interpolation happens on your machine when Compose reads the file. ${TAG} in the YAML is replaced with a value before anything is sent to the engine. It works anywhere in the file: image names, ports, paths.

Container environment is the set of variables the process inside the container sees. You set it with environment and env_file.

The .env file in the project directory belongs to the first job. It is read for interpolation, and nothing in it reaches a container unless the file uses it. This is a common surprise: putting DEBUG=true in .env does not give the container DEBUG.

services:
  api:
    image: "registry.example.com/api:${TAG:-latest}"   # interpolation
    env_file: api.env                                  # container env, from a file
    environment:                                       # container env, inline
      LOG_LEVEL: ${LOG_LEVEL:-info}
      PGPASSWORD: ${DB_PASSWORD:?DB_PASSWORD must be set}

The syntax

From the interpolation reference:

Form Result
${VAR} the value of VAR
${VAR:-default} default if VAR is unset or empty
${VAR-default} default only if VAR is unset
${VAR:?message} stop with message if VAR is unset or empty
${VAR:+other} other if VAR is set and non-empty, else empty
$$ a literal $, for the container’s own shell to expand

Use :? for anything that must not silently be empty, like a password. An empty POSTGRES_PASSWORD is worse than a refusal to start.

Who wins

For interpolation, the docs give this order: variables from your shell, then a file passed with --env-file, then the .env file in the project directory.

Inside the container, highest first: docker compose run -e, then environment or env_file values interpolated from the shell or an env file, then plain environment values, then env_file values, then the image’s ENV.

I checked both orders by loading a test project with compose-go, the library the Compose CLI uses. The project had TAG=1.4.2 and DB_PASSWORD=from-dotenv in .env, and api.env set LOG_LEVEL=debug and FEATURE_X=on. The resolved api service:

    environment:
      FEATURE_X: "on"
      LOG_LEVEL: info            # environment beat env_file's "debug"
      PGPASSWORD: from-dotenv
    image: registry.example.com/api:1.4.2

Run again with TAG=2.0.0 DB_PASSWORD=from-shell in the shell, the image became api:2.0.0 and the password from-shell. With the .env file moved away, loading failed with:

required variable DB_PASSWORD is missing a value: DB_PASSWORD must be set

TAG and DB_PASSWORD themselves never appeared in the container environment. Only what the file referenced did.

When you need to know where a value came from, docker compose config prints the resolved file and docker compose config --environment lists the variables used.

Keep secrets out of the repo

Commit compose.yaml. Commit a .env.example with placeholder values. Add .env to .gitignore. Values typed into environment are visible to anyone who can run docker inspect on the container, so passwords are better passed as files. That is covered in secrets at build time and run time.

Profiles: optional services

Some services are only needed sometimes: an admin UI, a mail catcher, a seed script. Give them profiles:

services:
  api:
    image: registry.example.com/api:1.4.2
  db:
    image: postgres:18
  adminer:
    image: adminer
    profiles: [debug]
    depends_on: [db]
  seed:
    image: registry.example.com/api:1.4.2
    command: ["python", "-m", "seed"]
    profiles: [tools]

The rules, from the profiles reference:

  • A service with no profiles is always enabled. Keep your core services that way.
  • A service with profiles is ignored unless one of them is active: docker compose --profile debug up, or COMPOSE_PROFILES=debug docker compose up. --profile "*" enables all.
  • Naming a service on the command line activates its profile for that command. docker compose run --rm seed works without --profile tools, and starts seed’s dependencies too.
  • Networks and volumes are not affected by profiles.

Loading the file above with COMPOSE_PROFILES=debug gave adminer, api and db, and no seed. Without it, only api and db.

COMPOSE_PROFILES can itself live in .env, so a developer can opt in to their usual tools once and forget about it.

About the video

The embed is a clip, 4:11 to 12:53, of VirtualizationHowto’s “Stop Struggling with Docker Compose – Use These 10 Tricks Instead!”, August 2025. Its chapters in that range: keeping configuration values out of the Compose file, using a .env file, Docker labels, health checks, and Compose profiles. The labels and health-check parts in the middle are off this lesson’s topic (health checks are the previous lesson), but the clip shows .env and profiles on a real homelab setup. It does not cover precedence rules or env_file versus .env, which the lesson adds. English is confirmed by YouTube’s “English (US) original” audio flag. In deeper, Nick Janetakis spends a full video on profiles and COMPOSE_PROFILES, and Nana’s chapter covers variables.

What was run: the example project was loaded with a small Go program built on compose-go v2.17.0, the library Compose v5.8.1 uses, in four configurations: defaults, shell overrides, COMPOSE_PROFILES=debug, and no .env. The outputs shown are its real output. No daemon was available, so nothing was started. The precedence lists are from Docker’s documentation, checked 2026-11-28.

Where this fits

Variables and profiles adapt one file. For a fast inner loop while you code, Compose can also layer files and sync your edits into running containers: Compose for development: watch, overrides and include.

Check yourself

  1. 01Your .env has DEBUG=true, and compose.yaml doesn't mention DEBUG anywhere. Does the container see DEBUG?

    No. .env only feeds interpolation of the Compose file. To pass it in, reference it (environment: DEBUG: ${DEBUG}) or list the file under env_file.

  2. 02TAG=1.4 is in .env, and you run TAG=2.0 docker compose up. Which image tag is used?

    2.0. Variables from the shell take precedence over the .env file during interpolation.

  3. 03api.env sets LOG_LEVEL=debug, and the service's environment block sets LOG_LEVEL: info. What does the app see?

    info. The environment attribute takes precedence over env_file.

  4. 04How do you make docker compose up fail fast when DB_PASSWORD is not set, instead of starting with an empty password?

    Write ${DB_PASSWORD:?DB_PASSWORD must be set}. Compose stops with that message if the variable is unset or empty.

  5. 05adminer has profiles: [debug]. What do docker compose up and docker compose up adminer each start?

    Plain up starts every service without a profile, so not adminer. Naming adminer on the command line activates its profile, so it starts, along with its depends_on dependencies.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark