Dockerfiles & Images — DevOps

A Dockerfile is the recipe for an image (a package holding the app and everything it needs), with one instruction per line. Every instruction that changes…

You change one line in app.js, build the image again, and wait. Seventeen seconds later it's done, and nearly all of that went to npm ci, even though not a single dependency changed. The cause is the line order of your image's recipe.

That recipe is called a Dockerfile: a text file with one instruction per line, which docker build runs from the first line to the last. Each instruction that changes files, such as COPY and RUN, produces one layer (a part) in the image. Picture a cook who keeps the result of every recipe step in the fridge. The next day they cook the same recipe. As long as a step is exactly the same, they take its result from the fridge. At the first step that changed, they cook again from that step to the end. In Docker, the cook is docker build, each recipe step is one instruction, and the saved result is its layer. The fridge is Docker's build cache.

A Dockerfile for notes-api

This is the recipe for the notes-api image we ran in the previous lesson. Save it in the project folder as Dockerfile (no extension):

FROM node:24-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Another instruction you'll see a lot: ENV NODE_ENV=production sets an environment variable for the app.

Build: from a Dockerfile to an image

docker build -t notes-api:1.1 .

-t notes-api:1.1 gives the image a name (notes-api) and a tag (a version label, here 1.1). The dot at the end is the build context: the current folder is sent to Docker, and only files inside it can be COPY'd. The first build runs every step. This is an excerpt of its output:

 => [1/5] FROM docker.io/library/node:24-alpine@sha256:116b800c3754b440ee0a 0.0s
 => [2/5] WORKDIR /app                                                      0.1s
 => [3/5] COPY package*.json ./                                             0.0s
 => [4/5] RUN npm ci --omit=dev                                            14.8s
 => [5/5] COPY . .                                                          0.1s

[4/5] means the fourth step of five. The FROM line is cut to the terminal's width: in full, the code after sha256: is much longer. That code marks the exact version of the base image (covered in the Container Registry lesson). EXPOSE and CMD aren't counted, because they only record settings and don't make files.

Line order decides the cache

Docker keeps the result of every step. For COPY, the cache is only used when the copied files are exactly the same. Once one step runs again, every step below it runs again too. Compare these two orders, after you change only app.js. CACHED means a saved result is used again:

Step Slow: COPY . . before npm ci Fast: COPY . . after npm ci
COPY package*.json ./ CACHED CACHED
COPY . . runs (app.js changed) (it's further down)
RUN npm ci --omit=dev runs again, 14.8 seconds CACHED
COPY . . (already above) runs, 0.1 seconds
Whole build about 17 seconds about 1.5 seconds

So in the slow order, npm ci runs again even though package.json didn't change, because the step above it (COPY . .) changed. That is where nearly 15 seconds go every time you edit app.js. In the fast order, the build output looks like this:

 => CACHED [2/5] WORKDIR /app                                               0.0s
 => CACHED [3/5] COPY package*.json ./                                      0.0s
 => CACHED [4/5] RUN npm ci --omit=dev                                      0.0s
 => [5/5] COPY . .                                                          0.1s

The rule: what rarely changes (the base image, the dependency list) goes at the top, what changes often (the code) goes at the bottom. When you add a dependency, package.json changes, and npm ci really does have to run again.

.dockerignore

COPY . . copies everything in the build context, including files that must not go into the image. So create a .dockerignore file next to the Dockerfile. It lists the files and folders that are not sent to Docker:

node_modules
.git
.env
*.log

Fuller image hardening (a non-root user, secrets at build time) is in the Docker Security Basics lesson of the Security track.

💡 Multi-stage builds: smaller images

If your app has a build step (say, TypeScript compiled into dist/), the build tools are only needed while building. A multi-stage build uses two FROMs: the first stage builds the app, the second stage only takes the result.

FROM node:24-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:24-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/app.js"]

AS builder gives the first stage the name builder. COPY --from=builder copies from that stage, not from the laptop. From the builder stage, only dist/ is brought into the final image. So the final image holds dist/ and the production dependencies, without the build tools. Example sizes, measured when the image was still on Node 20: one stage on node:20 is about 1.35 GB, multi-stage on node:20-alpine is about 182 MB. The Node 24 base images are slightly bigger.

⚠️ Dockerfile lines people often get wrong

TL;DR: A Dockerfile is the recipe for an image (a package holding the app and everything it needs), with one instruction per line. Every instruction that changes files becomes a layer (a part of the image). FROM picks the base image (the starting image), WORKDIR the working folder, and COPY copies files from the build context (the folder sent to Docker). RUN runs a command at build time, EXPOSE records a port, and CMD ["node", "app.js"] is the command when a container (a running image) starts. Build with docker build -t notes-api:1.1 .. Docker keeps the result of every step (the cache). Once a step changes, every step below it runs again. So the order is: COPY package*.json ./, RUN npm ci, then COPY . .. .dockerignore keeps node_modules and .env out of the image, and a multi-stage build (two FROMs) leaves the build tools out of the final image.

⚡ An image's recipe, one instruction per line

Write:

FROM node:24-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Meaning: A Dockerfile is the recipe for an image (a package holding the app and everything it needs). FROM picks the base image, the starting image: here, Alpine Linux with Node 24. WORKDIR picks the working folder inside the image. COPY copies files from the laptop into the image. RUN runs once at build time (when the image is made). EXPOSE records the app's port, and CMD runs every time a container (a running image) starts. Every instruction that changes files, such as COPY and RUN, becomes one layer (a part) of the image.

⚡ Build it into an image

Write:

docker build -t notes-api:1.1 .

Meaning: -t gives a name and a tag (a version label, here 1.1). The dot at the end is the build context: the current folder, which is sent to Docker, and only files inside it can be COPY'd.

⚡ Line order decides the cache

Write:

 => CACHED [4/5] RUN npm ci --omit=dev     0.0s
 => [5/5] COPY . .                         0.1s
Guess first: Docker keeps the result of every step (the cache), and CACHED means an old result is used again. If COPY . . sits before npm ci, and you only change app.js, what runs again?

npm ci runs again too, about 15 seconds, even though package.json didn't change. Once one step runs again, every step below it runs again too.

Meaning: What rarely changes (the dependency list in package*.json) goes at the top, the code at the bottom. In this order, a build after editing app.js takes only about 1.5 seconds.

⚡ .dockerignore

Write:

node_modules
.git
.env
*.log

Meaning: .dockerignore lists the files that are not sent to Docker at build time. The laptop's node_modules doesn't overwrite what npm ci built in the image, and .env with its secrets stays out of the image.

What you will learn