✦ For everyone, free.

Practical knowledge for real and everyday life

Home

4.2.8 ENV

A focused guide to ENV, connecting core concepts with practical Docker and container operations.

ENV is the Dockerfile instruction that sets an environment variable, available both to subsequent build instructions and to every container started from the resulting image, unless explicitly overridden at run time.

Basic Usage

ENV accepts a key and a value, persisting that environment variable into the image's configuration.

ENV NODE_ENV=production
ENV PORT=8080

Both build-time instructions and any container started from the resulting image have access to these variables.

Setting Multiple Variables at Once

Multiple environment variables can be set in a single ENV instruction, which produces fewer layers than setting them across separate instructions.

ENV NODE_ENV=production \
    PORT=8080 \
    LOG_LEVEL=info
ENV Variables Are Available During the Build

Because ENV takes effect immediately, subsequent RUN instructions in the same Dockerfile can reference variables it sets, which is useful for configuring build behavior.

ENV PIP_NO_CACHE_DIR=1
RUN pip install -r requirements.txt
Overriding ENV at Run Time

Environment variables set with ENV establish defaults that can be overridden when a container is started, without needing to modify or rebuild the image.

docker run -e NODE_ENV=development myapp:1.0
Inspecting ENV Variables Set in an Image

The environment variables baked into an image's configuration can be inspected directly.

docker inspect myapp:1.0 --format '{{json .Config.Env}}'
Why ENV Matters

ENV is the primary mechanism for giving a container a sensible default configuration directly within the image, while still preserving the flexibility to adjust that configuration per environment without needing to rebuild — a balance that is central to how Docker images remain reusable across different deployment contexts.

Content in this section