How to shrink Docker images by 90%by Christoph Dähne on 22.06.2022
Recently I stumbled across a tool to reduce the size of docker images: docker-slim. Today I gave it a try and want to share my experience.
Nodejs and Hello World
The following Dockerfile starts a container listening on port 5050.
FROM node:latest EXPOSE 5050 CMD node --eval ' \ require("http") \ .createServer( \ (req,res) => res.end("Hello World!")) \ .listen(5050); \ console.log("listing on port 5050"); \ '
Let’s run it and test it.
# build the image docker build -t test . # start a container docker run --rm -p 5050:5050 test # GET http://localhost:5050 http :5050 # > Hello World! # shutdown container docker stop … # inspect local docker images docker image list # > test … 998MB
The Hello-World container works as expected but requires 998MB of disk space. Let’s see how much docker-slim can save us.
Optimize image with docker-slim
# download docker-slim brew install docker-slim # optimize hello world image docker-slim build --target test # start a container docker run --rm -p 5050:5050 test.slim # GET http://localhost:5050 http :5050 # > Hello World! # shutdown container docker stop … # inspect local docker images docker image list # > test.slim … 92.2MB
This seems nice: the image size is reduced by over 900MB to 92.2MB. This not only saves disk space but network as well, since we tend to ship our Docker images somewhere.
I hope you find this blog post helpful. If you have any feedback, feel free to contact us.