Clouds
Containers·4 min read

Docker Images: From 1.2GB to Under 100MB Without Losing Sanity

Multi-stage builds, distroless bases, and layer hygiene. A practical guide to shrinking Docker images the right way.


A 1.2GB image takes forever to push, pulls slowly on every node, and doubles your storage bill. Here is the exact recipe I use to shrink images, and what to do only after measuring.

Start with multi-stage builds

Never ship your builder toolchain. Build in one stage, copy artifacts into a clean one.

dockerfile
# Stage 1: build
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json .
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: run
FROM node:22-alpine
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json .
RUN npm ci --omit=dev
EXPOSE 3000
CMD ["node", "dist/index.js"]

One FROM per stage, only what the runtime needs in the final stage.

Choose your base image deliberately

  • Alpine (node:22-alpine): tiny, but musl libc differences occasionally bite (native modules, DNS quirks). Fine for pure-JS apps.
  • Distroless (gcr.io/distroless/static): no shell, no package manager, fewer attack surfaces, but debugging inside is harder.
  • Debian slim (node:22-slim): glibc, familiar tooling, slightly larger. Use when native builds matter.

A rule of thumb: default to slim, not alpine for anything with native dependencies, and consider distroless for hardened, immutable services.

Layer hygiene is the real win

Each RUN, COPY, and ADD creates a layer. Reorder so the slowest-changing instructions come first: dependency files before source code. That way a one-line source change reuses all the cached dependency layers.

Never run apt-get upgrade or caches in the final image:

dockerfile
RUN apt-get update && apt-get install -y git \
    && rm -rf /var/lib/apt/lists/*

Ignore what does not belong in the build context

Every file in your working directory is sent to the Docker daemon before the first instruction runs. A repo with node_modules, .git, and test fixtures ships tens of megabytes to the daemon on every build, and none of it ends up in the image: you paid for the transfer anyway.

dockerfile
.git
node_modules
coverage
*.log
.env
.env.*
dist
tmp

Two things happen. Build context stops being the bottleneck, and a stray .env file stops being one COPY . . away from production.

Cache the expensive steps instead of rerunning them

Package installation is usually the slowest layer and it changes far less often than your source does. BuildKit can hold the package manager's cache outside the image entirely:

dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json .
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN --mount=type=cache,target=/root/.npm npm run build

The cache lives on the build host, never becomes a layer, and survives across builds and branches. The image stays small while the build gets fast, which is the combination people usually assume is a trade-off.

The same trick applies to pip's cache directory, Go's module cache, and Rust's target directory. Anything with a download step benefits.

The mistakes that quietly undo the savings

A one-line change in a Dockerfile can add six hundred megabytes back. The three that show up most:

Copying the whole working directory when the runtime only needs dist/. Installing dev dependencies in the final stage because npm ci was copied verbatim from the builder stage. Running apt-get upgrade "just to be safe", which both bloats the image and breaks reproducibility, since what you install today is not what you installed last month.

There is also the ordering problem. If COPY . . sits above RUN npm ci, every source edit invalidates the dependency layer and your cache stops working entirely. Dependencies first, source last.

Measure, then trim

Don't guess. These tell you what is actually big:

bash
docker history --no-trunc $IMAGE | head -20
docker image inspect $IMAGE | jq '.[0].RootFS.Layers' | head

Keep planned savings tables: "base swap −80MB, multi-stage −600MB, omit dev deps −120MB." If a trim doesn't move the number, drop it.

The wins in order

  1. Multi-stage build (usually the biggest, often −70%)
  2. Right base image
  3. --omit=dev / --production dependency install
  4. Meaningful .dockerignore (keeps build context small and secure)
  5. Only copy artifacts you run

Summary

Compress the image, not the code. Measure the diff, ship the artifact, pull it in seconds. If your image is over 300MB and you shipped from a builder stage, you know where to start.

#docker#containers#images#optimization

Divyesh Parmar

Founder & editor, SDP Clouds

Writes SDP Clouds: hands-on DevOps, Kubernetes, CI/CD, and cloud engineering. Every article comes from work actually done in a terminal, not from rewriting documentation.

More about the author →

Related articles