Build-time variable

2 snippets in Docker

DKDocker

ENV - Set Environment Variables

DK · Dockerfile
Syntax
ENV <key>=<value> [<key>=<value>...]
Example
ENV NODE_ENV=production
ENV APP_PORT=3000 LOG_LEVEL=info

Note ENV values persist into the running container and into child images. If you only need a variable during build time (e.g., a version number), use ARG instead to avoid leaking it into the final image.

ARG - Build-Time Variables

DK · Dockerfile
Syntax
ARG <name>[=<default_value>]
Example
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-alpine

ARG BUILD_DATE
LABEL build-date=$BUILD_DATE

Note ARG values are only available during build (not at runtime). Pass them with docker build --build-arg NODE_VERSION=22. ARG before FROM is only in scope for the FROM line itself - redeclare it after FROM if needed later.

Frequently asked questions

How does Docker handle build-time variable?
Docker covers this with 2 copy-ready snippets on this page. The "ENV - Set Environment Variables" snippet in Docker uses `ENV <key>=<value> [<key>=<value>...]`.
Which command does the Docker example use?
The "ENV - Set Environment Variables" snippet uses `ENV <key>=<value> [<key>=<value>...]`, from the Dockerfile section of the Docker cheat sheet.
What other Docker snippets are shown for "build-time variable"?
Besides "ENV - Set Environment Variables", this page also shows "ARG - Build-Time Variables".
Is there anything to watch out for?
Yes. For "ENV - Set Environment Variables": ENV values persist into the running container and into child images. If you only need a variable during build time (e.g., a version number), use ARG instead to avoid leaking it into the final image.