Docker Image Size - How to Keep It Small?

February 19, 2020

Introduction

Docker images are essential components used for building Docker containers. Although closely related, there are major difference between Docker images and containers.

A docker image is the base of a container. These images are created by writing Dockerfiles, lists of instructions automatically executed for creating a specific Docker image.

When building a Docker image, you want to make sure to keep it light. Avoiding large images speeds up building and deploying containers. Therefore, it is crucial to reduce the image size to a minimum.

Read on to learn how to keep your Docker images small.

How to reduce Docker image size

Use a Smaller Image Base (Alpine)

To create a Docker image, you need a base on which you can install and add components, as needed. You can download an existing parent image and use it as the base of your own image or build one from scratch.

You install a variation of an operating system as the base of an image. The OS base can drastically impact the size of your final Docker image, which is why deciding on the right one plays a significant role.

Linux created a helpful alternative that is lightweight and has a minimal POSIX environment – Alpine. This Linux distribution image base is only 5 MB, built around musl libc and BusyBox.

Compared to other OS images, Alpine is much smaller in size. The most downloaded OS image, Ubuntu, is 188 MB, while Alpine is only 5 MB.

Use a .dockerignore File

Excluding certain files that aren’t necessary for your image can help you reduce the image size. That is where the .dockerignore file comes in.

When building an image, you write a Dockerfile with specifications of what that image should look like.

When outlining the build context, it is important also to include a .dockerignore file and store it in the same folder as the Dockerfile.

This Docker feature is initiated with docker run. The system checks whether there is such a file and applies its exceptions and ignore rules. That way, you remove any irrelevant content from the built context.

Utilize the Multi-Stage Builds Feature in Docker

Docker introduced the multi-stage feature in its 17.05 version. It allows users to divide the Dockerfile into multiple stages.

Each stage begins with a FROM instruction. The required artifact passes to the following stage, leaving behind content that you won’t need in the final image artifact.

Since the process only transfers the necessary components of the artifact, you don’t have to clean up manually after every instruction.

With the multi-stage feature, you avoid adding unnecessary layers, which has a considerable impact on the overall image size.

Avoid Adding Unnecessary Layers to Reduce Docker Image Size

A Docker image takes up more space with every layer you add to it. Therefore, the more layers you have, the more space the image requires.

Each RUN instruction in a Dockerfile adds a new layer to your image. That is why you should try to do file manipulation inside a single RUN command. Also, combine different commands into one instruction using the && option.

For instance, you can update the repository and install multiple packages in a single RUN instruction. To get a clear, comprehensive line, use the backslash (\) to type out the command in multiple lines.

Apart from updating and installing the packages, you should also clean up apt cache with && rm -rf /var/lib/apt/lists/* to save up some more space.

RUN apt-get update && apt-get install -y\
         [package-one] \
         [package-two] 
   && rm -rf /var/lib/apt/lists/*

Beware of Updates and Unnecessary Packages and Dependencies

Another way to save space and keep your Docker image small is to ensure you are running the latest version of the platform you are building on.

By having the newest version, you avoid extensive updates that download countless rpm packages and take up a lot of space.

Note: If you need to update, make sure to clean up the rpm cache and add the dnf clean all option: RUN dnf -y update && dnf clean all.

Installing a package also often includes downloading dependencies on which the software relies on. However, sometimes the download will also store packages that are not required but rather are recommended.

Such unwanted packages can add up and consume a lot of disk space. To download only the main dependencies, add the --no-install-recommends option to the install command.

For example:

RUN apt-get install --no-install-recommends [package-one]

Bonus Tip: Caching

Although this tip doesn’t affect the overall size of the Docker image, it does help with faster Docker builds.

Docker speeds up image building by locally caching existing layers of a Dockerfile and using it to rebuild images faster.

For example, imagine you have a simple Dockerfile consisting of three layers. Once you build an image from that file, the system automatically caches these three layers. The next time you build the image, it loads from the local cache.

If you decide to modify the image and change one of the layers, the cache won’t be used for anything after the modified layer.

We recommend ordering the instructions in a way that improved efficiency and utilizes the caching feature. Place instructions that are likely to change as low in the Dockerfile as possible.

How Docker Cache works and how it helps with faster Docker builds.

Conclusion

Docker containers support the implementation of CI/CD in development. Image size and build efficiency are important factors when overseeing and working with the microservice architecture. This is why you should try to keep your Docker images small, by following the valuable advice outlined in this article.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
Docker CMD vs Entrypoint Commands: What's the Difference?
February 18, 2020

CMD is Docker instruction used if you need a default command which users can easily override. ENTRYPOINT is...
Read more
How to Install Docker on CentOS 8
December 27, 2019

CentOS 8 does not provide official support for Docker. This article clearly shows you how to install a fully...
Read more
How to Create Docker Image with Dockerfile
October 23, 2019

A Dockerfile offers a simpler and faster way of creating Docker images. They go through the script with all...
Read more
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019

Docker allows users to create a container in which an application or process can run. In this guide, you will...
Read more