Skip to content
Tech News
← Back to articles

We reduced a real Node.js production Docker image from 1.2GB to 78MB

read original get Node.js Docker Slimming Guide → more articles
Why This Matters

This article demonstrates how optimizing a Node.js Docker image can drastically reduce its size from 1.2GB to just 78MB, leading to faster deployments, reduced storage costs, and a smaller attack surface. The step-by-step approach provides practical techniques that can be applied by developers to improve efficiency and security in containerized applications.

Key Takeaways

A step-by-step optimization of a real Node.js Docker image, from a 1.2GB monster to a 78MB production container. Each technique is benchmarked, copy-paste ready, and explained with the trade-offs.

Bloated Docker images are the silent tax on every team that ships containers. Slow CI. Slow deploys. Bigger attack surface. Bigger registry bill. And almost always, it’s fixable in an afternoon.

I took a real Node.js + TypeScript service we ship to production, started from the naive Dockerfile most teams write, and walked it down from 1.2GB to 78MB. Same app, same behavior, six steps, all measured on the same machine. Here is exactly what moved the needle.

The starting point: 1.2GB

This is the Dockerfile most teams begin with. It works. It is also wasteful in almost every line.

FROM node:22 WORKDIR /app COPY . . RUN npm install RUN npm run build EXPOSE 3000 CMD [ "npm" , "start" ]

Build it and check the size:

$ docker build -t app:naive . $ docker images app:naive REPOSITORY TAG SIZE app naive 1.21GB

1.21GB to ship a service that produces about 4MB of compiled JavaScript. Let’s fix it.

Step 1: Switch the base image, 1.21GB to 412MB

... continue reading