How to Keep Secrets Out of Docker Images and Compose Files
A database password in a docker-compose.yml file feels harmless until the repo goes public, a teammate forks it, or a registry image gets pulled by someone outside your team. Docker makes it easy to ship configuration with your app. It also makes it easy to ship secrets by accident.
This guide covers where secrets leak in Docker workflows, and how to keep them out of images and Compose files without adding an enterprise tool.
Where secrets leak
Three places cause most of the trouble:
- Hardcoded values in Compose files. A line like
POSTGRES_PASSWORD: hunter2ends up in version control and in every copy of the repo. - Build arguments.
ARGvalues are visible in image history. Anyone who can pull the image can rundocker historyand read them. - Copied
.envfiles. ACOPY . .in your Dockerfile can pull a local.envfile into the image layer, even if you later delete it in the same project.
Once a secret is in an image layer, deleting it in a later step does not remove it. The value stays in the earlier layer.
Use Compose secrets for runtime values
Compose supports a top-level secrets key. Each secret points to a file on the host, and Compose mounts it into the container at /run/secrets/<name>.
services:
api:
image: your-org/api:1.4.2
secrets:
- db_password
environment:
DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
db_password:
file: ./secrets/db_password.txt
Your application reads the file at startup instead of reading the value from the environment. This keeps the secret out of docker inspect output and out of the Compose file itself.
Two habits make this work:
- Add the secrets directory to
.gitignorebefore you create the first file. - Read secrets from the file path in your code, for example with a
_FILEconvention or a small helper that reads the file and trims the trailing newline.
Keep build-time secrets out of layers
Some builds need a token, such as a private package registry. Do not pass it with ARG or ENV. Use BuildKit secret mounts instead:
# syntax=docker/dockerfile:1
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN=$(cat /run/secrets/npm_token) npm ci
COPY . .
Build it with:
docker build --secret id=npm_token,src=./npm_token.txt -t your-org/api:1.4.2 .
The secret is available during that single RUN step and is not written to a layer.
Stop copying your local .env into images
Add a .dockerignore file so local configuration never enters the build context:
.env
.env.*
secrets/
*.pem
Then confirm it works. Build the image, run docker run --rm your-org/api:1.4.2 ls -la /app, and check that no .env file appears.
Use .env for Compose variables, not for secrets you ship
Compose reads a .env file in the project directory to fill in variables like ${IMAGE_TAG} inside your Compose file. That is a good use. Keep the file out of git, and keep real credentials out of it when you can. Use the secrets mechanism above for anything sensitive.
If you use env_file: to pass values into a container, treat that file the same way you treat any other secret source: restrict who can read it and rotate values when someone leaves the team.
Check your history
If a secret has already been committed or baked into an image, changing the file is not enough. Rotate the credential first. Then clean up the repo and any registry images that contain it. Our guide on removing a .env file from Git history covers the repo side.
A simple checklist
- No passwords or tokens in
docker-compose.ymlorDockerfile .dockerignoreexcludes.envfiles and secret directories- Runtime secrets are mounted with Compose
secrets - Build-time secrets use
RUN --mount=type=secret docker historyon your image shows no credential values- Secrets are rotated when someone with access leaves
Secrets belong in a store that controls who can see them, not in a file that travels with your image. Keep the Compose file boring, and keep the values somewhere you can rotate them.
