Sub topic 8
Passing configuration with environment variables
This chapter assumes you can use a terminal, know what a process environment variable is, and have Docker Engine or Docker Desktop installed. You should also know how to start a container with docker run.
Why configuration goes in the environment
An image should work the same everywhere. Settings such as passwords, hostnames, and log levels differ between machines, so you supply them when you start the container instead of baking them into the image. Environment variables are the usual way to do this. Docker sets them in the container's main process before it starts.
Setting variables with -e
The -e (or --env) flag sets one variable per use. The PostgreSQL image reads POSTGRES_PASSWORD on first start to set the password of the postgres superuser:
docker run -d --name db \
-e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data \
postgres:16The -v line stores the database files in a named volume. A later chapter covers volumes.
If you give -e a name with no value, Docker copies the value from your current shell:
export POSTGRES_PASSWORD=example
docker run -d --name db2 -e POSTGRES_PASSWORD postgres:16This keeps the secret out of the command text, and so out of your shell history. It does not hide it from docker inspect, as explained below.
Loading many variables from a file
For more than a few settings, use --env-file. Each line is NAME=value. Lines starting with # are comments.
printf 'POSTGRES_USER=app\nPOSTGRES_PASSWORD=example\nPOSTGRES_DB=appdb\n' > db.env
docker run -d --name db3 --env-file db.env postgres:16Values in the file are taken literally. Quotes are not stripped, so NAME="x" gives a value that includes the quote characters.
Checking what the container sees
Run env inside the container to see the result:
docker exec db3 envThe output includes your variables and some that Docker adds, such as HOSTNAME and PATH. Image authors can also set defaults with the ENV instruction in the Dockerfile. Those defaults show up too.
When the same name is set in several places, the order of precedence is: -e on the command line, then --env-file, then the image's ENV default. Anything you pass at run time overrides the image.
Common errors
- Changing a variable after the container exists. You cannot edit the environment of a created container. Remove it and run a new one with the new values.
- Initialization variables ignored. The PostgreSQL image applies
POSTGRES_PASSWORDonly when the data directory is empty. If the volumepgdataalready holds a database, changing the variable has no effect. - Missing required variable. The
postgresimage exits immediately if no password option is set. Checkdocker logs dbfor the message. - Spaces and special characters. In a shell, quote values that contain spaces or
$, for example-e "GREETING=hello world". - Committing env files. A file such as
db.envoften holds secrets. Keep it out of version control.
Reading the variables back
docker inspect shows the configured environment without entering the container:
docker inspect --format '{{json .Config.Env}}' db3This prints a JSON array of NAME=value strings. Use it when docker exec is unavailable, for example because the container has stopped.
Exercise
You started a container with -e LOG_LEVEL=info and now want debug. The container is named api. What do you do, and why can't you just edit the variable?
Show answer
Stop and remove the container (docker stop api, docker rm api), then start a new one with -e LOG_LEVEL=debug. The environment is fixed when the container is created, so it cannot be changed afterward. Any data that must survive should be in a volume.
Exercise
A variable is set in the image with ENV MODE=prod, in app.env as MODE=staging, and on the command line as -e MODE=test. Which value does the container see?
Show answer
test. Values from -e override --env-file, which overrides the image's ENV default.