Sub topic 12
Why environment variables are a weak place for secrets
This chapter assumes you can run docker run -e and --env-file, and that you know the difference between an image and a container. It explains why a password passed as an environment variable is easy to expose, and what to use instead.
What an environment variable guarantees
An environment variable is a name and value that the operating system copies into a process and into every child process it starts. Nothing about this mechanism protects the value. Any code in the process can read it, and so can anything that can inspect the process or its configuration.
Docker adds more places where the value is stored. When you run -e POSTGRES_PASSWORD=example, the value becomes part of the container's saved configuration.
docker run -d --name db -e POSTGRES_PASSWORD=example postgres:16
docker inspect --format '{{json .Config.Env}}' dbThe output lists POSTGRES_PASSWORD=example in plain text. Anyone who can run docker inspect on this host can read it. In practice, access to the Docker daemon is close to root access, so this matters on shared machines and CI runners.
Where the value can leak
| Location | How it is exposed |
|---|---|
| Command line | `-e NAME=value` is saved in shell history and appears in the host's process list while `docker run` starts. |
| Container configuration | `docker inspect` prints it to anyone with daemon access. |
| Process environment | Root, or the same user, can read `/proc/<pid>/environ`. |
| Child processes | Every program the app starts inherits the value, including tools you did not write. |
| Logs and error reports | Debug output often prints the whole environment. Some crash reporters send it away. |
| Image layers | A secret set with `ENV` is stored in the image, and `docker history` can show it. |
Using --env-file removes the value from the command line and shell history. It does not change the other rows. The file is also a secret on disk, so keep it out of version control and restrict who can read it.
Setting a secret at build time is worse
A line such as ENV API_KEY=abc123 in a Dockerfile copies the key into the image. Everyone who pulls the image gets it, and deleting the line later does not remove it from images already pushed. Treat any secret that has entered an image as exposed and rotate it.
A safer pattern: read the secret from a file
Many images accept a path to a file instead of the value. The official Postgres image supports POSTGRES_PASSWORD_FILE. Mount the file read-only and pass only its path in the environment.
printf 'example' > db_password.txt
docker run -d --name db-file \
-e POSTGRES_PASSWORD_FILE=/run/secrets/db_password \
--mount type=bind,source="$(pwd)/db_password.txt",target=/run/secrets/db_password,readonly \
postgres:16Now docker inspect shows only the path. The password stays in a file that you can protect with host permissions. The container's user must be able to read the file, so check its mode if the container fails to start with a permission error.
Orchestrators go further. Docker Swarm secrets and Compose secrets mount the value as a file under /run/secrets, and Kubernetes can do the same. Cloud secret managers can supply the value when the app starts, and they log who read it.
Deciding what to use
- Use plain
-efor non-sensitive settings such asLOG_LEVELor a hostname. - Use
--env-filefor local development secrets that have no value outside your machine, such as theexamplepassword above. - Use mounted secret files or a secret manager for anything real, shared, or long-lived.
- Never put a secret in
ENVorARGin a Dockerfile.
Exercise
You run docker run -d --name api --env-file ./prod.env myapp, where prod.env holds a production database password. Name two ways the password can still be read, and one change that reduces the risk.
Show answer
The password is still visible to anyone with daemon access through docker inspect api, and to root or the same user through /proc/<pid>/environ inside the container. It can also appear in logs if the app prints its environment. To reduce the risk, put the password in a file, mount it read-only, and pass its path (for example with a *_FILE variable if the app supports it), or fetch it from a secret manager at startup.