ARG vs ENV: build-time and run-time variables
Select an icon to see what it is.
Step 6 of 6
Do not put secrets in either
A password in ARG or ENV can often be read from the image, its history or the container's inspect output. For build-time secrets, use a BuildKit secret mount, RUN --mount=type=secret, which is not stored in a layer. For run-time secrets, pass a file or use a secrets feature, as in the Compose secrets lesson.
PrevNext
Safer pattern for build-time secrets
RUN --mount=type=secret,id=token ./fetch.sh (reads /run/secrets/token, never stored in a layer)docker build --secret id=token,src=token.txt .
Try it yourself
Build with an ARG, run with and without an override, then look for where the values show up.
$ printf 'FROM alpine\nARG APP_VERSION=1.0\nARG BUILD_ONLY=unset\nENV APP_VERSION=$APP_VERSION\nENV GREETING=hello\nRUN echo "build arg: $BUILD_ONLY" > /build-info.txt\nCMD ["sh", "-c", "echo $GREETING from $APP_VERSION"]\n' > Dockerfile$ docker build --build-arg BUILD_ONLY=demo-value --build-arg APP_VERSION=2.0 -t argdemo .$ docker run --rm argdemo$ docker run --rm -e GREETING=hi argdemo$ docker run --rm argdemo env$ docker history --no-trunc argdemo # look for demo-value; it may or may not appear, depending on the builder