4.2.8.1 ENV Build Defaults
A focused guide to ENV Build Defaults, connecting core concepts with practical Docker and container operations.
ENV build defaults are environment variables set specifically to influence the behavior of the build process itself — package managers, compilers, build tools — rather than to configure the application's eventual runtime behavior.
Configuring a Package Manager's Build-Time Behavior
Many package managers respect environment variables that adjust their behavior during installation, such as disabling interactive prompts or download caching.
ENV PIP_NO_CACHE_DIR=1
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
Setting DEBIAN_FRONTEND=noninteractive prevents apt-get from attempting to display interactive prompts during a non-interactive build process, which would otherwise cause the build to hang.
Configuring Compiler or Build Tool Behavior
Build-time environment variables can also influence how a compiler or build tool behaves, such as enabling or disabling specific features during compilation.
ENV CGO_ENABLED=0
RUN go build -o app .
This disables CGo specifically to produce a statically linked binary suitable for a minimal final image.
Distinguishing Build Defaults From Runtime Defaults
Because ENV variables persist into the final image's runtime configuration as well, a variable set purely to influence the build (and not relevant to the running application) still remains present and visible inside any container started from the image, unless deliberately unset afterward.
ENV PIP_NO_CACHE_DIR=1
RUN pip install -r requirements.txt
ENV PIP_NO_CACHE_DIR=
Explicitly clearing a build-only variable after it has served its purpose avoids leaving build-specific configuration unnecessarily present in the running container's environment.
Using ARG Instead for True Build-Only Values
For values that should genuinely never persist into the final image at all, ARG is generally the more appropriate instruction, since it does not carry forward into the resulting image's runtime configuration the way ENV does.
ARG BUILD_VERSION
RUN echo "Building version $BUILD_VERSION"
Why Distinguishing Build Defaults Matters
Being deliberate about which environment variables are meant only to influence the build process, versus which are meant to configure the application at runtime, keeps the final image's environment clean and avoids leaving unnecessary or potentially confusing build-time artifacts visible inside running containers.