GrepBoard/Manuals
605 manuals ·

Find an API key left in a Docker image, use build secrets

·Tested on Docker Engine 29.9.2 · Buildx v0.38.1 · alpine:3.20 · tested with fake keys · 2026-11·Test scope ↓·7 steps

A key passed with ARG shows in docker history; with ENV it also shows in inspect and the container. How to find it and fix it with RUN --mount=type=secret.

A key passed with docker build --build-arg API_KEY=… shows in docker history for anyone who pulls the image. The Docker docs say build arguments and environment variables "persist in the final image", so they are inappropriate for secrets. Fake values show where each one lands.

[1] If the image is already in a registry, rotate the key first. Deleting the image or tag does not tell you who pulled it meanwhile. Revoke the key at the provider, issue a new one, then clean up the image (Gemini example).

[2] Make a dedicated work folder, then look at the build history of an image that takes one value with ARG and one with ENV.

console
$ mkdir dbs-work && cd dbs-work && mkdir save
Dockerfile.arg
FROM alpine:3.20ARG API_KEYENV SERVICE_TOKEN=FAKE-KEY-ENV-0001RUN test -n "$API_KEY"
console
$ docker build -q -t dbs-arg --build-arg API_KEY=FAKE-KEY-ARG-0002 -f Dockerfile.arg . > /dev/null$ docker history --no-trunc --format '{{.CreatedBy}}' dbs-arg
output
RUN |1 API_KEY=FAKE-KEY-ARG-0002 /bin/sh -c test -n "$API_KEY" # buildkitENV SERVICE_TOKEN=FAKE-KEY-ENV-0001ARG API_KEY=FAKE-KEY-ARG-0002…

The value is printed on three lines. Without --no-trunc the column is cut off.

[3] **ARG and ENV leave traces in different places.** Check the image's environment variables and the container's.

console
$ docker image inspect --format '{{range .Config.Env}}{{println .}}{{end}}' dbs-arg$ docker run --rm dbs-arg env | grep -E 'API_KEY|SERVICE_TOKEN'
output
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binSERVICE_TOKEN=FAKE-KEY-ENV-0001 SERVICE_TOKEN=FAKE-KEY-ENV-0001

ENV is in both; ARG is in neither. Not seeing it in env is no proof of safety. The docs say an ARG is "visible in the docker history command".

[4] Unpack the image and scan its layers and config file (some files are compressed, hence zcat -f). You will reuse two functions.

console
$ unpack() { docker save "$1" -o "save/$1.tar" && mkdir "save/$1" && tar -xf "save/$1.tar" -C "save/$1"; }$ scan() { for f in "$1"/blobs/sha256/*; do zcat -f "$f" | grep -aq "$2" && echo "$f"; done; }$ unpack dbs-arg$ scan save/dbs-arg FAKE-KEY
output
save/dbs-arg/blobs/sha256/f53a480985936be7108e2561a35c938c76c99f328f62c6398b307922df00284d

The match is the image config (JSON). For a real key, search for its first few characters only.

[5] Fix it. Pass the key as a build secret and read it only inside RUN --mount=type=secret. The value exists at /run/secrets/<id> for that step only.

Dockerfile.secret
FROM alpine:3.20RUN --mount=type=secret,id=api_key test -s /run/secrets/api_key
console
$ FAKE_KEY=FAKE-KEY-SECRET-0003 docker build -q -t dbs-secret --secret id=api_key,env=FAKE_KEY -f Dockerfile.secret . > /dev/null$ docker history --no-trunc --format '{{.CreatedBy}}' dbs-secret
output
RUN /bin/sh -c test -s /run/secrets/api_key # buildkit…
console
$ unpack dbs-secret$ scan save/dbs-secret FAKE-KEY$ echo "scan done"
output
scan done

No value in the history or in the scanned files. The same scan found it in [4], so the command is not what failed to find it.

[6] Also check where the secret goes after it is read: a file added with COPY and removed by RUN, and a secret copied into a file in the image.

Dockerfile.traps
FROM alpine:3.20COPY .env.fake /app/.envRUN rm /app/.envRUN --mount=type=secret,id=api_key mkdir /app2 && cp /run/secrets/api_key /app2/key.txt
console
$ echo 'API_KEY=FAKE-KEY-FILE-0004' > .env.fake$ FAKE_KEY=FAKE-KEY-SECRET-0005 docker build -q -t dbs-traps --secret id=api_key,env=FAKE_KEY -f Dockerfile.traps . > /dev/null$ unpack dbs-traps$ L=$(scan save/dbs-traps FAKE-KEY-FILE)$ echo "$L"$ zcat -f "$L" | tar -xO app/.env$ docker run --rm dbs-traps cat /app2/key.txt
output
save/dbs-traps/blobs/sha256/c00616a4da9ffe0b7eaec2e245432677002005dc393f1274b648e968f7bd9812API_KEY=FAKE-KEY-FILE-0004FAKE-KEY-SECRET-0005

The deleted .env is still in the COPY layer. RUN rm only hides it in the next layer. The copied /app2/key.txt is readable too. A key needed at run time should not be in the image; pass it when the container starts.

[7] Clean up. Check with ls that the folder you are about to delete is the dbs-work made in [2].

console
$ docker rmi dbs-arg dbs-secret dbs-traps$ cd .. && ls dbs-work$ rm -rf dbs-work

#docker#dockerfile#build-arg#secret#docker-history#buildkit