Sub topic 12
Keeping secrets out of Git and images
This chapter assumes you know how environment, env_file, .env files and interpolation work, and that you use Git and have built a Docker image. A secret is any value that grants access, such as a password, an API token, or a private key.
Where secrets leak
Compose makes it easy to pass values around, and each route can leak them:
| Route | How it leaks |
|---|---|
| Committed `.env` or `env_file` | The secret lives forever in Git history. |
| `COPY . .` in a Dockerfile | A `.env` file ends up inside an image layer. |
| Build args (`ARG`) | Values show up in `docker history` and image metadata. |
| `environment` values | `docker inspect` and `docker compose config` print them in plain text. |
Keep secret files out of Git
Ignore real env files and commit a template that holds only names and harmless placeholders.
# .gitignore
.env
*.local.env# .env.example (committed)
POSTGRES_USER=app
POSTGRES_PASSWORD=
DB_PORT=5432A new teammate copies .env.example to .env and fills in real values. Pair this with the required-variable syntax from earlier, ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set}, so Compose fails loudly when the value is missing.
Adding a file to .gitignore does not remove it from history if it was already committed. Rotate any secret that was committed, then remove the file from Git with git rm --cached .env.
Keep secrets out of images
Images get pushed, shared and cached, so anything in a layer should be treated as public. Add a .dockerignore so the build context never contains your env files:
# .dockerignore
.env
*.env
.gitDo not pass secrets as build args. If a build needs a credential, use a BuildKit build secret instead. It is mounted for one RUN step and is not stored in a layer. Runtime secrets belong in the container's environment or in files, not in the image.
Use Compose secrets for files
Environment variables are visible to anyone who can run docker inspect, and some applications log their whole environment. Compose has a top-level secrets element that mounts a file into the container at /run/secrets/<name>.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txtThe official postgres image reads the password from the file named by POSTGRES_PASSWORD_FILE. Many popular images support this _FILE convention. If your own application only reads a variable, change it to read the file at startup.
Add secrets/ to .gitignore as well.
Check what you expose
docker compose config prints resolved values, including secrets passed through environment. Do not paste its output into chat, tickets or CI logs without checking it first.
docker compose config | grep -i passwordSummary of habits
- Ignore real env files in Git and Docker, and commit a
.env.example. - Rotate any secret that has been committed or baked into an image.
- Use
:?to fail early when a secret is missing. - Prefer Compose secrets or
_FILEvariables over plain environment values. - Never use build args for credentials.
Exercise
A teammate adds API_TOKEN=abc123 to .env, runs git add . and pushes. Their Dockerfile also contains COPY . .. Name two places the token is now exposed and the first step you take.
Show answer
The token is in the Git history and in an image layer, because COPY . . copies .env into the image. The first step is to revoke and rotate the token. Then remove .env from Git with git rm --cached .env, add it to .gitignore and .dockerignore, and rebuild the image. Deleting the file in a later commit does not remove it from history.