Skip to content

Commit 5866096

Browse files
committed
main: adding multi-stage-build code
1 parent 27159f9 commit 5866096

File tree

6 files changed

+261
-2
lines changed

6 files changed

+261
-2
lines changed

README.md

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,112 @@
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+
![Version](https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000)
3+
[![Twitter: thiagoolsilva](https://img.shields.io/twitter/follow/thiagoolsilva.svg?style=social)](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)_

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "multi-stage-build-docker-example",
3+
"version": "1.0.0",
4+
"description": "This is a very simple example the use of multi stage docker build that improve the docker's image size.",
5+
"main": "dist/index.js",
6+
"repository": "git@github.com:thiagoolsilva/multi-stage-build-docker-example.git",
7+
"author": "Thiago lopes da silva <thiagoolsilva@gmail.com>",
8+
"license": "Apache V2",
9+
"private": true,
10+
"scripts": {
11+
"build": "./node_modules/.bin/tsc ",
12+
"docker:build-clean": "yarn docker:build && yarn docker:clean-stage-build",
13+
"docker:build": "docker build --file=./script/docker/dockerfile --tag=multi-stage-build .",
14+
"docker:run": "docker run --rm -it multi-stage-build",
15+
"docker:clean-stage-build": "docker image prune --force --filter label=stage=builder"
16+
},
17+
"devDependencies": {
18+
"@types/uuid": "8.3.2",
19+
"typescript": "4.4.3"
20+
},
21+
"dependencies": {
22+
"uuid": "8.3.2"
23+
}
24+
}

script/docker/dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2021 Thiago Lopes da Silva
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Stage 1: Build the base code.
16+
FROM node:14.17.0-alpine as buildStage
17+
18+
LABEL stage="builder"
19+
20+
WORKDIR /app
21+
22+
COPY . /app
23+
24+
RUN [ "yarn" ]
25+
26+
RUN [ "yarn", "build" ]
27+
28+
RUN ["rm", "-rf", "node_modules"]
29+
30+
RUN [ "yarn", "--production" ]
31+
32+
# Stage 2. Run build container code using the code from buildStage phase.
33+
FROM node:14.17.0-alpine
34+
LABEL author="Thiago lopes da Silva<thiagoolsilva@gmail.com>"
35+
36+
RUN apk update && apk add bash
37+
38+
WORKDIR /app
39+
40+
RUN addgroup -S container-group && adduser -S container-user -G container-group
41+
42+
USER container-user
43+
44+
COPY --from=buildStage /app/dist/ /app/
45+
COPY --from=buildStage /app/node_modules/ /app/node_modules
46+
47+
CMD [ "node", "src/index.js" ]

src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2021 Thiago Lopes da Silva
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { v4 } from "uuid";
18+
19+
export class Index {
20+
main(): void {
21+
console.log("The method main() was called.");
22+
console.log("calling the uuid.v4()")
23+
console.log(`Result: ${v4()}`);
24+
}
25+
}
26+
27+
new Index().main();

tsconfig.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "./",
4+
"baseUrl": "./",
5+
"outDir": "dist",
6+
"moduleResolution": "node",
7+
"target": "ES6" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
8+
"module": "commonjs" /* Specify what module code is generated. */,
9+
"resolveJsonModule": true /* Enable importing .json files */,
10+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
11+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
12+
"strict": true /* Enable all strict type-checking options. */,
13+
"alwaysStrict": true /* Ensure 'use strict' is always emitted. */,
14+
"noUnusedLocals": true /* Enable error reporting when a local variables aren't read. */,
15+
"noUnusedParameters": true /* Raise an error when a function parameter isn't read */,
16+
"isolatedModules": false,
17+
"sourceMap": true,
18+
"jsx": "preserve",
19+
"declaration": true,
20+
"experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */,
21+
"emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
22+
"composite": true,
23+
"strictPropertyInitialization": false
24+
},
25+
"include": [
26+
"src/**/*.ts",
27+
"test/**/*.ts"
28+
],
29+
"exclude": [
30+
"dist",
31+
"node_modules"
32+
]
33+
}

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@types/uuid@8.3.2":
6+
version "8.3.2"
7+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.2.tgz#b7077bdfc866dbb39939029774806f24e95be791"
8+
integrity sha512-u40ViizKDmdl5FhOXn9WQbulpigYCaiD5hD4KqR3xyQww6l3+0ND+A9TeFla8tFpqvR+UAkJdYb/8jdaQG4/nw==
9+
10+
typescript@4.4.3:
11+
version "4.4.3"
12+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
13+
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==
14+
15+
uuid@8.3.2:
16+
version "8.3.2"
17+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
18+
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

0 commit comments

Comments
 (0)