Guides / Process

Secrets in .env files, Git and Docker images: where they leak and what to do

beginnerUpdated 26 Nov 2026847 words

An API key in a .env file is convenient. It is also one git add . away from a public repository, one COPY . . away from a container image, and readable by anyone who can run docker inspect. On small setups, secrets leak in a few common places that you can check. A secret that has leaked has to be rotated; deleting it from the repository is not enough.

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

Dedicated scanners such as gitleaks and TruffleHog look for hundreds of key formats in the whole history. Hosting platforms scan too: GitHub secret scanning and push protection reject or report known token formats.

Make sure .env cannot be 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'

Even public values end up there. 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

A database password set with ENV in a Dockerfile is just as visible 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 the old one has been copied: forks, clones, caches and backups keep copies you cannot delete.

Then clean up, so the old value does not cause confusion or reappear:

# remove a file from the whole history (tool: git-filter-repo)
git filter-repo --path .env --invert-paths

This rewrites every commit; everyone has to clone again, and you need to force-push. On a hosting platform, ask support to purge cached views of the old commits if the repository was public.

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

You do not need a vault server to improve on a world-readable .env.

File permissions. The file belongs to the user the service runs as, nobody else can read it:

sudo chown app:app /srv/app/.env && sudo chmod 600 /srv/app/.env

Keep secrets out of images. Use .dockerignore with .env in it, pass configuration at run time, and use build secrets for values needed during the build (BuildKit):

# 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. The value does not show up in docker inspect. systemd services can do the same with LoadCredential=, which places the file in a directory named by $CREDENTIALS_DIRECTORY.

Separate secrets per environment and per service. A leaked staging key should not open production, and the web app does not need the backup credentials.

Know where every secret lives. A short list (secret, where it is stored, which servers use it, how to rotate it) turns a leak or an incident into a routine task. The guide on the first hour of a security incident relies on it.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free