How to Reduce Docker Image Size
Tags: nebulagraph, docker
Published: 2020-02-12 07:08:52

---
title: How to Reduce Docker Image Size
published: true
date: 2020-02-11 08:30:59 UTC
tags: nebula-graph,docker
canonical_url: http://nebula-graph.io/en/posts/how-to-reduce-docker-image-size/
---

![](https://cdn-images-1.medium.com/max/1024/1*zcDMzkLqdfZXJwIMM_6FKw.png)<figcaption>How to Reduce Docker Image Size</figcaption>

If there are top ten buzzwords in the technology industry in the year of 2019, **container** is sure to be one of them. With the popularity of Docker, more and more scenarios are using Docker in the front-end field.

This article shows how do we use Docker in the visualization interface of **Nebula Graph** , a distributed open source graph database and have reduced the Docker image from 1.3G to 0.3G with the tips listed in this post.

### Why Using Docker

Docker is widely used in daily front-end development. [Nebula Graph Studio](https://github.com/vesoft-inc/nebula-web-docker/blob/master/README.md) (A visualization tool for **Nebula Graph** ) uses Docker based on the following considerations:

- **Unified operating environment** : There are several services behind our tools, such as existing services from different technology stacks, and pure front-end static resources.
- **Low user cost** : Currently, cloud services are under development. We want a smooth experience of the combined services, and Docker makes this possible with the ability of starting and applying all the services locally just in one step.
- **Quick deployment** : This front-end project is inspired by the [Nebula Graph Docker Image](https://github.com/vesoft-inc/nebula-docker-compose).

### Building Docker Image

We need to build an image first before hosing our services with Docker. Here we need a configuration file named [Dockerfile](https://docs.docker.com/engine/reference/builder/) that contains descriptions of the building steps. To be brief, we need to copy the project into the image and set the startup method:

{% gist https://gist.github.com/Jamie-VESoft/ae62291a024dc61ff0a1c8f0bba37223 %}

### Reducing Docker Image

The above configuration file will build a Docker image with a size of about 1.3GB, which looks a bit scary because downloading is too time-consuming even with a fast network. That is totally unacceptable.

After some research, we learned some tips that help reduce Docker image size.

#### Using Smaller Base Image

Docker base image (for example, the above mentioned node:10) is the basic image on which you add layers and create a final image containing your applications. There are **multiple versions** of the Node.js image on [DockerHub](https://hub.docker.com/_/node), and each of them share a different **internal environment**. For example, the [alpine](https://yeasy.gitbooks.io/docker_practice/cases/os/alpine.html) version is a more simplified Linux system image that removes some tools like bash, curl etc. to decrease size.

Based on our needs, we change the base image to [alpine](https://yeasy.gitbooks.io/docker_practice/cases/os/alpine.html) and rebuild to reduce the docker image from 1.3GB to 500MB+. So if the docker image you are building is too large, you can **replace the basic image**.

#### Multi-stage Build

[Multi-stage](https://docs.docker.com/develop/develop-images/multistage-build/) build in docker is a new feature introduced in docker 17.05. 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.

**1.Docker Building Principle**

In short, multi-stage build is dividing the 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. Let’s consider an example:

{% gist https://gist.github.com/Jamie-VESoft/4a7a0f8ef2bc1c45269885671c79f860 %}

**2. .dockerignore**

Similar to the well known .gitignore that ignores unnecessary (such as document files, git files, node\_modules, etc) files when using COPY or ADD command to copy or add files, we can use [.dockerignore](https://docs.docker.com/engine/reference/builder/#dockerignore-file) to specify files to be ignored.

**3. Merging Multiple Layers Into One**

When building a Docker image with a Dockerfile, each operation adds a new layer based on the previous step image. We can use & to merge multiple operations to reduce layers. For example:

{% gist https://gist.github.com/Jamie-VESoft/febdd58e697eea466d98e48d773ed6a1 %}

Merge the above command to one:

{% gist https://gist.github.com/Jamie-VESoft/12a85bb59f748f0e50cea24f7ecc643c %}

#### Regular Front-End Optimization

- Compress ugly code and remove source code  
Finish this step when building image so that the image size is further reduced.
- Only downloading code needed for production with node\_modules  
Finish this step when deploying, be sure to download only third party dependence code for production: npm install --production
- Place public source on CDN  
If the image is expected to run in network environment, place large public files ( pictures and third party libraries, etc.) on the the CDN server so that some resources are separated and the image size is further reduced.
- …

The above suggestions are only for your reference. You can migrate more regular front-end optimizations to the image given that image building itself is an environment for running code.

### Summary

The above is our experience on reducing the Docker image of the [Nebula Graph Studio](https://github.com/vesoft-inc/nebula-web-docker/blob/master/README.md). Please leave us a comment if you have any questions. Welcome to try Nebula Graph Studio on [GitHub](https://github.com/vesoft-inc/nebula-web-docker).

### References

- [How to reduce Docker Image sizes using multi-stage builds](https://dev.to/bnevilleoneill/how-to-reduce-docker-image-sizes-using-multi-stage-builds-51nl)
- [Nebula Graph Docker](https://github.com/vesoft-inc/nebula-docker-compose)
- [Nebula Graph Studio](https://github.com/vesoft-inc/nebula-web-docker)



* * *