Secrets in .env files, Git and Docker images: where they leak and what to do
A .env file with an API key reaches a public repository with one git add . and a container image with one COPY . .. A secret that has leaked has to be rotated: deleting it from the repository leaves the copies in clones, forks and caches.
Where secrets leak
| Place | How it leaks |
|---|---|
| Git repository | .env or a config file committed once stays in the history, even after it is deleted |
| Container image | COPY . . copies .env into a layer; ENV and ARG values used in the build are stored in the image metadata |
| Environment of a container | docker inspect shows it to anyone with access to the Docker socket |
| Environment of a process | /proc/<pid>/environ is readable by root and the same user; crash reports and debug pages print it |
| Logs and CI output | a command that echoes its configuration, or a failed request that logs headers |
| Backups and copies | an unencrypted backup of /etc or the app directory |
Check your repositories
Search the whole history as well as the current files:
# every file that was ever committed, on all branches
git log --all --name-only --format= | sort -u | grep -i -E '\.env|secret|credential|\.pem|id_rsa'
# commits that added or removed a string
git log --all --oneline -S 'DB_PASSWORD'
In a test repository where .env was committed and later removed with git rm --cached, the second command still finds both commits:
151fd9d two
e293bf5 one
Scanners such as gitleaks and TruffleHog look for hundreds of key formats in the whole history. GitHub secret scanning and push protection report or reject known token formats.
Keep .env from being committed again:
echo '.env' >> .gitignore
git check-ignore -v .env
Check your images
# environment and build steps stored in the image
docker history --no-trunc <image> | grep -E 'ENV|ARG'
docker inspect --format '{{json .Config.Env}}' <image>
# files in the image
docker run --rm --entrypoint sh <image> -c 'find / -xdev -name ".env*" 2>/dev/null'
The official python:3.13-slim image shows its ENV lines in the history:
ENV PYTHON_VERSION=3.13.18
ENV GPG_KEY=7169605F62C751356D054A26A821E680E5FA6305
ENV LANG=C.UTF-8
These values are public. A database password set with ENV in a Dockerfile shows up the same way to anyone who pulls the image.
When a secret has leaked
Rotate first
Revoke the key or change the password at the service that issued it, then deploy the new one. Assume someone has copied the old one: forks, clones, caches and backups keep copies you cannot delete.
Then clean up
# remove a file from the whole history (tool: git-filter-repo)
git filter-repo --path .env --invert-paths
This rewrites every commit. You need to force-push, and everyone has to clone again. If the repository was public, ask the support of the hosting platform to purge cached views of the old commits.
Check for use
The provider of the key often has an access log (cloud audit logs, API usage pages). Look for use from addresses you do not know between the leak and the rotation.
Store secrets better
None of the steps below needs a vault server.
File permissions
The file belongs to the user the service runs as, and nobody else can read it:
sudo chown app:app /srv/app/.env && sudo chmod 600 /srv/app/.env
Secrets out of images
Put .env in .dockerignore, pass configuration at run time, and use BuildKit build secrets for values the build needs:
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
docker build --secret id=npmrc,src=$HOME/.npmrc .
The secret is available during that one RUN step and is not stored in a layer.
Files instead of environment variables
Docker Compose can mount secrets as files under /run/secrets/:
services:
app:
image: registry.example.com/app:1.4
secrets: [db_password]
secrets:
db_password:
file: ./secrets/db_password
The application reads /run/secrets/db_password, and the value does not show up in docker inspect. systemd services can do the same with LoadCredential=, which places the file in the directory named by $CREDENTIALS_DIRECTORY.
One secret per environment and service
A leaked staging key should not open production, and the web app does not need the backup credentials.
A list of secrets
Keep a short list: each secret, where it is stored, which servers use it, how to rotate it. With that list, a leak means working through known steps. The first hour of a security incident uses it.
Sources
- git-filter-repo and Git documentation: git log -S
- Docker: build secrets and Compose: secrets
- systemd.exec: LoadCredential=
- GitHub: secret scanning and push protection
- Git commands tested with Git and git-filter-repo from Debian 12 in a
debian:bookworm-20261027-slimcontainer;docker historyofpython:3.13-slim, both on 2026-11-26
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse