-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile-Multistage
69 lines (59 loc) · 1.88 KB
/
Dockerfile-Multistage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
docker Image multistage
########################
One of the most challenging things about building images is keeping the image size down. Each RUN, COPY, and ADD 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.
Example for multistage build:
-----------------------------
code file: hello.c
--------
#include <stdio.h>
int main (void)
{
printf("Hello,world!\n");
return 0;
}
Dockerfile ( Without multistage )
----------
FROM alpine:3.7
RUN apk update && apk add --update alpine-sdk
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN mkdir bin
RUN gcc -Wall hello.c -o bin/hello
CMD /app/bin/hello
Dockerfile ( multistage )
-----------
FROM alpine:3.7 AS build
RUN apk update && apk add --update alpine-sdk && mkdir /app
WORKRIR /app
COPY . .
RUN mkdir bin
RUN gcc -Wall hello.c -o bin/hello
FROM alpine:3.7
COPY --from=build /app/bin/hello /app/hello
CMD /app/hello
EXM: for java file
code file: hello.java
-----------
class Hello{
public static void main(String[] args){
System.out.println("This is java app \n by using Docker");
}
}
Dockerfile ( without mutlistage )
-----------
FROM java:8
COPY . /var/www/java
WORKDIR /var/www/java
RUN javac hello.java
CMD ["java", "hello"]
Dockerfile ( mutlistage )
-----------
FROM java:8 AS build #image with runtime and compiler
COPY . /var/www/java
WORKDIR /var/www/java
RUN javac hello.java
FROM openjdk:18-jdk-alpine3.15 #searche image with just runtime without any compiler
COPY --from=build /var/www/java/hello.java
CMD ["java", "hello"]
https://docs.docker.com/develop/develop-images/multistage-build/