logo
Parslinks
Back to blog
Guides4 min read

Multi-stage Dockerfiles for Node.js: from 1.2 GB to 150 MB

Smaller images mean faster deploys, faster rollbacks and less to attack. Most of the win takes a few lines.

Alireza Tadi

Co-founder & Head of Development and Communications, Parslinks

Run docker images on a typical Node.js project and the number is sobering. Ours, for an internal invoicing API, was 1.2 GB. Inside: TypeScript, native build toolchains, the npm cache, test fixtures and, somehow, the whole .git directory. None of it is needed to run node dist/server.js.

Oversized images cost you three ways. They're slow to push and pull, which slows every deploy and every rollback. And every tool left inside is something an attacker could use.

The idea: build in one place, run in another

A multi-stage Dockerfile has several FROM lines. Early stages install everything and build the app. The final stage starts from a clean base and copies in only the build output and runtime dependencies. Whatever you don't copy doesn't exist in the final image.

Dockerfile
# ---- stage 1: dependencies ----
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# ---- stage 2: build ----
FROM node:22-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build && npm prune --omit=dev

# ---- stage 3: runtime ----
FROM node:22-alpine AS run
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup -S app && adduser -S app -G app
COPY --from=build --chown=app:app /app/node_modules ./node_modules
COPY --from=build --chown=app:app /app/dist ./dist
COPY --from=build --chown=app:app /app/package.json ./
USER app
EXPOSE 3000
CMD ["node", "dist/server.js"]
dockerfile24 lines

Why each line is there

Manifests before source. Docker caches each layer until its inputs change. Because only the lockfile is copied before npm ci, dependencies reinstall only when they actually change. Editing a route handler rebuilds just the later stages.

npm prune --omit=dev. Once the build is done, TypeScript, linters and test runners are dead weight. This removes them.

A non-root user. If someone exploits a bug in your app, a limited user can do far less inside the container.

.dockerignore. Don't skip it. Without one, your local node_modules, .git and .env files all get sent into the build context.

.dockerignore
node_modules
.git
.env*
coverage
*.log
Dockerfile
.dockerignore
text7 lines
Single-stage vs. multi-stage image

The result

The invoicing API went from 1.2 GB to about 150 MB. Deploys dropped from around seven minutes to under two, and since the dependency layer is usually cached, most builds finish in under a minute.

If your platform builds images straight from source, you may not need a Dockerfile at all; we cover when to write one in Do you really need a Dockerfile?. But if you do write one, make it multi-stage.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.