Keywords: Dockerfile | Environment Variables | ENV Instruction | RUN export | Image Persistence
Abstract: This paper provides an in-depth examination of the fundamental differences between the ENV instruction and RUN export command for environment variable configuration in Dockerfile. Through comparative experiments and analysis of Docker image layer principles, it reveals that variables set with ENV persist during container runtime, while those set with RUN export are only valid within the same build layer and cannot propagate across layers. The article combines official documentation with practical cases to explain the lifecycle management mechanism of environment variables in Docker image construction, offering developers proper guidance for environment variable configuration.
Comparative Analysis of Environment Variable Persistence Mechanisms
During Docker image construction, the method of setting environment variables significantly impacts their availability in the final container. Although both the ENV instruction and RUN export command can set environment variables, their persistence mechanisms differ fundamentally.
Persistence Characteristics of ENV Instruction
The ENV instruction is Dockerfile's dedicated method for declaring environment variables. Variables set with ENV are persisted into the generated image. According to Docker official documentation, environment variables set via ENV will persist when a container is run from the resulting image. This persistence manifests in several aspects:
- Variable values can be viewed using the
docker inspectcommand - Variables can be overridden during container runtime using
docker run --env <key>=<value> - Variables remain available in all subsequent build steps
Temporary Limitations of RUN export
In contrast, variables set with RUN export are only valid within the same build layer. This occurs because each Dockerfile instruction creates a new intermediate container, which is committed as an intermediate image. Variables set via export are not preserved in this process. As demonstrated in Docker GitHub issue 684, when attempting to extend PATH using RUN export PATH=$PATH:/foo/bar, subsequent docker run commands cannot see the /foo/bar path.
Experimental Verification and Analysis
The following Dockerfile example clearly demonstrates the differences between the two approaches:
FROM centos:6
ENV FOO=foofoo
RUN export BAR=barbar
RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"
The output during the build process shows:
Step 4/4 : RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"
---> Running in eb66196b238d
foofoo bazbaz
From the output, three key observations can be made:
- The
FOOvariable (set viaENV) remains available in subsequent build steps - The
BARvariable (set viaRUN export) becomes inaccessible in the next step - The
BAZvariable is set and used within the sameRUNinstruction, thus displaying correctly
Docker Image Layer Principles
The fundamental reason for this difference lies in Docker's image layer architecture. Each Dockerfile instruction creates a new image layer, and the persistence of environment variables depends on which layer they are recorded in:
- Variables created with
ENVinstruction are recorded in image metadata, becoming part of the image - Variables set with
RUN exportexist only in the container environment during that instruction's execution - When a container is committed as an image, variables set via
exportare not included in the image
Practical Application Recommendations
Based on the above analysis, to ensure proper persistence of environment variables in Docker containers, it is recommended to:
- Use
ENVinstruction to declare environment variables that need to be used across multiple build steps - Use
exportfor temporary variables that are only used within a singleRUNinstruction - For critical system variables like PATH, prioritize using
ENVto ensure availability during container runtime - Consider using
ARGinstruction in combination withENVfor build-time parameter passing
Conclusion
The differences between ENV and RUN export in Docker environment variable management reflect the layered nature of Docker image construction. Understanding these differences is crucial for writing efficient and reliable Dockerfiles. Proper use of the ENV instruction ensures environment variables remain available throughout the image's lifecycle, while misuse of RUN export may cause variables to become inaccessible at critical moments, affecting the normal operation of container functionality.