Configuring containers with environment variables
Passing settings with -e and --env-file, which value wins, the env-file format's surprises, and why environment variables are a poor place for secrets.
Configuring containers with environment variables
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- One image, many environments: the image carries defaults in its config, and each container overrides them at run time.
- -e KEY=value sets a variable, -e KEY copies it from your shell, and --env-file reads a file. A -e flag beats the env file, which beats the image's ENV.
- An --env-file is not a shell script. Quotes stay in the value, ${VAR} isn't expanded, and a # mid-line is part of the value.
- Environment is fixed when the container is created. Changing it means recreating the container, not restarting it.
- Environment variables are readable through docker inspect, /proc and crash dumps, so secrets belong in mounted files or a secrets manager.
One image, three environments
You build notes-api:1.4.2 once. In development it talks to a local Postgres and logs everything. In staging it uses a staging database. In production it logs warnings only and points at the real database. If any of that lived inside the image you’d need three images, and you’d no longer be shipping the thing you tested.
The twelve-factor answer is to keep configuration in the environment. Every process already has a list of KEY=value strings, and Docker sets that list when it creates the container. The image stays the same and the environment changes.
Defaults in the image, values at run time
A Dockerfile’s ENV lines land in the image config as defaults. The official python:3.14-slim image, which I pulled with the registry API, carries these:
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
PYTHON_VERSION=3.14.10
PYTHON_SHA256=de414a57119abef52784d8993e341d53aed635fc2b05c2607d9f98a2c5b72032
At run time, three flags add or override values:
docker run -e LOG_LEVEL=debug notes-api:1.4.2 # set a value
export AWS_REGION=eu-west-1
docker run -e AWS_REGION notes-api:1.4.2 # copy it from your shell; omitted if unset
docker run --env-file ./prod.env notes-api:1.4.2 # read a file of KEY=value lines
Build-time variables (ARG) are a different mechanism that disappears after the build. The build arguments lesson covers them.
Which value wins
The Docker CLI source settles it. ReadKVEnvStrings reads every --env-file first, then appends the -e flags “to allow override”, and the daemon lays the result over the image’s ENV. The last value for a key wins:
image ENV < --env-file (in order given) < -e / --env
The env-file format is not a shell
This is the trap. Docker’s --env-file parser, pkg/kvfile in docker/cli, says in its own header: “no interpolation, substitution or escaping is supported, and quotes are considered part of the key or value”. People write env files as if a shell or Compose read them. To see what happens, I ported the parser to Python line for line, merged it the way the CLI does, and fed it this file:
# notes-api settings
DATABASE_URL="postgres://notes@db:5432/notes"
LOG_LEVEL=warn
GREETING=hello world
CACHE_DIR=${HOME}/cache
TIMEOUT=30 # seconds
INDENTED=ok
AWS_REGION
NOT_SET_ON_HOST
With image defaults LOG_LEVEL=info and PORT=8080, -e LOG_LEVEL=debug, and AWS_REGION=eu-west-1 in the shell, the result was:
LOG_LEVEL='debug' -e beat the file, which beat the image
PORT='8080' image default kept
DATABASE_URL='"postgres://notes@db:5432/notes"' quotes are part of the value
GREETING='hello world ' trailing spaces kept
CACHE_DIR='${HOME}/cache' not expanded
TIMEOUT='30 # seconds' mid-line # is not a comment
INDENTED='ok' leading whitespace trimmed
AWS_REGION='eu-west-1' bare name copied from the shell
NOT_SET_ON_HOST vanished, because a bare name with nothing in the shell is left out entirely. The same file read by Docker Compose behaves differently, since Compose’s .env handling does interpolate and strip quotes. That’s covered in variables, env files and profiles. For docker run, write values bare.
Fixed at creation
The environment is part of the container’s configuration, stored when the container is created. docker restart reuses it. Edit prod.env, restart, and nothing changes. You have to remove the container and create a new one (docker rm -f api && docker run ..., or docker compose up -d, which recreates services whose config changed). Inside, docker exec api env shows what PID 1 actually received.
Not for secrets
It’s tempting to pass -e DB_PASSWORD=.... The value then sits in plain text in places you don’t control:
docker inspect apishows it underConfig.Envto anyone who can talk to the daemon./proc/1/environinside the container exposes it to any process there.- Child processes inherit it, and crash reporters and debug pages often dump the whole environment.
- If you put it in a Dockerfile
ENV, it’s baked into the image config for everyone who pulls it.
Better patterns: mount the secret as a file and point the app at it (many official images accept POSTGRES_PASSWORD_FILE=/run/secrets/db_password), use Compose or Swarm secrets, or fetch from a secrets manager at start-up. Secrets at build time and run time goes further.
About the video
The embed is all of DheerajTechInsight’s “Docker Environment Variables & Secrets Explained | Secure App Configuration”, January 2026, 15 minutes. Its chapters cover why configuration matters, passing environment variables to containers, using them in Docker Compose, why they aren’t secure for secrets, and practical secret-management strategies with a real-time example. That matches this lesson’s arc. It doesn’t show the env-file parser’s quirks or the precedence order, which the lesson adds from the CLI source. English was judged from its “English (US) original” audio track. Mayank’s video in deeper covers the build-time side, ARG against ENV.
What was run: the python:3.14-slim config was fetched from Docker Hub’s registry API, and the env-file behaviour came from a Python port of docker/cli’s pkg/kvfile parser and its readKVStrings merge order, run against the sample file above. That’s a stand-in, not Docker. The real parser is linked in deeper. Nothing ran in Docker. Checked 2026-11-28.
Where this fits
Next, where a container’s output goes: container logs and logging drivers.
Check yourself
Your env file has DATABASE_URL="postgres://db/notes" and the app fails to parse it. Why?
docker run's env-file parser keeps quotes as part of the value, so the app receives the string with literal double quotes at each end. Remove the quotes.
The image sets LOG_LEVEL=info, the env file sets LOG_LEVEL=warn and you pass -e LOG_LEVEL=debug. What does the app see?
debug. The CLI reads env files first and -e flags after them, and the image's ENV is only the starting point, so the last value set wins.
You changed a value in the env file and ran docker restart api. The app still sees the old value. Why?
The environment is part of the container's configuration, fixed at create time. docker restart reuses it. Remove and recreate the container, or use docker compose up, which recreates changed services.
Why is -e DB_PASSWORD=... a weak way to pass a password?
Anyone who can run docker inspect sees it in Config.Env, it's in /proc/1/environ inside the container, child processes inherit it and it can leak into logs and error reports. A mounted secret file limits who can read it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.