Skip to content
4 min

Same Express app. 1.88 GB, then 244 MB.

One small app built seven ways. The base image, a missing .dockerignore and dev tools in production, each measured. 158 MB of the final image is Node itself.

  • Docker
  • Node.js
  • CI/CD
Seven horizontal bars drawn to scale, from node:22 without a .dockerignore at 1.88 GB down to alpine multi-stage at 244 MB, with a handwritten note that the app itself is 16.4 kB.

One small Express and TypeScript app. One route. The app's own compiled code is 16.4 kB.

I built it seven ways, same code every time, and wrote down what Docker reported (arm64, uncompressed):

BuildSize
node:22, no .dockerignore1.88 GB
node:221.70 GB
node:22-slim, no .dockerignore607 MB
node:22-alpine, no .dockerignore496 MB
node:22-slim426 MB
node:22-alpine315 MB
alpine, multi-stage244 MB

Nothing clever in the last one. Three boring changes, and each one shows up in a different place.

Find the big layer first

docker history <image> --format 'table {{.Size}}\t{{.CreatedBy}}'

Whatever's biggest in that list is what you fix. Guessing is how people end up adding rm -rf lines that do nothing. Deleting files in a later RUN doesn't shrink the image. The earlier layer still has them.

1. The base image

node:22 ships a full Debian with compilers. The same app went 1.70 GB on node:22, 426 MB on slim, 315 MB on alpine. If a native module won't build on alpine, use slim.

2. No .dockerignore

This was the embarrassing one. Without a .dockerignore, the build context was this:

transferring context: 103.28MB 1.8s

My laptop's node_modules and .git went straight into the image. It added about 181 MB to every variant. With six lines of .dockerignore:

transferring context: 75.68kB
node_modules
.git
dist
*.log
.env
Dockerfile*

.env is in there for a second reason. Without it, your secrets get baked into an image that anyone who can pull it can read.

3. Dev tools in production

TypeScript, ESLint and all the type packages are only for building. In the single-stage alpine image:

$ docker run --rm dbf/size:node-22-alpine sh -c 'du -sh node_modules; ls node_modules | wc -l'
45.0M   node_modules
141

Multi-stage keeps them in the build stage:

# stage 1: build with everything
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY tsconfig.json ./
COPY src ./src
RUN npm run build

# stage 2: only what runs
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && \
    npm cache clean --force
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]
$ docker run --rm dbf/size:multistage sh -c 'du -sh node_modules; ls node_modules | wc -l; id'
4.8M    node_modules
71
uid=1000(node) gid=1000(node) groups=1000(node),1000(node)

And with USER node it doesn't run as root.

Where the 244 MB actually is

SIZE      CREATED BY (shortened)
16.4kB    COPY /app/dist ./dist              <- the app
7.77MB    RUN npm ci --omit=dev, cache clean <- production deps
90.1kB    COPY package.json package-lock.json
5.49MB    RUN apk add (from the node image)
158MB     RUN addgroup, install node         <- node itself
9.31MB    ADD alpine-minirootfs-3.24.4-aarch64.tar.gz

158 MB of that is Node itself. Everything I added is under 8 MB.

Bonus: the order of COPY

Same app. COPY . . above npm ci meant every code change reinstalled everything, 6.03 seconds a rebuild. Copying package*.json first, installing, then copying the code, brought it to 1.71 seconds, multi-stage included. A small app, so seconds. On a big one it's minutes, on every push.

In CI

Print the image size in your pipeline and fail the build above a number you choose.

The image size chart, plus containers that exit, ports that answer nothing, volume permissions and a full disk, are in the Docker Break-Fix Field Guide. Want to see the format first? The Kubernetes sampler is free.

Useful to someone on your team?