brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker build --secret hands a password to one build step without baking it into the image

Docker
~/tips/tip-docker-build-secret

docker build --secret id=token,src=$HOME/.build-token -t example/app:1.0 .

Passes a private value to one Dockerfile step as a temporary file that never lands in the image.

A build argument with a token in it ends up in the image. A build secret does not, and it is not much harder to use, so I would pick it for anything private.

You give the secret an id on the command line, with a file or an environment variable as the source:

docker build --secret id=token,src=$HOME/.build-token -t example/app:1.0 .
docker build --secret id=API_TOKEN -t example/app:1.0 .

The second form reads the variable with the same name as the id, so $API_TOKEN. In the Dockerfile you mount the secret on the one RUN line that needs it:

RUN --mount=type=secret,id=token cat /run/secrets/token | some-download-tool

By default the secret shows up as a file at /run/secrets/<id>, and you can pick another path with target= or hand it over as an environment variable with env=. It exists only while that instruction runs.

The docs say not to pass secrets through build arguments or environment variables, because they persist in the final image. A secret mount only protects you if your own command does not copy the value somewhere, like writing it into a file in the image or printing it to the build log. And for SSH keys the docs say to use an SSH mount (--ssh) rather than a secret.

Further reading: Build secrets

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link