Sub topic 2
Anatomy of a Dockerfile
This chapter assumes you can use a terminal, have Docker installed, and know that a container is a running process isolated from the host. It covers how a Dockerfile is laid out and read. Later chapters cover each instruction in depth.
What a Dockerfile is
A Dockerfile is a plain text file, usually named Dockerfile with no extension. Docker reads it from top to bottom. Each instruction changes the image being built, and most instructions add a layer to it. The result is an image you can run as a container.
Here is a complete Dockerfile for a Node.js app:
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]The shape of an instruction
Every line has the form INSTRUCTION arguments. Three rules cover most of the syntax:
- Instruction names are case-insensitive, but the convention is uppercase so they stand out from arguments.
- Lines starting with
#are comments. A#in the middle of a line is not a comment. It is passed to the instruction as part of its arguments. - A backslash at the end of a line continues the instruction on the next line. This is how long
RUNcommands stay readable.
# Install tools in one instruction
RUN apt-get update && \
apt-get install -y --no-install-recommends curl && \
rm -rf /var/lib/apt/lists/*Reading the example line by line
| Instruction | What it does |
|---|---|
| `FROM node:20-slim` | Starts from an existing image that already contains Node.js 20. |
| `WORKDIR /app` | Sets the directory for the instructions that follow. Docker creates it if it does not exist. |
| `COPY package*.json ./` | Copies the dependency manifests from the build context into `/app`. |
| `RUN npm ci --omit=dev` | Runs a command at build time and saves the result as a layer. |
| `COPY . .` | Copies the rest of the source into `/app`. |
| `ENV NODE_ENV=production` | Sets an environment variable in the image and in containers started from it. |
| `EXPOSE 3000` | Records that the app listens on port 3000. It does not publish the port. |
| `USER node` | Runs later instructions and the container process as the `node` user. |
| `CMD ["node", "server.js"]` | Sets the default command when a container starts. |
Notice the split between build-time and run-time instructions. RUN executes while the image is built. CMD is stored as metadata and executes only when a container starts. Mixing these up is a common early error.
Instructions as layers
Instructions that change the filesystem (RUN, COPY, ADD) each create a layer. Instructions like ENV, EXPOSE, USER, and CMD only add metadata. The order of lines therefore decides what Docker can reuse on a rebuild.
Common errors
- Missing
FROM. Apart fromARGand comments,FROMmust come first. Without it, the build fails. - Wrong form for
CMD.CMD ["node", "server.js"]is the exec form, a JSON array that needs double quotes. Single quotes make it invalid JSON, and Docker then treats it as shell form with unexpected results. - Expecting
EXPOSEto open a port. You still need-p 3000:3000withdocker run. - Putting a build step in
CMD. Installing packages there repeats the work on every container start.
Exercises
Exercise
In the example Dockerfile, which instruction runs when you build the image, and which runs when a container starts: RUN npm ci --omit=dev or CMD ["node", "server.js"]?
Show answer
RUN npm ci --omit=dev runs at build time, and its result is stored in a layer. CMD ["node", "server.js"] is only recorded as metadata and runs when a container starts.
Exercise
A teammate writes CMD ['node', 'server.js'] and the container fails to start as expected. What is wrong, and how do you fix it?
Show answer
The exec form must be a JSON array, which requires double quotes. With single quotes, Docker treats the line as shell form. Change it to CMD ["node", "server.js"].