Shrink Docker Images up to 30x with SlimToolkit (and Cut CVEs)

DevOps tutorial - IT technology blog
DevOps tutorial - IT technology blog

Shipping a 900 MB container for a 20 MB web service slows down CI/CD pipelines, bloats registries, and leaves hundreds of unused binaries sitting around for attackers to exploit. I have tried almost every trick to slim down images—from aggressive multi-stage builds to hand-crafted scratch containers. When you want big size drops without rewriting every Dockerfile from scratch, SlimToolkit (formerly DockerSlim) is hard to beat.

Here is how SlimToolkit stacks up against multi-stage builds and Distroless, where dynamic minification breaks, and the exact workflow I use in production.

Multi-Stage Builds vs. Distroless vs. SlimToolkit

Before picking a tool, look at the three main ways teams shrink Docker images:

  • Multi-stage builds with Alpine: You compile your app in a full builder stage, then copy the binary or runtime files into an alpine image. It is a solid default. Still, Alpine’s musl libc can cause subtle headaches with Python C-extension wheels, Node native addons, or DNS resolution edge cases.
  • Distroless base images: Maintained by Google, Distroless images strip out shells and package managers, leaving only your runtime (Java, Python, or Node) and glibc. They reduce risk, yet you still ship the full standard runtime and every module your app never imports.
  • SlimToolkit (automated minification): SlimToolkit takes an existing image—even a stock ubuntu or node:20 image—and runs it in a temporary sandbox. It watches which files, shared libraries, and syscalls your app actually touches, then builds a new image with only those pieces.

You do not have to choose just one. Keep a clean multi-stage Dockerfile for readability, then let SlimToolkit handle the last-mile stripping that cuts image size by up to 30x.

Pros and Cons of SlimToolkit

What It Gets Right

  • Dramatic size drops without Dockerfile gymnastics: A stock node:20 image (~1.1 GB) or python:3.12 image (~1.0 GB) routinely shrinks to 30–45 MB.
  • Smaller attack surface: Shells (bash, sh), package managers (apt, apk), curl, and unused OS libraries are gone. Even if an attacker exploits your app, there are no local tools left to pivot or fetch payloads.
  • Auto-generated security profiles: SlimToolkit writes tailored Seccomp and AppArmor profiles based on the syscalls your container made during probing.
  • Zero code changes: You keep your existing app structure and base images while still shipping tiny artifacts.

Tradeoffs to Plan Around

  • Dynamic analysis only keeps what it sees: If a code path, template, or shared library is never triggered during the probe, SlimToolkit will leave it out of the final image.
  • No shell for live debugging: Running docker exec -it ... sh fails once the shell is stripped. Use ephemeral debug containers like docker debug or kubectl debug instead.
  • Longer CI builds: Spinning up and probing the container adds roughly 15–45 seconds per build.

Production Rules That Prevent Broken Builds

On a typical Node.js API, this workflow cut our image from 860 MB to 34 MB, dropped pod pull times from ~40 seconds to under 3 seconds, and wiped out over 110 OS-level CVEs. To get those results without runtime surprises, follow four rules:

  1. Pair probing with a real smoke test: Do not stop at GET /health. Hit key API routes, database drivers, and serialization paths so SlimToolkit traces every shared library your app loads.
  2. Pin dynamic assets explicitly: If your app loads config files, SQL migrations, or Jinja/EJS templates lazily at runtime, pass them with --include-path so they never get stripped.
  3. Keep the fat image as a debug artifact: Push your pre-slim build (such as myapp:1.2.0-fat) to a private registry tier, and deploy myapp:1.2.0-slim to production.
  4. Scan before and after: Run Trivy on both tags so your team can track the exact drop in CVEs.

Step-by-Step: Shrinking Your First Image

1. Install SlimToolkit

Grab the latest binary on Linux or macOS using the official install script or Homebrew:

# Install on Linux / macOS
curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo -E bash -

# Or using Homebrew on macOS
brew install docker-slim

# Verify installation
slim --version

2. Inspect the Fat Image with Xray

Before minifying, run slim xray to see where the bloat actually sits—oversized layers, duplicate files, or leftover package caches:

slim xray --target my-node-app:latest

xray is read-only and safe to run anywhere. Check the largest files in the report and look for /var/cache or build toolchains hiding in your final layers.

3. Minify with HTTP or Command Probing

Say you have a Node.js or Python service listening on port 3000. Run slim build with HTTP probing enabled and list any folders loaded dynamically at runtime:

slim build \
  --target my-node-app:latest \
  --tag my-node-app:slim \
  --http-probe=true \
  --http-probe-cmd "GET /health" \
  --http-probe-cmd "GET /api/v1/items" \
  --include-path /app/views \
  --include-path /app/certs

Background workers and CLI tools do not expose an HTTP port. For those, turn off HTTP probing and pass a short --exec command so SlimToolkit can trace their dependencies:

slim build \
  --target my-python-worker:latest \
  --tag my-python-worker:slim \
  --http-probe=false \
  --exec "python -m worker --dry-run"

4. Verify Size, Behavior, and CVEs

Once the build finishes, check the size difference, boot the slimmed container locally, and compare Trivy reports:

# Compare image sizes
docker images | grep my-node-app

# Test the slimmed container
docker run --rm -p 3000:3000 my-node-app:slim

# Compare vulnerabilities with Trivy
trivy image my-node-app:latest
trivy image my-node-app:slim

Most web services drop from 700–1,000 MB down to 25–50 MB. OS-level CVEs from packages like openssl, perl, and apt typically fall from triple digits to single digits or zero.

5. Practical CI/CD Tips

  • Pin the SlimToolkit version: Avoid pulling latest in CI runners so builds stay reproducible across releases.
  • Give slow-booting apps extra time: JVM or heavy Python services often need more than a few seconds to start. Pass --continue-after 15 (15 seconds) or pair probing with a custom --exec health script so SlimToolkit waits long enough.
  • Use the generated Seccomp profile: SlimToolkit saves a *-seccomp.json file in its state directory. Attaching it in Docker or Kubernetes locks down syscalls with almost no extra work.

Pick one non-critical service to start, verify your probes cover every runtime path, and soak-test the slim image in staging first. Once your team sees faster pod autoscaling and near-empty Trivy reports, shipping lean containers sticks.

Share: