Minimize a Docker image
Docker is great to get you started with a solution or to share your setup. A problem you may not be aware of at the beginning is the size of a Docker image. You start with a small base image like Debian, add your stuff and ready. This works perfectly when you only add a “simple” solution, like one that is available via apt-get. When you install a solution from scratch, with downloading code from GitHub, and do this using a Docker image, your image can grow to a significant large size. You will notice this when using a Dockerfile. Each RUN command creates a new layer. You are adding files to a previous layer, and to have a running Docker image, all layers are needed. This is great when you work intensively with Dockerfiles and images, as you can also revert to a previous layer, but when your resulting product is just having a solution installed and runnable, you can end up having an image too large for your needs.
You can see the different layers of an image when you pull it form Docker hub.
In my case, the pulled image had a size of 2.321 GB.
Minimize Docker image
A nice feature of Docker is that each container can also be an image. You can export a container to an image, without its history of layers attached to it. Therefore, this container serves as a minimized Docker image.
docker run –d –t tobiashofmann/openui5-rt:1.28
Container is started using the image tobiashofmann/openui5-rt:1.28 and the container ID is displayed. Next step is to transform the container to an image. To not type in the ID, I`ll get the human readable name of the container. Note: use option –name when starting the container and you can specify your own name
docker ps –l
Here the name of my running container is modest_lalande. To transform the container to an image, to command docker export is used. To create the image, the output is piped to docker import.
docker export modest_lalande | docker import – tobiashofmann/openui5-rt:1.28-min
The final size of the minimized docker image 2.118 GB. Close to 200 MB less. Not much here, given also to the content of the image and already low number of RUN commands in my Dockerfile. But when you have a human readable Dockerfile with several RUN commands, you can achieve a very good size reduction.
devops online training · June 28, 2017 at 04:33
Wow. That is so elegant and logical and clearly explained. Keep it up! I follow up your blog for future post.