Today I found a amazing tool to optimise and minifies docker image and reduces the docker image size.
docker-slim
docker-slim doesn't change anything in your Docker container image and minify it by up to 30x. docker-slim will optimize and secure your containers by understanding your application and what it needs using various analysis techniques. It will throw away what you don't need, reducing the attack surface of your container.
docker slim on github --> github.com/docker-slim/docker-slim
how to install?
If you are on Mac, install via brew or bash and here is the official installation instructions for various OS --> github.com/docker-slim/docker-slim#installa..
brew install docker-slim
how to use?
# Pull fastapi docker image from docker hub
$ docker pull tiangolo/uvicorn-gunicorn-fastapi:python3.9
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tiangolo/uvicorn-gunicorn-fastapi python3.9 ae7af924b174 2 weeks ago 991MB
# Run the fast api
$ docker run -p 80:80 tiangolo/uvicorn-gunicorn-fastapi:python3.9
# Check if app is running
$ curl http://localhost
{"message":"Hello world! From FastAPI running on Uvicorn with Gunicorn. Using Python 3.9"}
# run docker-slim with docker image name that needs to be minified and output docker image name
$ docker-slim build --target tiangolo/uvicorn-gunicorn-fastapi:python3.9 --tag tiangolo/uvicorn-gunicorn-fastapi:docker-slim
# Run the fast api
$ docker run -p 80:80 tiangolo/uvicorn-gunicorn-fastapi:docker-slim
# Check if app is running
$ curl http://localhost
{"message":"Hello world! From FastAPI running on Uvicorn with Gunicorn. Using Python 3.9"}
# Check the image size between original and minified
# Original size 991MB
# Minified size 102MB
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tiangolo/uvicorn-gunicorn-fastapi docker-slim b2747e1f1fad 7 seconds ago 102MB
tiangolo/uvicorn-gunicorn-fastapi python3.9 ae7af924b174 2 weeks ago 991MB