#Claude Code#Docker#Security#Secrets#DevOps

Claude Code's Local Settings Are Leaking Into Docker Images: Fix the Build Context, Not .gitignore

webhani·

AI coding agents write small state files into your repository. Claude Code, for example, stores per-user permission decisions in .claude/settings.local.json. When you approve a command with "don't ask again", the exact command string is saved there, and if that command contained a token, the token is now on disk in plain text.

According to GitGuardian's research as relayed in press coverage, the scale is not small: 31,088 distinct secrets found in these files across 23,276 public GitHub repositories, with 5,343 still valid at detection time. The same research reports 613 secrets from Claude Code settings files inside public Docker Hub images, 99 of them valid. We could not locate the original report and are working from secondary coverage, so treat the exact numbers as reported rather than verified. The mechanism, however, is easy to reproduce and worth fixing regardless of the numbers.

Why .gitignore does not help here

Most teams already ignore the local settings file in Git. That protects the repository history. It does nothing for docker build.

Docker does not read .gitignore. The build context is whatever directory you pass to docker build, minus the patterns listed in .dockerignore. A typical Dockerfile contains:

FROM node:22-slim
WORKDIR /app
COPY . .
RUN npm ci && npm run build

COPY . . copies every file in the context, including untracked and git-ignored ones. So the file you carefully excluded from Git lands in an image layer. If that image is pushed to a registry, anyone who can pull it can run docker save or inspect the layer and read the file. Deleting the file in a later layer does not remove it from earlier layers.

The same applies to other local, agent-generated or tool-generated files: .env.local, MCP configuration with embedded credentials, editor state, shell history that ended up in the project directory.

Fix 1: Make .dockerignore the allow-list's mirror

Start by excluding the known risky paths:

# .dockerignore
.git
.claude/settings.local.json
.claude/
.cursor/
.env
.env.*
!.env.example
*.pem
*.key
node_modules
.next

Excluding the whole .claude/ directory is the safer default for application images. Your container does not need agent configuration to run. If you keep shared, committed agent settings in .claude/settings.json, they still belong in Git, not in the runtime image.

A stricter pattern is to invert the logic: ignore everything, then allow only what the build needs.

# .dockerignore (allow-list style)
*
!package.json
!package-lock.json
!src/
!public/
!next.config.mjs
!tsconfig.json

This costs a few minutes when you add a new top-level directory, and in exchange new stray files never enter the build by accident. For repositories where several people and several tools write into the working directory, we prefer this style.

Fix 2: Do not pass build-time secrets through the context at all

If the build legitimately needs a credential (a private npm registry token, for example), use BuildKit secret mounts. The secret is available during one RUN step and is not stored in any layer:

# syntax=docker/dockerfile:1
FROM node:22-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
COPY src ./src
RUN npm run build
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp:dev .

Avoid ARG NPM_TOKEN and ENV for secrets. Both are recorded in image metadata and can be read with docker history or docker inspect.

Fix 3: Keep tokens out of agent approvals

The deeper cause is that approving a command with an inline credential writes the credential into a file. Two habits reduce this:

  • Never put secrets on the command line. Read them from environment variables or a secret manager, so the saved command string contains $GITHUB_TOKEN, not the token.
  • Review .claude/settings.local.json occasionally. It is a record of what you allowed, and it is useful for noticing overly broad rules as well as leaked values.

Audit what you have already shipped

Fixing the Dockerfile does not recall images already pushed. A short audit:

  1. List images you have pushed to public or shared registries in the last year.
  2. For each, check for agent and env files:
docker create --name probe myorg/myapp:latest
docker export probe | tar -tf - | grep -E '\.claude/|\.env|\.cursor/'
docker rm probe
  1. If anything shows up, open the file, assume every value in it is compromised, and rotate. Rotation matters more than deletion: a pulled image stays pulled.
  2. Add a secret scanner (such as ggshield or trufflehog) to CI so it scans both the repository and the built image.

Our recommendation

Treat the Docker build context as a publishing boundary, the same as a git push to a public repository. Three changes cover most of the risk: an allow-list .dockerignore, BuildKit secret mounts for anything a build needs, and a CI step that scans the final image. None of them is expensive, and together they close the gap that .gitignore leaves open.

If you want a second pair of eyes on your container build pipeline, this is a short review that we do regularly for client teams.