Sub topic 1
From Dockerfile to image: what the CI will build
Prerequisites
This chapter assumes you can run containers with docker run and have written a simple Dockerfile. It also assumes you know Git branches and tags, can read YAML, and know what a CI pipeline is. Later chapters use GitHub Actions and GitHub Container Registry, but nothing here depends on them.
What the CI actually runs
A CI job does not build images in a special way. It checks out your repository on a clean machine and runs the same command you run locally, docker build. If the build works on your laptop, it works in CI as long as the repository holds everything the build needs. The rest of this tutorial adds automation around that one step.
A Dockerfile for a Node.js service
This Dockerfile is the starting point for the chapters that follow.
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
ENV NODE_ENV=production
USER node
CMD ["node", "dist/server.js"]Each instruction has a distinct job:
| Instruction | What it does |
|---|---|
| `FROM` | Chooses the base image everything else builds on. |
| `WORKDIR` | Sets the directory for the instructions that follow. |
| `COPY` | Copies files from the build context into the image. |
| `RUN` | Runs a command at build time and saves the result. |
| `ENV` | Sets an environment variable that persists in the image. |
| `USER` | Sets the user that the container runs as. |
| `CMD` | Sets the default command used when a container starts. |
RUN executes while the image is built. CMD executes only when someone starts a container from the image. Mixing these up is a common source of confusion, such as expecting a server to start during the build.
Building and checking the image
From the repository root, build and tag the image:
docker build -t example-app:dev .
docker image ls example-appThe final . is the build context, the set of files Docker may copy from. The Dockerfile is read from the context by default. A CI job passes the same arguments, usually through an action rather than by hand.
Why the order of instructions matters
The Dockerfile copies package.json and package-lock.json before the rest of the source. Docker saves a layer for each instruction and reuses it when the inputs are unchanged. A source edit changes only the later COPY . . layer, so the npm ci layer can be reused. If you copied everything first, any edit would reinstall all dependencies. Chapter 3 covers this in detail.
Common errors
- Unpinned base images.
node:latestcan change between two CI runs. Use a specific tag such asnode:20-alpine. - Shell form for
CMD.CMD node dist/server.jsruns through a shell, which can stop the app from receiving stop signals. Use the JSON array form shown above. - Missing files in CI. A file that exists only on your machine, such as a generated config, fails the build on a clean checkout.
- Running as root. Without
USER, the process runs as root inside the container.
Exercise
A teammate edits one source file and the CI build reruns npm ci every time. The Dockerfile starts with COPY . . followed by RUN npm ci. What change fixes this, and why?
Show answer
Copy only package.json and package-lock.json, run RUN npm ci, and then run COPY . .. Docker reuses a layer only when its inputs are unchanged. With the manifests copied first, a source edit leaves the install layer's inputs the same, so the cached npm ci layer is reused.