Docker image size

At the heart of the Docker platform lies the Docker image, an essential component that houses all the necessary files, libraries, and dependencies required to run a containerized application. However, these images can grow significantly in size, making them challenging to store, transfer, and manage, especially in large-scale production environments. In this article, we'll explore some tips and tricks for reducing Docker image size to help you optimize your containerized application deployments.

Use multi-stage builds

One of the most effective ways to reduce Docker image size is to use multi-stage builds. A multi-stage build is a technique that involves creating multiple Docker images within a single Dockerfile. By breaking down the build process into smaller stages, you can minimize the number of files, libraries, and dependencies included in the final image.

For example, you can use a base image that contains all the necessary build tools and dependencies to build your application. Once you've compiled your code, you can copy only the required files and libraries into a separate image, leaving any unnecessary build artifacts behind.

Multi-stage builds use two or more FROM commands. The following example illustrates building a simple web server that serves HTML from your docs directory in Git:

# syntax=docker/dockerfile:1

# stage 1
FROM alpine as git
RUN apk add git

# stage 2
FROM git as fetch
WORKDIR /repo
RUN git clone https://github.com/your/repository.git .

# stage 3
FROM nginx as site
COPY --from=fetch /repo/docs/ /usr/share/nginx/html

This build has 3 stages: git, fetch and site. In this example, git is the base for the fetch stage. It uses the COPY --from flag to copy the data from the docs/ directory into the Nginx server directory.

Each stage has only a few instructions, and when possible, Docker will run these stages in parallel. Only the instructions in the site stage will end up as layers in the final image. The entire git history doesn’t get embedded into the final result, which helps keep the image small and secure.

Choose a lightweight base image.

Another way to reduce Docker image size is to use a lightweight base image. The base image forms the foundation of your Docker image and includes the core operating system and essential libraries and dependencies. Choosing a lightweight base image, such as Alpine Linux, can significantly reduce the size of your final Docker image.

Alpine Linux is a minimal Linux distribution designed for resource-constrained environments. It's incredibly lightweight, with a base image size of just a few megabytes. Additionally, Alpine Linux uses the musl libc library instead of the more common glibc library, resulting in smaller binary sizes.

Use multi-layer image caching.

Docker employs a layer-based storage system that caches each build step as a separate layer. Docker will only rebuild layers that have changed since the last build when building an image. As such, utilizing multi-layer image caching can help reduce build times and overall image size.

To take advantage of multi-layer image caching, you should order the commands in your Dockerfile from most to least frequently changed. For example, you should place commands that install dependencies at the top of your Dockerfile to avoid rebuilding the entire image each time you make a code change.

Minimize the number of layers.

In addition to multi-layer image caching, you should also aim to minimize the number of layers in your Docker image. Each layer adds overhead to the final image size, so the fewer layers you have, the smaller your image will be.

You can reduce the number of layers in your Docker image by chaining multiple commands together in a single RUN instruction, separating each command with &&. Additionally, you can use the COPY instruction to copy multiple files into a single layer.

Remove unnecessary files and dependencies.

Finally, one of the most effective ways to reduce Docker image size is to remove any unnecessary files and dependencies from your image. This can include temporary build artifacts, unused libraries, and unneeded system packages.

You can use tools like Docker Squash or dive into identifying and removing unnecessary files and dependencies. Docker Squash is a tool that optimizes Docker images by removing unnecessary layers and squashing the remaining layers into a single layer. Dive is a tool that lets you explore and analyze Docker images, allowing you to identify any files or dependencies that can be safely removed.

Conclusion

Optimizing Docker image size is crucial for efficient containerized application deployments. By following the tips and tricks outlined in this article, you can reduce the size of your Docker images and improve the overall performance of your containerized applications. Remember to use multi-stage builds, choose a lightweight base image, utilize multi-layer image caching, minimize the number of layers, and remove unnecessary files.

Mateusz Kozak

Mateusz Kozak

Warsaw, Poland