How to reduce Docker Image sizes using multi-stage builds
Tags: docker, tutorial
Published: 2019-08-02 13:21:50

---
title: How to reduce Docker Image sizes using multi-stage builds
published: true
tags: docker, tutorial
canonical_url: https://blog.logrocket.com/reduce-docker-image-sizes-using-multi-stage-builds/
---

![](https://thepracticaldev.s3.amazonaws.com/i/bss537j8mquvqa764103.png)

## Introduction

[Docker](https://www.docker.com/) is one of the most important technologies in enterprises nowadays. Most tech companies are using Docker to improve the deployment strategy of its products and services, making them robust and scalable. In this article, we will look at one of the most promising features of writing Dockerfiles efficiently to reduce the final image size. But first, let’s understand a bit about Docker.

## What is Docker?

Docker is containerization of the applications similar to VMs, but very lightweight (read the complete article on [Docker vs Virtual Machines](https://nickjanetakis.com/blog/comparing-virtual-machines-vs-docker-containers)). Docker is a tool to easily create, deploy, and run applications by using containers that are independent of the OS.

A **container** packages the application services or functions with all of the libraries, configuration files, dependencies, and other necessary parts to operate. Each container shares the services of one underlying operating system.

[![LogRocket Free Trial Banner](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/f760c-1gpjapknnuyhu8esa3z0jga.png?resize=1200%2C280&ssl=1)](https://logrocket.com/signup/)

## What are these Docker Images?

Docker Images are the set of instructions written in a file called `Dockerfile`. These instructions act as a multi-layered filesystem in Docker. When a Docker user runs the images, it produces one or multiple containers.

We can also say that Docker Images are immutable files, basically a snapshot of the container. We can make _n_ number of containers from a single Docker image similar to OOPs concept of creating _n_ objects instances (which share common characteristics and behavior) from a single Class.

Like I said earlier, Dockerfile contains the set of instructions which acts as a multi-layer filesystem. The more instructions we have (for example `RUN`, `COPY`, `ADD`)  in our Dockerfile, the greater the final size of the image. There are many other things that are responsible for increasing the size of the image, like the context, base image, unnecessary dependencies, packages, and a number of instructions.

## Why reduce the size of Docker Images?

Why do we need to reduce the size of the Docker image in this modern era of tech, where memory and storage are relatively cheap?

By reducing the Docker image size, we keep only the required artifacts in the final image and remove all the unnecessary data. It is also necessary because:

- First and foremost, it’s [best practices](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/)
- Installing and keeping unnecessary dependencies in your image increases complexity and chances of vulnerability in your application
- It will take a lot of time to download and spawn the containers
- It will also take a lot of time to create and push the image to the registry and ends up blocking our CI/CD pipelines
- Sometimes, we end up leaving keys and secrets in the Dockerfile due to build context
- To make the container immutable (yeah you read that right) we can’t even edit a file in the final container. That’s why we use [CoreOS](https://coreos.com/) instances

## How to reduce the size of Docker Images

Reducing Docker Images is something we should know how to do to keep our application secure and stick with the proper industry standards and guidelines.

There are a lot of ways to do this, including:

- Use a .dockerignore file to remove unnecessary content from the build context
- Try to avoid installing unnecessary packages and dependencies
- Keep the layers in the image to a minimum
- Use alpine images wherever possible
- Use Multi-Stage Builds, which I am going to talk about in this article.

Let’s move to **Multi-Stage Builds.**![🤘](https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f918.png)

## Multi-stage builds in Docker

Multi-stage builds in Docker are a new feature introduced in [Docker 17.05](https://docs.docker.com/develop/develop-images/multistage-build/). It is a method to reduce the image size, create a better organization of Docker commands, and improve the performance while keeping the Dockerfile easy to read and understand.

The multi-stage build is the dividing of Dockerfile into multiple stages to pass the required artifact from one stage to another and eventually deliver the final artifact in the last stage. This way, our final image won’t have any unnecessary content except our required artifact.

Previously, when we didn’t have the multi-stage builds feature, it was very difficult to minimize the image size. We used to clean up every artifact (which isn’t required) before moving to the next instruction as every instruction in Dockerfile adds the layer to the image. We also used to write bash/shell scripts and apply hacks to remove the unnecessary artifacts.

Let’s look at an example:

[![](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2019/08/runimagedocker.png?resize=958%2C590&ssl=1)](https://carbon.now.sh/?bg=rgba%28171%2C%20184%2C%20195%2C%201%29&t=dracula&wt=none&l=dockerfile&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=RUN%2520wget%2520http%253A%252F%252Fxyz.com%252Fabc.tar.gz%2520%2526%2526%2520%255C%250A%2520%2520%2520%2520tar%2520zxf%2520abc.tar.gz%2520%2526%2526%2520%255C%250A%2520%2520%2520%2520cd%2520abc%2520%2526%2526%2520%255C%250A%2520%2520%2520%2520make%2520DESTDIR%253D%252Ftmp%2520install%2520%2526%2526%2520%255C%250A%2520%2520%2520%2520cd%2520..%252F%2520%2526%2526%2520%255C%250A%2520%2520%2520%2520rm%2520-rf%2520abc*)

This is just the one instruction of the Dockerfile in which we need to download the `abc.tar.gz` file from some [`http://xyz.com`](https://gen.xyz/) website and extract the content and run `make install`.

In the same instruction, we stored the content of the `make install` command to `/tmp` dir and removed the remaining data like the downloaded `tar` file and extracted `tar` contents so that we can only have the content of the `make install` command, which is required for our further processing.

That’s all the stuff we have to do in one instruction to reduce the size of the final image. Now we can imagine the complexity of the Dockerfile for _n_ number of instructions.

Ohh wait..wait..wait..!!! Now we have the power of multi-stage builds with which we can reduce the size of the image without compromising the readability of the Dockerfile.

Let’s look at the same example using multi-stage build:

[![](https://i1.wp.com/blog.logrocket.com/wp-content/uploads/2019/08/multistagedockerinstructions.png?resize=512%2C506&ssl=1)](https://carbon.now.sh/?bg=rgba%28171%2C%20184%2C%20195%2C%201%29&t=dracula&wt=none&l=dockerfile&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=%2523%2520Base%2520image%2520Stage%25201%250AFROM%2520ubuntu%253A16.04%2520as%2520stage1%250A%250ARUN%2520apt-get%2520update%250ARUN%2520apt-get%2520-y%2520install%2520make%2520curl%250ARUN%2520curl%2520http%253A%252F%252Fxyz.com%252Fabc.tar.gz%2520-O%250ARUN%2520tar%2520zxf%2520abc.tar.gz%2520%2526%2526%2520cd%2520abc%250ARUN%2520make%2520DESTDIR%253D%252Ftmp%2520install%250A%250A%2523%2520Stage%25202%250AFROM%2520alpine%253A3.10%250A%250ACOPY%2520--from%253Dstage1%2520%252Ftmp%2520%252Fabc%250A%250AENTRYPOINT%2520%255B%2522%252Fabc%252Fapp%2522%255D)

Here in this Dockerfile, we are using `ubuntu:16.04` as a base image and called this stage as `stage1` and executed some instructions as follows:

1. Run `apt-get update` to update the packages
2. Run `apt-get -y install make curl` to install make and curl packages
3. We downloaded the `abc.tar.gz` file from `http://xyz.com` using curl
4. Untar the `abc.tar.gz` file and change the directory to `abc`
5. Run the `make DESTDIR=/tmp install` command to store the output to `tmp` directory
6. Rather than removing the unnecessary artifacts, we created another stage i.e stage 2 with `alpine:3.10` as the base image because it is lighter
7. We copied the content from the `/tmp` dir from `stage1` to `/abc` dir in `stage2` by simply running `COPY --from=stage1 /tmp /abc` command
8. Finally, we added the path of the binary in the `Entrypoint` to run it

This way, we copied the required artifact from stage 1 to stage 2 without compromising the Dockerfile and successfully created the most optimized and reduced image. Similarly, we can use multi-stage builds to create a static build for the frontend files and pass the static files to stage 2 where we can use nginx base image to host them without keeping the large, bulky `node_modules` in our app which is of no use after the static build.

## Conclusion

We can also use external Docker images as a stage and can also stop at a specific build stage. It is not always useful as we lost the previous stage intermediate containers so we won’t be able to leverage build cache in Docker. Read more about the multi-stage build from [Docker official docs](https://docs.docker.com/develop/develop-images/multistage-build/).

In this article, we looked at what Docker is, why we need to reduce the size of images, and how can we do this using multi-stage builds effectively. I hope this article helped you understand Docker and its multi-stage builds feature.

Feel free to comment and ask me anything. You can follow me on [Twitter](https://twitter.com/ankitjain28may) and [Medium](https://medium.com/@ankitjain28may). Thanks for reading! ![👍](https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f44d.png)
* * *
## Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps
 
![LogRocket Dashboard Free Trial Banner](https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/1d0cd-1s_rmyo6nbrasp-xtvbaxfg.png?resize=1200%2C677&ssl=1)
 
[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.
 
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.
 
[Try it for free](https://logrocket.com/signup/).
* * *
The post [How to reduce Docker Image sizes using multi-stage builds](https://blog.logrocket.com/reduce-docker-image-sizes-using-multi-stage-builds/) appeared first on [LogRocket Blog](https://blog.logrocket.com).