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.
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:
- Analyze – inspects the original image structure and layers
- Probe – runs the container and tracks all file and network activity
- Build – creates a new minimal image with only the required files
- 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
| Command | Purpose |
|---|---|
slim build | Analyze and create optimized image |
slim xray | Inspect image without modification |
slim lint | Check Dockerfile for best practices |
slim profile | Analyze image without building slim version |
slim version | Show 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.