Docker Image Optimizing

M Bilal Alpaslan
3 min readJan 22, 2023

--

Docker images likes virtual machine images but don’t consist kernel in self and it’s becomes layers above the default kernel. This is the first optimization for us. Moving virtual machine to Container. But there are a lot of ways to optimize this images.

Actually this article not about only docker, it’s about all OCI images. We can optimize our container image a lot of ways. We can optimize from dockerfile or any build configuration files, from image builders (like buildx), from container runtime. In this article I want to talk about optimizing on dockerfiles.

Smaller Base Images

Image size affect pull and push time and relatively affect build time. Mostly build time is not important like pull. Because when servers are down and system restart we need to pull all necessery images immediately.

We can choose slim, alphine or buster version of base images. Not only pull/push time, it’s for a smaller attack surface we should choose smaller images.

Alpine Linux is a Linux distribution built around musl libc and BusyBox. The image is only 5 MB in size and has access to a package repository that is much more complete than other BusyBox based images. This makes Alpine Linux a great image base for utilities and even production applications. Read more about Alpine Linux here and you can see how their mantra fits in right at home with Docker images.

Many choices available for populer programing languages and tool on Dockerhub, github or any other public container registry.

Multi Stage Build

Some technologies need build tools or sdk kits for build or development section. But we dont need them in last product. Multi stage build is our solution.

First Image has any tools for build and debug, we dont care how much MB or GB size. Second image consist only we need in final product. We transfer application data from builder image to production image.

Especially compilable languages gain a great advantage on multi stage build.

Already alpine image has a very small size like 5mb but we have a more fantastic option. Actually we dont need extra image layer.We can use empty image with only executable application(bytes). This way we can create 1mb docker container with amazing pull and start times.

--

--

M Bilal Alpaslan
M Bilal Alpaslan

No responses yet