Skip to content
Blog

Image Slimming Workflows: Measure, Cut, and Rebuild

A systematic methodology for shrinking container image size: measuring layer bloat, choosing minimal base images, cutting build tools, and multi-stage rebuilding.

Published on • November 18, 2026

AI Assistant

Smaller container images build faster, pull faster across networks, reduce cloud storage costs, and significantly minimize attack surface by eliminating unnecessary system utilities and unpatched libraries.

However, randomly deleting files or choosing non-functional base images can break application runtimes. Image slimming should follow a structured 3-phase engineering methodology: Measure, Cut, and Rebuild.

In this article, we will examine this 3-phase workflow and walk through shrinking a heavy 1.2 GB application image down to under 30 MB.

The 3-Phase Image Slimming Framework

+-----------------------------------------------------------------------------------+
| 1. MEASURE                                                                        |
|    - Audit total image size (`docker image ls`).                                  |
|    - Analyze layer-by-layer size breakdown (`docker history`, `dive`).            |
|    - Inspect embedded packages (`syft`, `trivy`).                                 |
+-----------------------------------------------------------------------------------+
                                         │
                                         v
+-----------------------------------------------------------------------------------+
| 2. CUT                                                                            |
|    - Swap heavy base OS (`ubuntu`, `debian`) for minimal bases (`alpine`, `slim`). |
|    - Eliminate package caches (`apt-get clean`, `pip --no-cache-dir`).            |
|    - Exclude non-production assets via `.dockerignore`.                           |
+-----------------------------------------------------------------------------------+
                                         │
                                         v
+-----------------------------------------------------------------------------------+
| 3. REBUILD                                                                        |
|    - Implement Multi-Stage Builds (separate build environment from runtime).      |
|    - Copy ONLY compiled production artifacts into final `scratch` or `distroless`.|
|    - Verify build functionality and scan for security CVEs.                       |
+-----------------------------------------------------------------------------------+

Phase 1: Measure

Before editing code, measure your baseline.

# 1. Check total image size
docker image ls myapp:naive

# 2. Inspect layer breakdown to locate bloated steps
docker history --no-trunc myapp:naive

Common culprits discovered during measurement:

  • Unused compilers (gcc, g++, make) left in production images.
  • Un-cleared package manager index caches (/var/lib/apt/lists/* or ~/.cache/pip).
  • Development dependencies (devDependencies in package.json, test runners, documentation).
  • Large local context transfers (un-ignored .git folders, node_modules, build artifacts).

Phase 2: Cut

Apply immediate quick-win optimizations:

1. Add a Comprehensive .dockerignore

Prevent local test files, documentation, and source control repositories from being sent to the Docker build daemon:

# .dockerignore
.git
.gitignore
node_modules
dist
coverage
*.md
Dockerfile

2. Clean Package Caches in the Same RUN Layer

Always delete temporary package installer archives within the exact same RUN command layer:

# GOOD: Package cache deleted in the same layer before layer freeze
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

Phase 3: Rebuild with Multi-Stage Pipelines

The single most effective technique for image slimming is the Multi-Stage Build. Separate your build toolchain from your production execution environment.

Case Study: Slimming a Go Web Application

Naive Single-Stage Dockerfile (Size: ~850 MB):

FROM golang:1.21

WORKDIR /app
COPY . .
RUN go build -o server .

CMD ["/app/server"]

Result: Includes full Go compiler, toolchain, C libraries, and source code (850 MB).

Optimized Multi-Stage Dockerfile (Size: ~15 MB):

# Stage 1: Build environment
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Build static CGO-disabled binary
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o server .

# Stage 2: Minimal runtime environment
FROM alpine:3.18
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
COPY --from=builder /app/server .

USER nobody:nobody
EXPOSE 8080
CMD ["./server"]

Result: 15 MB total size!

-ldflags="-w -s" strips DWARF debugging symbols and symbol tables, saving an additional 20-30% binary size.

Base Image Comparison Cheat Sheet

Base ImageSizeStandard Use CaseTrade-offs
ubuntu:22.04~78 MBLegacy apps, complex dependenciesHeavy; includes unnecessary tools
debian:bookworm-slim~30 MBGeneral Python/Node/Ruby appsGood balance of glibc compatibility & size
alpine:3.18~5 MBLightweight microservicesUses musl libc (may require C re-compilation)
gcr.io/distroless~20 MBHigh-security production runtimeNo shell/package manager (harder to exec debug)
scratch0 MBStatic Go / Rust binariesEmpty base; requires fully static binaries

Summary

Following the Measure, Cut, and Rebuild methodology allows engineering teams to systematically reduce container image sizes by 80% to 98%, improving deployment speeds and security posture.