Abstract
Dockerfile
と Manifest File ( kind: Deployment )
を書く機会があったのでまとめておきます。
1. Dockerfile
- Command
1
2
|
docker build -t test-image .
docker images
|
imageの一括削除の参考は以下
1.1. Sample - Execute SpringBoot Jar
1
2
3
4
|
FROM openjdk:11-jre
ARG JAR_FILE=./build/libs/simple-restcontroller-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "./app.jar"]
|
1.2. Sample - Copy File
- フォルダ構成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[username@instance-1 work]$ tree -L 3
.
├── Dockerfile
├── home
│ ├── boot
│ │ └── init_run.sh
│ │ └── sample.jar
│ ├── env
│ │ └── env.conf
│ └── lib
│ └── hoge.jar
└── root
└── opt
└── oracle
|
- Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
|
FROM centos:7
ENV TEST_HOME /usr/test/home
# mkdir
RUN mkdir -p ${TEST_HOME}
# copy
COPY root/opt /opt
COPY home ${TEST_HOME}
# init settings and run boot app
ENTRYPOINT sh ${TEST_HOME}/boot/init_run.sh
|
- init_run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# initialize processing
## do something
# run boot app
JAR_NAME="sample.jar"
JAVA_OPTIONS=(
""
)
SPRING_OPTIONS=(
""
)
java ${JAVA_OPTIONS} -jar ${JAR_NAME} ${SPRING_OPTIONS}
|
1.3. Sample - Multi-stage Builds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# syntax=docker/dockerfile:experimental
FROM openjdk:11-jdk AS builder
ADD . /work
WORKDIR /work/simple-restcontroller
RUN --mount=type=cache,target=/root/.gradle \
./gradlew clean build
FROM openjdk:11-jre AS runner
WORKDIR /home/app
COPY --from=builder /work/simple-restcontroller/build/libs/simple-restcontroller-0.0.1-SNAPSHOT.jar /home/app/
ENTRYPOINT ["java", "-jar", "./simple-restcontroller-0.0.1-SNAPSHOT.jar"]
|
2. Manifest File ( kind: Deployment )
-
Command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 生成する場合
kubectl apply -f deployment.yaml
kubectl expose deployment simple-restcontroller --type=LoadBalancer --name=my-service
# deployment と service の内容確認
## deployment
### 概要
kubectl get deployments simple-restcontroller
### 詳細
kubectl describe deployments simple-restcontroller
## service
### 概要
kubectl get services my-service
### 詳細
kubectl describe services my-service
# スケールする場合
kubectl scale --replicas=3 -f deployment.yaml
# 削除する場合
kubectl delete services my-service
kubectl delete deployment poc1-app
|
-
deployment.yaml
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
|
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-restcontroller
spec:
replicas: 1
selector:
matchLabels:
name: simple-restcontroller
template:
metadata:
labels:
name: simple-restcontroller
track: stable
version: 1.0.0
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
- name: simple-restcontroller
image: test-image:latest
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: "1"
memory: "1Gi"
ports:
- name: http
containerPort: 8080
readinessProbe:
httpGet:
path: /readiness
port: 81
scheme: HTTP
initialDelaySeconds: 5
timeoutSeconds: 1
livenessProbe:
httpGet:
path: /healthz
port: 81
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 15
timeoutSeconds: 5
|
3. In addition - Execution Result - Sample Multi-Stage Builds
Multi-Stage Buildsにより、約80秒ほどビルドが早くなりました。
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
[username@instance-1 work]$ sudo DOCKER_BUILDKIT=1 docker build --no-cache -t demo .
[+] Building 238.2s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 509B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:experimental 1.0s
=> docker-image://docker.io/docker/dockerfile:experimental@sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183afa6412ff 1.0s
=> => resolve docker.io/docker/dockerfile:experimental@sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183afa6412fff9ca 0.0s
=> => sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183afa6412fff9caa6871649c44 1.69kB / 1.69kB 0.0s
=> => sha256:8c69d118cfcd040a222bea7f7d57c6156faa938cb61b47657cd65343babc3664 521B / 521B 0.0s
=> => sha256:08fb2b2ca3d58e19d791e73dea16126df37608115532e748d63525688df457a5 897B / 897B 0.0s
=> => sha256:61261561661960014533790f5d6c42f5b88362db7e005f35248c20f075900a61 8.88MB / 8.88MB 0.3s
=> => extracting sha256:61261561661960014533790f5d6c42f5b88362db7e005f35248c20f075900a61 0.6s
=> [internal] load metadata for docker.io/library/openjdk:11-jre 0.4s
=> [internal] load metadata for docker.io/library/openjdk:11-jdk
=> [internal] load build context 71.9s
=> => transferring context: 1.88GB 71.7s
=> [runner 1/3] FROM docker.io/library/openjdk:11-jre@sha256:e76cbcd78500cb019f25af60bac7e85d9b2108467379129a22445 22.6s
=> => resolve docker.io/library/openjdk:11-jre@sha256:e76cbcd78500cb019f25af60bac7e85d9b2108467379129a22445d77011cd 0.0s
=> => sha256:8c7c4070295266d562c41ebe5379eaba7d76a4d9f47bf48d067aaa73cbc0ad35 8.65kB / 8.65kB 0.0s
=> => sha256:989e6b19a265d6b8b7934e7ddd7dc07f6e2fc945b3a28dda9b8aecb12cdb30e0 7.81MB / 7.81MB 0.4s
=> => sha256:e76cbcd78500cb019f25af60bac7e85d9b2108467379129a22445d77011cdaa0 1.04kB / 1.04kB 0.0s
=> => sha256:7c462965a1cd98938b2956c1878a297f2edbd73323b3575699711e0685c377d3 1.58kB / 1.58kB 0.0s
=> => sha256:e9afc4f90ab09248d75c8081b6dfba749a7f7efdac704ced7e0ceb506e02fa4a 50.39MB / 50.39MB 1.4s
=> => sha256:af14b6c2f8785723bceb5964c5dec1f0489b7750e9d4ec671e49bfba15d80a39 10.00MB / 10.00MB 0.4s
=> => sha256:68a79816c3e1e5cdd064948818bbd2dbf38783031aeca1474a9a9b50c2d8e5e8 5.53MB / 5.53MB 2.2s
=> => sha256:e440f69a0a762646d5aed47652c8a122f43b415f52b87a90eb14690e1426fd0f 210B / 210B 2.2s
=> => extracting sha256:e9afc4f90ab09248d75c8081b6dfba749a7f7efdac704ced7e0ceb506e02fa4a 233.8s
=> => sha256:837b533a935df78a47a09e44fd06c8ec7af6624ef1eec0d69e8d7d208efe2434 41.94MB / 41.94MB 3.5s
=> => extracting sha256:af14b6c2f8785723bceb5964c5dec1f0489b7750e9d4ec671e49bfba15d80a39 224.6s
=> => extracting sha256:68a79816c3e1e5cdd064948818bbd2dbf38783031aeca1474a9a9b50c2d8e5e8 1.6s
=> => extracting sha256:e440f69a0a762646d5aed47652c8a122f43b415f52b87a90eb14690e1426fd0f 0.0s
=> => extracting sha256:837b533a935df78a47a09e44fd06c8ec7af6624ef1eec0d69e8d7d208efe2434 6.9s
=> [builder 1/4] FROM docker.io/library/openjdk:11-jdk@sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e343234050 44.4s
=> => resolve docker.io/library/openjdk:11-jdk@sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e3432340500a5ec233c 0.0s
=> => sha256:29fc9543dead0f8c1b25afa026e4a3ddafa38c5c353ee407edcea62a95f1f2e4 1.79kB / 1.79kB 0.0s
=> => sha256:e9afc4f90ab09248d75c8081b6dfba749a7f7efdac704ced7e0ceb506e02fa4a 50.39MB / 50.39MB 235.4s
=> => sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e3432340500a5ec233cc7c 1.04kB / 1.04kB 0.0s
=> => sha256:fcfdce4526dfd3cd24f4922025a010c01d9cc4345dd8521cf03b92a98df19bc3 7.01kB / 7.01kB 0.0s
=> => sha256:989e6b19a265d6b8b7934e7ddd7dc07f6e2fc945b3a28dda9b8aecb12cdb30e0 7.81MB / 7.81MB 235.4s
=> => sha256:af14b6c2f8785723bceb5964c5dec1f0489b7750e9d4ec671e49bfba15d80a39 10.00MB / 10.00MB 235.4s
=> => sha256:5573c4b3094956931fd68c310ae92c9eb1a787f0c77ac2730be9d16cce172d5e 51.83MB / 51.83MB 2.0s
=> => sha256:fb1a405f128d35ffb78464f5ac9f15e721e0d205b94360e151d60f32f9b38111 5.28MB / 5.28MB 1.4s
=> => extracting sha256:e9afc4f90ab09248d75c8081b6dfba749a7f7efdac704ced7e0ceb506e02fa4a 7.6s
=> => sha256:cf63ebed11424864a6f71681347d31876110d84dfb087460179f9b66a658df0a 196.21MB / 196.21MB 6.9s
=> => sha256:612a9f566fdc082f9d43f642791723e34bab94c243b34a965b4602d416ee7a72 211B / 211B 2.0s
=> => extracting sha256:989e6b19a265d6b8b7934e7ddd7dc07f6e2fc945b3a28dda9b8aecb12cdb30e0 1.3s
=> => extracting sha256:af14b6c2f8785723bceb5964c5dec1f0489b7750e9d4ec671e49bfba15d80a39 0.7s
=> => extracting sha256:5573c4b3094956931fd68c310ae92c9eb1a787f0c77ac2730be9d16cce172d5e 9.8s
=> => extracting sha256:fb1a405f128d35ffb78464f5ac9f15e721e0d205b94360e151d60f32f9b38111 0.6s
=> => extracting sha256:612a9f566fdc082f9d43f642791723e34bab94c243b34a965b4602d416ee7a72 0.0s
=> => extracting sha256:cf63ebed11424864a6f71681347d31876110d84dfb087460179f9b66a658df0a 8.8s
=> [runner 2/3] WORKDIR /home/app 12.8s
=> [builder 2/4] ADD . /work 45.6s
=> [builder 3/4] WORKDIR /work/simple-restcontroller 0.0s
=> [builder 4/4] RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build 117.0s
=> [runner 3/3] COPY --from=builder /work/simple-restcontroller/build/libs/simple-restcontroller-0.0.1-SNAPSHOT.jar 0.3s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:e19d9e89988f5ebed7aa0e99933ca117243f766ec1aa96aa23c21a67bc3ee84a 0.0s
=> => naming to docker.io/library/demo 0.0s
[username@instance-1 work]$ sudo DOCKER_BUILDKIT=1 docker build --no-cache -t demo .
[+] Building 158.9s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 97B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:experimental 0.5s
=> CACHED docker-image://docker.io/docker/dockerfile:experimental@sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183af 0.0s
=> [internal] load metadata for docker.io/library/openjdk:11-jre 0.2s
=> [internal] load metadata for docker.io/library/openjdk:11-jdk 0.2s
=> [internal] load build context 5.1s
=> => transferring context: 2.34MB 5.0s
=> [runner 1/3] FROM docker.io/library/openjdk:11-jre@sha256:e76cbcd78500cb019f25af60bac7e85d9b2108467379129a22445d 0.0s
=> CACHED [builder 1/4] FROM docker.io/library/openjdk:11-jdk@sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e343 0.0s
=> CACHED [runner 2/3] WORKDIR /home/app 0.0s
=> [builder 2/4] ADD . /work 45.3s
=> [builder 3/4] WORKDIR /work/simple-restcontroller 0.0s
=> [builder 4/4] RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build 106.4s
=> [runner 3/3] COPY --from=builder /work/simple-restcontroller/build/libs/simple-restcontroller-0.0.1-SNAPSHOT.jar 0.3s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:04cc41f59890b6ea317181a516c2257efc78b0e127534bcb29b49ec530a68a8e 0.0s
=> => naming to docker.io/library/demo 0.0s
[username@instance-1 work]$ docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] u
Total reclaimed space: 0B
[username@instance-1 work]$ docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/build/prune?filters=&keep-storage=0: dial unix /var/run/docker.sock: connect: permission denied
[username@instance-1 work]$ sudo docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
Deleted build cache objects:
yxrjv1y47b3y0hm3x6owx189g
qu1o8wc8j1bv1nnjgzeae2zgj
aya90ygi62mlj07avq15squq6
jsyugg6rs20g7jffgtcfjecke
f5tujft67zgtyuox9c26ik7iu
r88y39mqt5wsebtfuy5ar6div
3vumssfvndd90oyvilyxp4qnu
imvymyhj17q2sqsllth0qndor
q6pedljpsshcspdq2fsyt6ocq
0yk4ab9ls61d50f4gtokykg6y
sha256:c9ef80d5f34210a78113974f64ce32386f5c28a6cf538c398daf76052d2abf99
sha256:e313f84ea12ee09cf103147b76bf2fb372d40ccfed273e2389e96a56fb31e11e
sha256:15239410c554dafb7f891f0d51def4992ead6d7307b4f224241d1c685b7ce493
sha256:f863ffb247f4c239aeccf155b7c0d92e35403aed0c825690f6739a89a5901a7c
Total reclaimed space: 6.424GB
[username@instance-1 work]$ sudo DOCKER_BUILDKIT=1 docker build --no-cache -t demo .
[+] Building 221.4s (15/15) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 509B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:experimental 0.4s
=> CACHED docker-image://docker.io/docker/dockerfile:experimental@sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183af 0.0s
=> [internal] load metadata for docker.io/library/openjdk:11-jre 0.2s
=> [internal] load metadata for docker.io/library/openjdk:11-jdk 0.2s
=> [internal] load build context 61.5s
=> => transferring context: 1.88GB 61.4s
=> [runner 1/3] FROM docker.io/library/openjdk:11-jre@sha256:e76cbcd78500cb019f25af60bac7e85d9b2108467379129a22445d 0.0s
=> [builder 1/4] FROM docker.io/library/openjdk:11-jdk@sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e343234050 29.4s
=> => resolve docker.io/library/openjdk:11-jdk@sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e3432340500a5ec233c 0.0s
=> => sha256:43b4a74e4c7698e71ab00cfbbbbafffadc0d49f2c1e3432340500a5ec233cc7c 1.04kB / 1.04kB 0.0s
=> => sha256:29fc9543dead0f8c1b25afa026e4a3ddafa38c5c353ee407edcea62a95f1f2e4 1.79kB / 1.79kB 0.0s
=> => sha256:fcfdce4526dfd3cd24f4922025a010c01d9cc4345dd8521cf03b92a98df19bc3 7.01kB / 7.01kB 0.0s
=> => sha256:612a9f566fdc082f9d43f642791723e34bab94c243b34a965b4602d416ee7a72 211B / 211B 0.3s
=> => sha256:5573c4b3094956931fd68c310ae92c9eb1a787f0c77ac2730be9d16cce172d5e 51.83MB / 51.83MB 1.7s
=> => sha256:fb1a405f128d35ffb78464f5ac9f15e721e0d205b94360e151d60f32f9b38111 5.28MB / 5.28MB 0.4s
=> => sha256:cf63ebed11424864a6f71681347d31876110d84dfb087460179f9b66a658df0a 196.21MB / 196.21MB 4.5s
=> => extracting sha256:5573c4b3094956931fd68c310ae92c9eb1a787f0c77ac2730be9d16cce172d5e 4.4s
=> => extracting sha256:fb1a405f128d35ffb78464f5ac9f15e721e0d205b94360e151d60f32f9b38111 0.6s
=> => extracting sha256:612a9f566fdc082f9d43f642791723e34bab94c243b34a965b4602d416ee7a72 0.0s
=> => extracting sha256:cf63ebed11424864a6f71681347d31876110d84dfb087460179f9b66a658df0a 7.1s
=> CACHED [runner 2/3] WORKDIR /home/app 0.0s
=> [builder 2/4] ADD . /work 50.3s
=> [builder 3/4] WORKDIR /work/simple-restcontroller 0.0s
=> [builder 4/4] RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build 107.6s
=> [runner 3/3] COPY --from=builder /work/simple-restcontroller/build/libs/simple-restcontroller-0.0.1-SNAPSHOT.jar 0.3s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:d9e6b95edf00e2fcd24792461a08f1964c3bfc2f433f9d518ca9645c3cb886f8 0.0s
=> => naming to docker.io/library/demo
|
4. Refference