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 run -e NAME without a value copies the variable from your shell

Docker
~/tips/tip-docker-run-env-passthrough

docker run --rm -e API_KEY example/app:1.0

Pass a secret from your shell into a container without typing it on the command line.

Typing -e API_KEY=s3cret puts the secret in your shell history and in the process list. If you give only the name, Docker takes the value from your current environment.

export API_KEY="$(cat ~/.config/app/api_key)"
docker run --rm -e API_KEY example/app:1.0

Inside the container, API_KEY has the same value as in your shell. A name that is not set in your shell is simply left out, rather than being passed as an empty string.

The same works in an env file: a line with just API_KEY and no equals sign takes the value from the shell that runs docker run --env-file. In Compose, a list item - API_KEY under environment: does the same.

This keeps the value out of your history, not out of the container. Anyone who can run docker inspect on the container still sees it in plain text under Config.Env. For a real secret I would rather mount a file into the container. And a missing variable gives no warning, so the app starts without its key. Check the variable is set first, or make the app fail loudly when it is empty.

Further reading: docker container run: set environment variables

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