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: ARG exists only while building, ENV stays in the image

Docker
~/tips/tip-dockerfile-arg-env-difference

ARG NODE_ENV=production

ARG values are for the build and ENV values persist in containers, so they do different jobs.

For a long time I treated ARG and ENV as two spellings of the same thing. They are not, and mixing them up has two nasty side effects.

ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV

The docs describe build arguments as variables for the Dockerfile itself. You pass a value with docker build --build-arg NODE_ENV=development . and the default after the equals sign is used when you leave it out. Environment variables are passed to the build execution environment and, as the docs say, persist in containers started from the image.

The two lines above show how to combine them: the ARG takes the value at build time and the ENV copies it so the running container sees it as well. That example is straight from the Docker docs on build variables.

One more rule from the Dockerfile reference: an ENV instruction always overrides an ARG of the same name. So if you set both under one name, --build-arg will seem to be ignored after the ENV line.

The caveat is secrets. The docs warn that build arguments may persist in the image metadata and history, and that both arguments and environment variables are not suitable for secrets, because they are exposed in the final image. For a token during a build, use build secrets instead.

Further reading: Build variables, Dockerfile reference: ARG

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