Sub topic 6
Configuration with ENV and ARG
This chapter assumes you can write a basic Dockerfile with FROM, WORKDIR, COPY, and RUN, and that you can run docker build and docker run. It covers the two instructions that carry configuration: ARG for build time and ENV for build time and run time.
Two kinds of variable
ENV sets an environment variable in the image. Every later instruction sees it, and it is also present in every container started from the image. A container user can override it with docker run -e.
ARG defines a build argument. It exists only while the image is being built. You set it with docker build --build-arg, and it is not present in the running container's environment.
| Feature | `ENV` | `ARG` |
|---|---|---|
| Available during build | Yes, after it is declared | Yes, after it is declared |
| Available in running container | Yes | No |
| Set from the command line | `docker run -e NAME=value` | `docker build --build-arg NAME=value` |
| Stored in image metadata | Yes | Yes, in the build history |
Using ENV
The Node.js Dockerfile from this tutorial sets a single variable:
ENV NODE_ENV=productionMany tools read NODE_ENV and change behavior when it is production. Because it is an ENV, the running server sees it too. Use ENV for defaults the application reads at run time, such as a port or a mode flag.
You can set several variables in one instruction. Each one becomes part of the same layer:
ENV APP_HOME=/app \
PORT=3000Later instructions can reference earlier variables with $NAME or ${NAME}:
FROM node:20-slim
ENV APP_HOME=/app
WORKDIR ${APP_HOME}Using ARG
Declare an ARG with an optional default. Pass a different value at build time to change it:
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]docker build -t myapp:node22 --build-arg NODE_VERSION=22 .An ARG declared before FROM can be used only in the FROM line itself. After FROM, the variable goes out of scope. To use it in later instructions, declare it again without a value:
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim
ARG NODE_VERSION
RUN echo "Building with Node ${NODE_VERSION}"Combining them
A common pattern is to accept a build argument and then promote it to a run-time variable:
FROM node:20-slim
ARG APP_VERSION=dev
ENV APP_VERSION=${APP_VERSION}The build argument supplies the value. The ENV copies it into the image so the app can read APP_VERSION when it runs.
Common errors
- Expecting an
ARGat run time. The variable is empty in the container. Copy it into anENVif the app needs it. - Putting secrets in either one. Both
ARGvalues andENVvalues can be read from the image.docker historyshows build arguments used inRUNcommands, anddocker inspectshowsENVvalues. Never pass passwords or tokens this way. The BuildKit secret mounts covered later in this tutorial are the right tool. - Using an
ARGafterFROMwithout redeclaring it. It expands to an empty string, with no error. - Changing an
ARGand being surprised by a rebuild. A different value invalidates the cache for the first instruction that uses it, and for everything after it.
Exercise
A Dockerfile contains ARG APP_VERSION=1.0 and no ENV instruction. After building and running the container, the app reads APP_VERSION from its environment and finds nothing. What is the cause, and what is the smallest change that fixes it?
Show answer
ARG values exist only during the build and are not part of the container's run-time environment. Add ENV APP_VERSION=${APP_VERSION} after the ARG line so the value is stored in the image and visible to the running app.
Exercise
You write ARG BASE=20 before FROM node:${BASE}-slim, then use ${BASE} in a later RUN instruction and get an empty value. Why, and how do you fix it?
Show answer
An ARG declared before FROM is out of scope after FROM. Redeclare it with ARG BASE after the FROM line, and the default from the first declaration is used.