Docker

Optimize Docker Images with Slim (DockerSlim)

Slim (formerly DockerSlim) is an open-source tool that analyzes and optimizes container images. It inspects your Docker image, understands what the application actually needs at runtime, and produces a minimized version that can be up to 30x smaller. Smaller images mean faster pulls, reduced attack surface, and lower storage costs.

Original content from computingforgeeks.com - post 85417

This guide covers installing Slim on Linux and macOS, optimizing Docker images, and understanding the different optimization profiles.

Install Slim

Slim provides a single binary that works on Linux and macOS. Download the latest release:

curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo bash -

Verify the installation:

slim --version

On macOS with Homebrew:

brew install slim

How Slim Works

Slim uses dynamic analysis to understand what your application needs. It runs the container, monitors filesystem access and network activity, then builds a new image containing only the files that were actually used. The process works in four steps:

  1. Analyze – inspects the original image structure and layers
  2. Probe – runs the container and tracks all file and network activity
  3. Build – creates a new minimal image with only the required files
  4. Verify – optionally tests the slim image to confirm it works

Optimize a Docker Image

Start with a basic example using an Nginx image. Pull it first:

docker pull nginx:latest
docker images nginx:latest

Run Slim to create an optimized version:

slim build --target nginx:latest --tag nginx:slim

Compare the sizes:

docker images | grep nginx

You will typically see the optimized image is 5-10x smaller than the original.

Optimize a Node.js application

For application images, Slim needs to exercise the application during the probe phase. Use --http-probe to have Slim make HTTP requests:

slim build --target myapp:latest --tag myapp:slim --http-probe

For applications that need specific endpoints probed:

slim build --target myapp:latest --tag myapp:slim --http-probe-cmd /api/health --http-probe-cmd /api/status

Include extra files

If Slim removes files your app needs at runtime (like config files loaded on demand), include them explicitly:

slim build --target myapp:latest --tag myapp:slim --include-path /app/config --include-path /etc/ssl/certs

Analyze Without Building

To inspect an image without creating a slim version, use the xray command:

slim xray --target nginx:latest

This shows the image layers, exposed ports, environment variables, entrypoint, and file statistics without modifying anything.

Slim Command Reference

CommandPurpose
slim buildAnalyze and create optimized image
slim xrayInspect image without modification
slim lintCheck Dockerfile for best practices
slim profileAnalyze image without building slim version
slim versionShow version information

Conclusion

Slim makes it straightforward to shrink Docker images to a fraction of their original size without rewriting Dockerfiles. The dynamic analysis approach catches dependencies that static tools miss. Integrate it into your CI/CD pipeline to automatically produce optimized images on every build. Refer to the Slim GitHub repository for the full documentation and advanced options.

Related Articles

AlmaLinux Setup Docker Swarm Cluster on Rocky Linux 10 / AlmaLinux 10 Containers How To Install Rancher on Ubuntu 24.04|20.04|18.04 Cloud Deploy Kubernetes Cluster using VMware Photon OS Containers Install Docker and Compose on Linux Mint

Leave a Comment

Press ESC to close