

A Guide to Docker Multi-Stage Builds
source link: https://www.tuicool.com/articles/YNzERbz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating efficient docker images using Dockerfile is very important when pushing out images into production. We need images as small as possible in production for faster downloads, lesser surface attacks.
In this article, we will see how we can build images efficiently with Docker multi-stage builds and also we will explore what are the options before multi-stage builds.
Here are the topics we cover in this article
- Example Project
- Building Image Using Dockerfile
- Problems With Normal Build
- What is Builder Pattern
- Problems with Builder Pattern
- What are Multi-stage Builds
- Multi-stage Builds Implementation
- Advantages
- Summary
- Conclusion
Tip
Use Bit to encapsulate components with all their dependencies and setup. Share them on Bit’s cloud , collaborate with your team and use them anywhere.

Example Project
Let’s understand the example project that we are building. It’s a simple web app with Angular and node app server. Look at the following diagram to understand better. We have a UI built with Angular and running on the nodejs server.
You can clone the below project and run it on your machine directly. The nodejs server runs on port 3070.
//clone the project
git clone https://github.com/bbachi/docker-multibuild-example.git// change to WebApp directory
cd WebApp// build the Angular Project
npm run build// change back to root directory and start the nodejs server
cd ..
npm start
We are not going to build any functionality in the app for the simplicity reasons and it is irrelevant for this post. We have a simple index.js
for Nodejs server and serve Angular app on port 3070.
We have a UI built with Angular under WebApp folder. We have separate node_modules
for UI and nodejs server. Here is the project structure.
If you follow above commands and you can see the output in the browser served by nodejs server running on port 3070.
Recommend
-
43
Starting in Docker version 17.05 multi-stage builds are available to help you optimize Dockerfiles. Using multi-stage builds is a clean way to set up a Docker build pipeline that simplifies the requirements you need on yo...
-
42
The build time of Golang Docker images always frustrated me as there was always a need to do the go get ./... before we started building the binary. This resulted in fetching the dependencies every…
-
39
In the last article of this series , we finished adding unit tests to our project to reach 100% code coverage. With tests in place...
-
29
When you’re choosing a base image for your Docker image, Alpine Linux is often recommended. Using Alpine, you’re told, will make your images smaller and speed up your builds. And if you’re using Go that’s reasonable advice...
-
11
Multi-stage Image Builds with Docker 2020-04-19docker 3 min readWhat and Why Behind Multistage Builds In a Dockerfile, each statement adds up a new layer to the imag...
-
11
Understanding Docker Multi Stage Builds - Installing Python and Ruby in the Same Dockerfile Nov 22, 2019 Now that I've come up to speed on Python, I have an application in production that uses both Python and...
-
4
Multi-stage builds for Ionic Apps with Azure Pipeline Templates 2021-01-26azure 9 min readIn this article, we will create an Azure Pipeline for an Ionic app using the new Azur...
-
7
Docker multi-stage technology Docker multi-stage technology We can build our application in Dockerfile. But we don’t want the whole thing in our final docker image. Docker multi-stage techno...
-
9
Caching Gems with Docker Multi-Stage build Jul 12, 2022 , by Manmeet Singh 2 minute read Installation...
-
4
Whooosh: A Comprehensive Guide for the Fastest Possible Docker Builds in Human ExistenceWhooosh: A Comprehensive Guide for the Fastest Possible Docker Builds in Human Existence
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK