DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Guide to Container Runtimes
  • Gemma 3: Unlocking GenAI Potential Using Docker Model Runner
  • Docker vs Kubernetes: Which to Use and When?

Trending

  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  • Streamlining Event Data in Event-Driven Ansible
  • Emerging Data Architectures: The Future of Data Management
  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Reduce Docker Image Size

How to Reduce Docker Image Size

After some research, we learned some tips that help reduce Docker image size. Read on for the tutorial!

By 
Jamie Liu user avatar
Jamie Liu
DZone Core CORE ·
Oct. 28, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
17.9K Views

Join the DZone community and get the full member experience.

Join For Free

If there are top ten buzzwords in the technology industry in the year 2019, the 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.

Why Using Docker

Docker is widely used in daily front-end development. Nebula Graph Studio (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 to start and applying all the services locally just in one step.
  • Quick deployment: This front-end project is inspired by the Nebula Graph Docker Image.

Building Docker Image

We need to build an image first before hosing our services with Docker. Here we need a configuration file named Dockerfile that contains descriptions of the building steps. To be brief, we need to copy the project into the image and set the startup method:

Java
 




x
13


 
1
# Select base image
2
FROM node:10
3
# Set work directory
4
WORKDIR /nebula-web-console
5
# Copy the current project to the /nebula-web-console directory of the image
6
ADD . /nebula-web-console
7
# Download front-end dependency in the image
8
RUN npm install
9
# Run the building
10
RUN npm run build
11
EXPOSE 7001
12
# Deployment commands executed when the image starts
13
CMD ["npm", "run", "docker-start"]



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, and each of them shares a different internal environment. For example, the alpine 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 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 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.

Docker Building Principle

In short, a 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:

Java
 




xxxxxxxxxx
1
17


 
1
# Set up the image generated in the first step and name it builder
2
FROM node:10-alpine as builder
3
WORKDIR /nebula-web-console
4
# Copy the current project to the image
5
ADD . /nebula-web-console
6
# Start building
7
RUN npm install
8
RUN npm run build
9
....
10

          
11
# Start the second step build
12
FROM node:10-alpine
13
WORKDIR /nebula-web-console
14
# Copy the product of the first step image to the current image. Only one image layer is used here, which saves the number of image layers in the previous building step.
15

          
16
COPY --from=builder . /nebula-web-console
17
CMD ["npm", "run", "docker-start"]



.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 to specify files to be ignored.

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:

Java
 




xxxxxxxxxx
1


 
1
# The two operations represent two layers
2
RUN npm install
3
RUN npm run build



Merge the above command to one:

Java
 




xxxxxxxxxx
1


 
1
# It becomes a single layer with &
2
RUN npm install && npm run build



Regular Front-End Optimization

  • Compress ugly code and remove source code Finish this step when building an 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 a network environment, place large public files ( pictures and third party libraries, etc.) on 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 in reducing the Docker image of the Nebula Graph Studio. Please leave us a comment if you have any questions. Welcome try Nebula Graph Studio on GitHub.

References

  • How to reduce Docker Image sizes using multi-stage builds
  • Nebula Graph Docker
  • Nebula Graph Studio
Docker (software)

Published at DZone with permission of Jamie Liu. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Guide to Container Runtimes
  • Gemma 3: Unlocking GenAI Potential Using Docker Model Runner
  • Docker vs Kubernetes: Which to Use and When?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!