Skip to content

Commit 8225b43

Browse files
committed
docker multistage
1 parent 9c82ee5 commit 8225b43

File tree

1 file changed

+70
-78
lines changed

1 file changed

+70
-78
lines changed

blogs/multistage_docker.html

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
display: none;
2121
}
2222
.container{
23-
width: 80%;
24-
margin: auto;
25-
}
23+
width: 80%;
24+
margin: auto;
25+
}
2626
.code {
27-
background-color: #eee;
28-
border-radius: 3px;
29-
font-family: courier, monospace;
30-
padding: 0 3px;
27+
background-color: #eee;
28+
border-radius: 3px;
29+
font-family: courier, monospace;
30+
padding: 0 3px;
3131
}
3232
.boxed {
33-
border: 1px solid green ;
33+
border: 1px solid green ;
3434
}
3535
</style>
3636
<body>
@@ -54,78 +54,70 @@
5454
<div class="w3-grayscale w3-large">
5555
<!-- About Container -->
5656
<p class="w3-container container" id="about">
57-
<p class="w3-content" style="max-width:700px">
58-
<h5 class="w3-center w3-padding-64"><span class="w3-tag w3-wide">Multi-stage Docker files</span></h5>
59-
</p>
60-
61-
<p class="w3-content" style="max-width:900px">
62-
Multistage docker files are one way to handle large images using multi-stage Dockerfiles.
63-
64-
The problem with the Dcoker image is that they are massive in size! This is because it contains the build tools we don’t need. Also, it contains the source code and intermediate build artifacts again we don't need this, at least not in production.
65-
We could use the RUN command to try and clean the image; delete intermediate build artifacts, uninstall build tools, and delete source code, but that would be tedious. Remember that containers are like cheap, disposable machines; let’s dispose of the build machine and grab a brand new one that has only the runtime installed!
66-
Docker has a neat way to do this; use a single Dockerfile file with distinct sections. An image can be named simply by adding AS at the end of the FROM instruction. Consider the following simplified Dockerfile file:
67-
<code>
68-
<br>
69-
70-
71-
<div class="w3-container container" style="width:500px;height:400 px;border:1px solid #000;">
72-
<b>Dockerfile</b>
73-
<br>
74-
FROM fat-image AS builder
75-
<br>
76-
...
77-
<br>
78-
FROM small-image
79-
<br>
80-
COPY --from=builder /result .
81-
<br>
82-
...
83-
<br>
84-
85-
</div>
86-
</code><br>
87-
<p class="w3-content" style="max-width:900px">
88-
It defines two images, but only the last one will be kept as a result of the docker build command.
89-
The filesystem that has been created in the first image, named builder, is made available to the second
90-
image using the <i>--from</i> argument of the <i>COPY</i> command. It states that the /result folder from the builder
91-
image will be copied to the current working directory of the second image.
92-
This technique allows you to benefit from the tools available in fat-image while getting an image with only the environment defined in the small-image it’s based on. Moreover, you can have many stages in a Dockerfile file when necessary.
93-
<br>Here is an example:
94-
<i>Copied from <a href="https://docs.docker.com/develop/develop-images/multistage-build/">Docker docs</a></i></p>
95-
<div class="w3-container container" style="width:500px;height:1000 px;border:1px solid #000;">
96-
<b>Dockerfile</b><br>
97-
FROM golang:1.7.3<br>
98-
WORKDIR /go/src/github.com/alexellis/href-counter/<br>
99-
RUN go get -d -v golang.org/x/net/html <br>
100-
COPY app.go .<br>
101-
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .<br>
102-
<br>
103-
FROM alpine:latest <br>
104-
RUN apk --no-cache add ca-certificates<br>
105-
WORKDIR /root/<br>
106-
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .<br>
107-
CMD ["./app"] <br>
57+
<p class="w3-content" style="max-width:700px">
58+
<h5 class="w3-center w3-padding-64"><span class="w3-tag w3-wide">Multi-stage Docker files</span></h5>
59+
</p>
60+
<p class="w3-content" style="max-width:900px">
61+
Multistage docker files are one way to handle large images using multi-stage Dockerfiles.
62+
The problem with the Dcoker image is that they are massive in size! This is because it contains the build tools we don’t need. Also, it contains the source code and intermediate build artifacts again we don't need this, at least not in production.
63+
We could use the RUN command to try and clean the image; delete intermediate build artifacts, uninstall build tools, and delete source code, but that would be tedious. Remember that containers are like cheap, disposable machines; let’s dispose of the build machine and grab a brand new one that has only the runtime installed!
64+
Docker has a neat way to do this; use a single Dockerfile file with distinct sections. An image can be named simply by adding AS at the end of the FROM instruction. Consider the following simplified Dockerfile file:
65+
<code>
66+
<br>
67+
<div class="w3-container container" style="width:500px;height:400 px;border:1px solid #000;">
68+
<b>Dockerfile</b>
69+
<br>
70+
FROM fat-image AS builder
71+
<br>
72+
...
73+
<br>
74+
FROM small-image
75+
<br>
76+
COPY --from=builder /result .
77+
<br>
78+
...
79+
<br>
10880
</div>
109-
<p class="w3-content" style="max-width:900px">
110-
111-
When I build an image from that multi-stage definition, I get a 91% improvement over the image size!
112-
When you create an image, you want it to be as small as possible for several reasons:
113-
<ul class="w3-content" style="max-width:900px">
114-
<li>Reduce pull and push times</li>
115-
<li>Use a minimum amount of space in the Registry</li>
116-
<li>Use a minimum amount of space on the machines that will run the containers</li>
117-
<li>Use a minimum amount of space on the machine that creates the image</li>
118-
</ul>
119-
<p class="w3-content" style="max-width:900px">
120-
You want to produce small images or if you plan to generate artifacts inside Docker, make sure to use multi-stage Dockerfile files.
121-
</p>
122-
</p>
123-
</p>
124-
125-
126-
</p>
127-
81+
</code><br>
82+
<p class="w3-content" style="max-width:900px">
83+
It defines two images, but only the last one will be kept as a result of the docker build command.
84+
The filesystem that has been created in the first image, named builder, is made available to the second
85+
image using the <i>--from</i> argument of the <i>COPY</i> command. It states that the /result folder from the builder
86+
image will be copied to the current working directory of the second image.
87+
This technique allows you to benefit from the tools available in fat-image while getting an image with only the environment defined in the small-image it’s based on. Moreover, you can have many stages in a Dockerfile file when necessary.
88+
<br>Here is an example:
89+
<i>Copied from <a href="https://docs.docker.com/develop/develop-images/multistage-build/">Docker docs</a></i>
90+
</p>
91+
<div class="w3-container container" style="width:500px;height:1000 px;border:1px solid #000;">
92+
<b>Dockerfile</b><br>
93+
FROM golang:1.7.3<br>
94+
WORKDIR /go/src/github.com/alexellis/href-counter/<br>
95+
RUN go get -d -v golang.org/x/net/html <br>
96+
COPY app.go .<br>
97+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .<br>
98+
<br>
99+
FROM alpine:latest <br>
100+
RUN apk --no-cache add ca-certificates<br>
101+
WORKDIR /root/<br>
102+
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .<br>
103+
CMD ["./app"] <br>
128104
</div>
105+
<p class="w3-content" style="max-width:900px">
106+
When I build an image from that multi-stage definition, I get a 91% improvement over the image size!
107+
When you create an image, you want it to be as small as possible for several reasons:
108+
<ul class="w3-content" style="max-width:900px">
109+
<li>Reduce pull and push times</li>
110+
<li>Use a minimum amount of space in the Registry</li>
111+
<li>Use a minimum amount of space on the machines that will run the containers</li>
112+
<li>Use a minimum amount of space on the machine that creates the image</li>
113+
</ul>
114+
<p class="w3-content" style="max-width:900px">
115+
You want to produce small images or if you plan to generate artifacts inside Docker, make sure to use multi-stage Dockerfile files.
116+
</p>
117+
</p>
118+
</p>
119+
</p>
120+
</div>
129121
</div>
130122
</div>
131123
<!-- Footer -->

0 commit comments

Comments
 (0)