|
1 | | -# multi-stage-build-docker-example |
2 | | -This is a very example the use of multi stage docker build that improve the docker's image size. |
| 1 | +# Welcome to multi-stage-build-docker-example 👋 |
| 2 | + |
| 3 | +[](https://twitter.com/thiagoolsilva) |
| 4 | + |
| 5 | +> This is a very simple example the use of multi stage docker build that improve the docker's image size. |
| 6 | +
|
| 7 | +### 🏠 [Homepage](https://github.com/thiagoolsilva/multi-stage-build-docker-example) |
| 8 | + |
| 9 | + |
| 10 | +## Why use multi stage docker build? |
| 11 | +According to the official documentation, the use of multi stage docker build will help us as the following description. |
| 12 | + |
| 13 | +> One of the most challenging things about building images is keeping the image size down. Each instruction in the Dockerfile adds a layer to the image, and you need to remember to clean up any artifacts you don’t need before moving on to the next layer. To write a really efficient Dockerfile, you have traditionally needed to employ shell tricks and other logic to keep the layers as small as possible and to ensure that each layer has the artifacts it needs from the previous layer and nothing else. |
| 14 | +
|
| 15 | +> It was actually very common to have one Dockerfile to use for development (which contained everything needed to build your application), and a slimmed-down one to use for production, which only contained your application and exactly what was needed to run it. This has been referred to as the “builder pattern”. Maintaining two Dockerfiles is not ideal. |
| 16 | +
|
| 17 | +Source: https://docs.docker.com/develop/develop-images/multistage-build/ |
| 18 | + |
| 19 | + |
| 20 | +## Which scenarios are recommended to use the multi stage docker? |
| 21 | + |
| 22 | +As described before, I can resume the use of multi docker build in the following use cases. |
| 23 | + |
| 24 | +* Reduce the final docker build image; |
| 25 | +* Split the build, Run and etc... to stages phases; |
| 26 | +* Cache the build Stages; |
| 27 | + |
| 28 | +# Multi Stage Build |
| 29 | + |
| 30 | +This is a piece of multi stage dockerfile split up in two stages. The first one will create a docker to build the code and the second one will get the output of first one |
| 31 | +using the code `COPY --from=buildStage /app/dist/ /app/` due to improve the final docker image size. |
| 32 | + |
| 33 | +``` |
| 34 | +# Stage 1: Build the base code. |
| 35 | +FROM node:14.17.0-alpine as buildStage |
| 36 | +
|
| 37 | +LABEL stage="builder" |
| 38 | +
|
| 39 | +... |
| 40 | +
|
| 41 | +# Stage 2. Run build container code using the code from buildStage phase. |
| 42 | +FROM node:14.17.0-alpine |
| 43 | +LABEL author="Thiago lopes da Silva<thiagoolsilva@gmail.com>" |
| 44 | +
|
| 45 | +... |
| 46 | +
|
| 47 | +COPY --from=buildStage /app/dist/ /app/ |
| 48 | +
|
| 49 | +... |
| 50 | +``` |
| 51 | +[Full dockerfile code](script/docker/dockerfile) |
| 52 | + |
| 53 | + |
| 54 | +## Do you want more details about this topic? |
| 55 | + |
| 56 | +Please give it a try and go to the post that I've created about this topic in the [medium](https://thiagolopessilva.medium.com/) |
| 57 | + |
| 58 | +> The post was written in the Portuguese language. |
| 59 | +
|
| 60 | +## Install |
| 61 | + |
| 62 | +```sh |
| 63 | +yarn install |
| 64 | +``` |
| 65 | + |
| 66 | +## Build |
| 67 | + |
| 68 | +```sh |
| 69 | +yarn build-clean |
| 70 | +``` |
| 71 | + |
| 72 | +## Usage |
| 73 | +```sh |
| 74 | +yarn docker:run |
| 75 | + |
| 76 | +$output: |
| 77 | +The method main() was called. |
| 78 | +calling the uuid.v4() |
| 79 | +Result: 032e4c47-0635-4e84-87b1-724d6e4be3f1 |
| 80 | +Done in 0.69s. |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +## Author |
| 86 | + |
| 87 | +👤 **Thiago lopes da silva <thiagoolsilva@gmail.com>** |
| 88 | + |
| 89 | +* Website: https://medium.com/@thiagolopessilva |
| 90 | +* Twitter: [@thiagoolsilva](https://twitter.com/thiagoolsilva) |
| 91 | +* Github: [@thiagoolsilva](https://github.com/thiagoolsilva) |
| 92 | +* LinkedIn: [@https:\/\/www.linkedin.com\/in\/thiago-lopes-silva-2b943a25\/](https://linkedin.com/in/https:\/\/www.linkedin.com\/in\/thiago-lopes-silva-2b943a25\/) |
| 93 | + |
| 94 | +## 🤝 Contributing |
| 95 | + |
| 96 | +Contributions, issues and feature requests are welcome! |
| 97 | + |
| 98 | +Feel free to check [issues page](https://github.com/thiagoolsilva/multi-stage-build-docker-example/issues). |
| 99 | + |
| 100 | +## Show your support |
| 101 | + |
| 102 | +Give a ⭐️ if this project helped you! |
| 103 | + |
| 104 | + |
| 105 | +## 📝 License |
| 106 | + |
| 107 | +Copyright © 2021 [Thiago lopes da silva <thiagoolsilva@gmail.com>](https://github.com/thiagoolsilva). |
| 108 | + |
| 109 | +This project is [Apache V2](https://github.com/thiagoolsilva/multi-stage-build-docker-example/blob/main/LICENSE) licensed. |
| 110 | + |
| 111 | +*** |
| 112 | +_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ |
0 commit comments