The ENV instruction in a Dockerfile sets environment variables that persist in the container environment and are available to all subsequent instructions and running containers.
The ENV instruction sets environment variables both during the image build process and in the final running container. These variables are available to all subsequent Dockerfile instructions (such as RUN, CMD, and ENTRYPOINT) and remain available when the container runs. This is essential for configuring applications that read configuration from environment variables, setting PATH modifications, defining default values, or making build-time information available at runtime.
Environment variables set with ENV remain in the final image and can be accessed in multiple ways: by running containers, through the docker inspect command, or when executing commands inside a container with docker exec. Users can also override these variables at runtime using the -e or --env flags with docker run, which is useful for adjusting configuration between environments without rebuilding the image.
ENV variables persist in the final image and are visible to anyone who can run the container, so avoid storing secrets in ENV .
Each ENV instruction creates a new layer, so combining multiple variables in one instruction reduces image size .
Variables are interpolated in later Dockerfile instructions, enabling dynamic path construction and configuration .
ENV values can be overridden at runtime with -e or --env flags, or through an env file with --env-file .
The scope includes the current build stage and all subsequent stages in multi-stage builds .