Docker
Dockerfile
ENV command
environment variables
command execution

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:

dockerfile
ENV <key> <value>

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:

dockerfile
1FROM alpine:latest
2
3# Use RUN to capture the command's output to a temporary file
4RUN echo $(date +%Y-%m-%d) > /tmp/build_date
5
6# Use ENV to set the environment variable using the temporary file
7ENV BUILD_DATE=$(cat /tmp/build_date)

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:

dockerfile
1FROM alpine:latest
2
3# Combine commands to reduce layers and dynamically set ENV
4RUN export BUILD_DATE=$(date +%Y-%m-%d) && \
5    echo "export BUILD_DATE=$BUILD_DATE" >> /etc/profile
6
7ENV BUILD_DATE $BUILD_DATE

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:

dockerfile
ARG VERSION
ENV VERSION=$VERSION

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, COPY or ADD instruction 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.
FeatureDescription
ENV in DockerfileSets key-value environment variables within the build context.
Command SubstitutionNot supported in ENV; only RUN executes shell commands.
Build ArgumentsPass computed values with ARG and --build-arg, then assign them with ENV.
Caching ConsiderationsUse strategies that benefit from Docker's caching mechanism for lighter builds.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design