Dockerfile ENV and ARG: build inputs and runtime defaults
Read the recipe
FROM busybox:1.37ARG BUILD_COLOR=blueRUN printf "%s\n" "$BUILD_COLOR" > /build-color.txtENV APP_MODE=demoCMD ["sh", "-c", "cat /build-color.txt; printf \"%s\\n\" \"$APP_MODE\""]BUILD_COLOR is consumed by RUN and recorded in a file. APP_MODE is part of the image environment. At runtime, the shell prints the file and the current environment value. ARG scope begins at its declaration; a global ARG before FROM must be declared again in the stage to use it there. A build argument can also be assigned to ENV deliberately, which makes its value persist in the image configuration. ARG and ENV can interact: a same-named ENV takes precedence for subsequent RUN commands. This example intentionally uses different names. Neither setting proves that confidential data is absent from an image.
Try it in a local lab
Manual lab only, not an interactive ByteMind execution. Use an approved disposable offline Linux VM, verified local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm the local context/builder, reviewed preloaded busybox:1.37 with root default, no entrypoint, and required applets. Record actual digest/platform and use reviewed digest for repeatability. No network egress; --network none is not a complete builder egress control. Verify every bm-v08 image tag and container name in the commands is unused. Use synthetic data only. Run commands individually and stop on unexpected failure; remove only resources created by this lab after ownership checks. If cleanup cannot be confirmed, retire the disposable VM. Expected results are predictions, not observed output. Before contacting any Engine, resolve DOCKER_HOST, DOCKER_CONTEXT and CLI context overrides; also check BUILDX_BUILDER and any builder override. Confirm that the actual builder used by docker build is the approved local docker driver. Review the base configuration for inherited triggers, volumes, health checks, user, shell and required applets; stop if assumptions differ. Make a private working copy containing only the supplied synthetic files. An approved digest may require registry metadata resolution even when layers are present locally; if the offline builder cannot resolve it, stop and record the limitation. Record the actual base image ID, platform, builder version and output image ID; never substitute an unknown digest. Do not retry by enabling egress. Apply owner-approved time and resource budgets and retire the VM if cleanup is uncertain.
Open lab-fixtures/dockerfile-configuration/01-env-arg/ from the extracted archive. Save the complete recipe and any additional files below in that directory.
docker build --load --network none --build-arg BUILD_COLOR=green -t bm-v08-env-arg:lab .docker run --rm --pull=never --network none bm-v08-env-arg:labdocker run --rm --pull=never --network none -e APP_MODE=test bm-v08-env-arg:labThe default run should print green and demo on separate lines because the build command selects green. The second run should print green and test. Changing APP_MODE does not rewrite the file. A plain ARG is not automatically a runtime environment variable.
Confirm the tag belongs to this lab and no container depends on it. Remove it without forcing deletion. Build cache may remain until VM disposal. Record output and exit status before removing the exact lab tag. Recheck the tag against the recorded output image ID; a tag may have been reassigned.
Common mistake
Neither instruction is a safe secret channel. Build arguments can be exposed by metadata or history; ENV values are visible in image/container configuration. Use only synthetic settings here; secret mounts are a later topic.
Manual practice
Predict the output if the build argument is omitted and the runtime value is set to review. Explain which value requires a rebuild to change the file.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dockerfile ENV and ARG: build inputs and runtime defaults — Knowledge check
Apply the configuration concept to a new image or container scenario.
Which setting is recorded in /build-color.txt?
Which flag changes APP_MODE for one container?
Why avoid secrets in ARG?