34

A Guide to Docker Multi-Stage Builds

 4 years ago
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.

vIR7b2Y.jpg Components with Bit: Easily share across projects as a team

Bit.dev | Github

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.

EvIrueN.png!web

Web App with angular and 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.

index.js

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.

JBBbQv3.png!web

Project Structure

If you follow above commands and you can see the output in the browser served by nodejs server running on port 3070.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK