Dockerfile: set an ENV variable to the result of a command
Codemia Knowledge Hub ·
You cannot set ENV directly to a command's output: Dockerfiles do not interpret shell syntax such as backticks or $(command) inside ENV. Only RUN executes shell commands, and its variables do not carry over to later instructions. To get a computed value into an environment variable, pass it in at build time with ARG and --build-arg, then assign it with ENV.
How ENV works
ENV defines an environment variable:
The value is normally a static string. Anything dynamic has to come from somewhere else.
Why the RUN-plus-ENV workarounds fail
A common attempt writes the command output to a file in RUN and reads it back in ENV:
This does not work. ENV never runs cat, so the $(...) is not substituted, and current BuildKit builds reject the line with a syntax error.
Another attempt exports the variable inside RUN:
This builds, but BUILD_DATE ends up empty in the container. The export only lives in that RUN step's shell, so $BUILD_DATE is undefined when ENV is evaluated. The line appended to /etc/profile does not change that.
Pass the value with ARG and --build-arg
Build arguments differ slightly from environment variables but let you supply values when the image is built:
Then run docker build --build-arg VERSION=1.0 .. You can compute the value in your shell when you call docker build, which also helps with reproducible builds.
Layers, caching and secrets
- Layers: each
RUN,COPYorADDinstruction adds a layer, so consolidate commands where you can and avoid unnecessary intermediate files. - Caching: Docker caches build steps to speed up builds, so plan dynamic values with the cache in mind.
- Runtime changes: environment variables set later in a running container do not change the image layers.
- Security: do not store sensitive data with
ENV, because it persists in the image layers.
| Feature | Description |
ENV in Dockerfile | Sets key-value environment variables within the build context. |
| Command Substitution | Not supported in ENV; only RUN executes shell commands. |
| Build Arguments | Pass computed values with ARG and --build-arg, then assign them with ENV. |
| Caching Considerations | Use strategies that benefit from Docker's caching mechanism for lighter builds. |
Related reading
- Dockerfile copy keep subdirectory structure
- Dockerfile if else condition with external arguments
- Dockerfile Is there any way to read variables from .env file
- Dockerfile strategies for Git
- Docker/Kubernetes Gunicorn/Celery - Multiple Workers vs Replicas?
- docker.sock permission denied
- Does Alpine have known DNS issue within Kubernetes?
- Does Kubernetes cache docker-registry secrets?
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.