Minimize a Docker image

Published by Tobias Hofmann on

2 min read

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.

Start container

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.

Let the world know
Categories: Technology

Tobias Hofmann

Doing stuff with SAP since 1998. Open, web, UX, cloud. I am not a Basis guy, but very knowledgeable about Basis stuff, as it's the foundation of everything I do (DevOps). Performance is king, and unit tests is something I actually do. Developing HTML5 apps when HTML5 wasn't around. HCP/SCP user since 2012, NetWeaver since 2002, ABAP since 1998.

1 Comment

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.