多阶段构建镜像
1.建立一个目录(将它命名为 multi-build )用作构建上下文,并切换到该目录
[root@host1 ~]# mkdir -p multi-build
[root@host1 ~]# cd multi-build
[root@host1 multi-build]# pwd
/root/multi-build
2..在该目录中添加一个 main.go 简单程序
[root@host1 multi-build]# vi main.go
[root@host1 multi-build]# cat main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
3.创建 Dockerfile
[root@host1 multi-build]# vi Dockerfile
[root@host1 multi-build]# cat Dockerfile
# syntax=docker/dockerfile:1
FROM golang:1.21 as build
WORKDIR /src
COPY main.go ./
RUN go build -o /bin/hello ./main.goFROM scratch
COPY --from=build /bin/hello /bin/hello
CMD ["/bin/hello"]
4.构建镜像
[root@host1 multi-build]# docker build -t go-hello-world .
[+] Building 117.8s (12/12) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 293B 0.0s=> resolve image config for docker-image://docker.io/docker/dockerfile:1 3.0s=> CACHED docker-image://docker.io/docker/dockerfile:1@sha256:dabfc0969b935b2080555ace70ee69a 0.0s=> [internal] load metadata for docker.io/library/golang:1.21 5.6s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [build 1/4] FROM docker.io/library/golang:1.21@sha256:4746d26432a9117a5f58e95cb9f954ddf0d 64.0s=> => resolve docker.io/library/golang:1.21@sha256:4746d26432a9117a5f58e95cb9f954ddf0de128e9d 0.0s=> => sha256:903681d87777d28dc56866a07a2774c3fd5bf65fd734b24c9d0ecd9a13c9f6 49.55MB / 49.55MB 7.6s=> => sha256:6ed93aa58a52c9abc1ee472f1ac74b73d3adcccc2c30744498fd5f14f3f5d2 64.14MB / 64.14MB 7.1s=> => sha256:e6ba96dde4af9f4e06caba475fa65e94e4c54fe4aa3e9f4f504c02178eb8934e 2.32kB / 2.32kB 0.0s=> => sha256:3cbbe86a28c2f6b3c3e0e8c6dcfba369e1ea656cf8daf69be789e0fe210598 24.05MB / 24.05MB 5.3s=> => sha256:4746d26432a9117a5f58e95cb9f954ddf0de128e9d5816886514199316e4a2fb 9.75kB / 9.75kB 0.0s=> => sha256:246ea1ed9cdb1164bb5cb7e1f45d7914b98c6d9418c8e1cc443105e820bbd9d1 2.82kB / 2.82kB 0.0s=> => sha256:1f46bd02dde39f0741d70614fc607bf03c1a0cd60d52e7dd06c44f8fb9358 92.23MB / 92.23MB 14.5s=> => sha256:54bf7053e2d96c2c7f4637ad7580bd64345b3c9fabb163e1fdb8894aea8a9 67.01MB / 67.01MB 14.0s=> => sha256:532d43a0bc41875119c835fd75616e87d5df2a3714ddea3a0b5a68c7c5982649 126B / 126B 8.6s=> => extracting sha256:903681d87777d28dc56866a07a2774c3fd5bf65fd734b24c9d0ecd9a13c9f636 10.1s=> => sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 32B / 32B 9.6s=> => extracting sha256:3cbbe86a28c2f6b3c3e0e8c6dcfba369e1ea656cf8daf69be789e0fe2105982b 2.5s=> => extracting sha256:6ed93aa58a52c9abc1ee472f1ac74b73d3adcccc2c30744498fd5f14f3f5d22c 11.3s=> => extracting sha256:1f46bd02dde39f0741d70614fc607bf03c1a0cd60d52e7dd06c44f8fb9358709 10.4s=> => extracting sha256:54bf7053e2d96c2c7f4637ad7580bd64345b3c9fabb163e1fdb8894aea8a9af0 19.7s=> => extracting sha256:532d43a0bc41875119c835fd75616e87d5df2a3714ddea3a0b5a68c7c5982649 0.0s=> => extracting sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 0.0s=> [internal] load build context 0.0s=> => transferring context: 165B 0.0s=> [build 2/4] WORKDIR /src 31.4s=> [build 3/4] COPY main.go ./ 0.1s=> [build 4/4] RUN go build -o /bin/hello ./main.go 13.1s=> [stage-1 1/1] COPY --from=build /bin/hello /bin/hello 0.0s=> exporting to image 0.0s=> => exporting layers 0.0s=> => writing image sha256:647f9aa79275c5bdeb0fd8218526326916085462bdf7d8d0a69d2f9e5553a2a2 0.0s=> => naming to docker.io/library/go-hello-world 0.0s1 warning found (use docker --debug to expand):- FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 2)
[root@host1 multi-build]# vi Dockerfile
[root@host1 multi-build]# cat Dockerfile
# syntax=docker/dockerfile:1
FROM golang:1.21 AS build
WORKDIR /src
COPY main.go ./
RUN go build -o /bin/hello ./main.goFROM scratch
COPY --from=build /bin/hello /bin/hello
CMD ["/bin/hello"]
[root@host1 multi-build]# docker build -t go-hello-world .
[+] Building 12.2s (2/2) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 293B 0.0s=> ERROR resolve image config for docker-image://docker.io/docker/dockerfile:1 12.1s
------> resolve image config for docker-image://docker.io/docker/dockerfile:1:
------
Dockerfile:1
--------------------1 | >>> # syntax=docker/dockerfile:12 | FROM golang:1.21 AS build3 | WORKDIR /src
--------------------
ERROR: failed to build: failed to solve: failed to resolve source metadata for docker.io/docker/dockerfile:1: unexpected status from HEAD request to https://9fbd5949cbc94e4a9581e33b9077c811.mirror.swr.myhuaweicloud.com/v2/docker/dockerfile/manifests/1?ns=docker.io: 400 Bad Request
[root@host1 multi-build]# docker builder prune -af
IDRECLAIMABLESIZELAST ACCESSED
9aojo2wrtoyrjoa77ymmsxyux* true 2.384kB 3 days ago
ocnpvxz7p906tivdqhi867i9n* true 1.096kB 3 days ago
bk0bpjrbwxjoco3lbft5g7f4x* true 194B About a minute ago
85yqi2tsrzds18xf8irj3dy58 true 0B 5 days ago
jkpvmyf4020dpzhu0jsyu2fuj* true 0B 24 hours ago
g0o06mhxo1a7a0xjohqt20r8v true 44B 9 days ago
48v0ybaxqnyz2kzow7wyvqtdb true 2.398kB 3 days ago
m9p6rcxpfvpsdy5v27j54y525* true 71B 3 minutes ago
z0yh7jlasndepayvkwgrnrd3o* true 131B 3 days ago
vek2uft5x170pwj3b5v2acrfc true 8B 24 hours ago
6ihdq68runx42neyfjrtymukq* true 0B 3 days ago
wwhz932hec3clnk9pyef15e33* true 0B 3 days ago
64m8kpp6i40quk01wbktz8glz* true 0B 3 days ago
pwql5nyw670czbv46ie1mqn70 true 2.384kB 3 days ago
zlgzkvy2uu60hx9n6ud60iy1j* true 2.398kB 3 days ago
lodoh5ioe9vh3ip4rcnaa1hy5* true 944B 3 days ago
tqj1jc1j5j8t12054lx8hdftn true 33B 3 days ago
mivqvfxac2pi6hpeqligz6mnh* true 33B 3 days ago
jl5szyyt8vlnulmo540mm039n* true 0B 3 days ago
la9att1cuag5dhfk3vdfit9c1 true 118B 12 days ago
awqh3y0ss2lwl307o2rzldd9b true 109.5MB Less than a second ago
kenzc91fzuw4pv5r41r8a3f9u true 710B Less than a second ago
kf2fh314ajeb524gdo4izjqu6* true 472B 24 hours ago
qy9bo8ccu74ovk42fafqmb893* true 1.242kB 3 days ago
b2iqyx25h7qz26g9akopw2eoy* true 27.02MB 3 minutes ago
h5d8qd50kksesrmvgcixihf5s true 1.806MB 3 minutes ago
f72hylrm6syeq4wqh3z6rs70b* true 871.5kB 2 days ago
xl8jit2od74u4ypdlqamjd9fi* true 0B 2 days ago
pjrehjq3rd7s3obw1hws3rj48 true 0B Less than a second ago
yibsm2t5k0txaaibkb6m83wnw true 0B Less than a second ago
t7setpaju0hkdgbowayyvzkd1 true 591.7MB Less than a second ago
z34gadape14b8nuzmmdsvn2ud true 871.5kB 2 days ago
hyq4dwxfhwyi1zz0i7zpu5hrb* true 40B 2 days ago
5gzwx2bzyrub412rw05d9xlf0 true 0B 3 minutes ago
yt9ykww6r3nraqk9sxavtd8ll* true 0B 3 minutes ago
n1mfp5csm05gk1ztw12ex6nct* true 2.252kB 3 days ago
wkif2wy9phuu19x0vhh48gn5c true 12.68MB Less than a second ago
ugru40sod1ismv96ilfa3hh93 true 0B Less than a second ago
zklcmzb43bq5n5mq68pe4eqtb true 2.21MB Less than a second ago
p38s2xtomk2niji0py7o6pc3s true 71B 3 minutes ago
hnq2jea3q8ojz3waxh4easgdx true 0B Less than a second ago
pzv0itrgnad5vt7gh34s557nx true 12B Less than a second ago
rpnaee2jxe1jjkitz7ic7va43 true 28.56MB Less than a second ago
op41kch1bom3sz1y1z31c5p9n true 0B Less than a second ago
orw4lu87s1skljwjnlf9ptayp true 8B 24 hours ago
yas6p9jq9m1cvrqgzc8amo0me true 0B Less than a second ago
nr9r8z5w2zdjjfc919pz6k31o true 0B Less than a second ago
pwn61d3tynhp1tv8g4q21ftj3 true 0B 3 minutes ago
5kp88y7rc7m3mndywvrbxpyob true 0B Less than a second ago
6n7v1zmhedm5juckimkzl9nn8 true 0B 3 minutes ago
sfmtt2p0y06810b4jxd566iy0 true 0B Less than a second ago
lu4b1xvyaumtprk67v70s95r6 true 1.315kB Less than a second ago
ok1piu62511s2o7ab6b6klfgt true 31B Less than a second ago
qspocpdn3jge791pl3isrwiiz true 0B Less than a second ago
mbew77ncb61b3b6sblprnj70f true 153.4MB Less than a second ago
ywwr6w5wp9hy3lbszot85bhaf true 612B Less than a second ago
evyt9nvlwyc0c3at3k6d96ic5 true 0B Less than a second ago
huzevfay80mzo5618h22mm5ra true 0B Less than a second ago
8qkizur54n8rg2tcepsc5sxwy true 0B 4 minutes ago
jszorljrdnq1es337h6hb3h6f true 95B Less than a second ago
kmqsc8gmiv5yq1tzwtiwtuxv5 true 604.8MB Less than a second ago
abnlj6sr50q6rbp5t7dqpewzz true 0B Less than a second ago
hhjto6y1i2ebjnovao0gams39 true 0B Less than a second ago
xbq85ym0xjr15586cqv2ift5a true 0B 4 minutes ago
o06iyz2tanytx7w8ubdji5bg9 true 0B Less than a second ago
0ojdq03b6ea9kwf9o0nb8jwzb true 202.8MB Less than a second ago
dtvkvv11sg1hrp2rg2woswn5i true 0B Less than a second ago
ouxss8x58i85neaa8koo6m5gn true 0B Less than a second ago
3vy5ksodbwvz8ng6k7738iz64 true 0B Less than a second ago
xyg3i3w2kk428tj0lbyx810q0 true 0B Less than a second ago
tjz2rhflh7idt39c7t0r4emay true 0B Less than a second ago
f04k1syqawnuhuxho7vspyrhy true 0B 4 minutes ago
syvkxjuikfbogvxlvhaf2ukvc true 0B Less than a second ago
q3rkbomf3gph0lcxyq4fw8vk0 true 0B Less than a second ago
p1vtpg8dwv3dl0gks599kfhb8 true 0B 4 minutes ago
uyszqp6ryfp8tjd2sb0vmnvey true 0B Less than a second ago
pcvmpqo278wvzpbjiywnagdbs true 0B Less than a second ago
ptwbi7lrp0yotvhc7bfeg2kr9 true 0B Less than a second ago
ltxfrhokd5bi8p3akycebg99z true 0B 4 minutes ago
bvqp9a881fhgr58tc43gb85lp true 0B Less than a second ago
j1tp5u6ehchvwmh819mhb8rpr true 0B Less than a second ago
sy49iggeyeh2l888mwq92cr40 true 0B Less than a second ago
e4mj1qy06g8o241yim4ihopkz true 0B 4 minutes ago
lsv13yco0gdrl1kfe6uokhi0s true 0B Less than a second ago
igp7tw7qmexat0jgzrct7fvvs true 0B Less than a second ago
ldqrq4111qz9ptr87y49ohwua true 0B Less than a second ago
Total:1.736GB
[root@host1 multi-build]# systemctl daemon-reload
[root@host1 multi-build]# systemctl restart docker
[root@host1 multi-build]# docker build -t go-hello-world .
[+] Building 73.0s (12/12) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 293B 0.0s=> resolve image config for docker-image://docker.io/docker/dockerfile:1 5.6s=> docker-image://docker.io/docker/dockerfile:1@sha256:dabfc0969b935b2080555ace70ee69a5261af8 2.8s=> => resolve docker.io/docker/dockerfile:1@sha256:dabfc0969b935b2080555ace70ee69a5261af8a8f1 0.0s=> => sha256:dabfc0969b935b2080555ace70ee69a5261af8a8f1b4df97b9e7fbcf6722eddf 8.40kB / 8.40kB 0.0s=> => sha256:6ce6616ec35021075551c55e604374e8b47a66d157bc55ec2f2d2326450a9ae1 850B / 850B 0.0s=> => sha256:7b1f115048803b89291c08409139f1390862db2244afc3f74e634bbbbe78129c 1.33kB / 1.33kB 0.0s=> => sha256:981cf2e35385187cc01dcd144cd31e3ce126a4fffcb9ac2c0b7f43280ecff3 14.12MB / 14.12MB 2.2s=> => extracting sha256:981cf2e35385187cc01dcd144cd31e3ce126a4fffcb9ac2c0b7f43280ecff33c 0.5s=> [internal] load metadata for docker.io/library/golang:1.21 4.8s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [build 1/4] FROM docker.io/library/golang:1.21@sha256:4746d26432a9117a5f58e95cb9f954ddf0d 48.1s=> => resolve docker.io/library/golang:1.21@sha256:4746d26432a9117a5f58e95cb9f954ddf0de128e9d 0.0s=> => sha256:4746d26432a9117a5f58e95cb9f954ddf0de128e9d5816886514199316e4a2fb 9.75kB / 9.75kB 0.0s=> => sha256:e6ba96dde4af9f4e06caba475fa65e94e4c54fe4aa3e9f4f504c02178eb8934e 2.32kB / 2.32kB 0.0s=> => sha256:246ea1ed9cdb1164bb5cb7e1f45d7914b98c6d9418c8e1cc443105e820bbd9d1 2.82kB / 2.82kB 0.0s=> => sha256:3cbbe86a28c2f6b3c3e0e8c6dcfba369e1ea656cf8daf69be789e0fe210598 24.05MB / 24.05MB 5.2s=> => sha256:903681d87777d28dc56866a07a2774c3fd5bf65fd734b24c9d0ecd9a13c9f6 49.55MB / 49.55MB 4.9s=> => sha256:6ed93aa58a52c9abc1ee472f1ac74b73d3adcccc2c30744498fd5f14f3f5d2 64.14MB / 64.14MB 7.9s=> => sha256:1f46bd02dde39f0741d70614fc607bf03c1a0cd60d52e7dd06c44f8fb9358 92.23MB / 92.23MB 12.6s=> => extracting sha256:903681d87777d28dc56866a07a2774c3fd5bf65fd734b24c9d0ecd9a13c9f636 7.1s=> => sha256:54bf7053e2d96c2c7f4637ad7580bd64345b3c9fabb163e1fdb8894aea8a9 67.01MB / 67.01MB 11.5s=> => sha256:532d43a0bc41875119c835fd75616e87d5df2a3714ddea3a0b5a68c7c5982649 126B / 126B 9.1s=> => sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 32B / 32B 10.1s=> => extracting sha256:3cbbe86a28c2f6b3c3e0e8c6dcfba369e1ea656cf8daf69be789e0fe2105982b 2.0s=> => extracting sha256:6ed93aa58a52c9abc1ee472f1ac74b73d3adcccc2c30744498fd5f14f3f5d22c 8.2s=> => extracting sha256:1f46bd02dde39f0741d70614fc607bf03c1a0cd60d52e7dd06c44f8fb9358709 7.6s=> => extracting sha256:54bf7053e2d96c2c7f4637ad7580bd64345b3c9fabb163e1fdb8894aea8a9af0 16.4s=> => extracting sha256:532d43a0bc41875119c835fd75616e87d5df2a3714ddea3a0b5a68c7c5982649 0.0s=> => extracting sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 0.0s=> [internal] load build context 0.0s=> => transferring context: 165B 0.0s=> [build 2/4] WORKDIR /src 0.7s=> [build 3/4] COPY main.go ./ 0.0s=> [build 4/4] RUN go build -o /bin/hello ./main.go 10.4s=> [stage-1 1/1] COPY --from=build /bin/hello /bin/hello 0.0s=> exporting to image 0.0s=> => exporting layers 0.0s=> => writing image sha256:58acf54b35138f29a417ad2b449ac396f4ca22077307cc81112122c7c9a7ac8d 0.0s=> => naming to docker.io/library/go-hello-world
处理构建警告(非必需,仅优化语法风格)
警告信息 FromAsCasing: 'as' and 'FROM' keywords' casing do not match
是 Docker 语法检查的 “风格提示”——Docker 推荐 AS
关键字使用大写(与 FROM
大小写保持一致),但小写 as
仍能正常运行,不影响功能。
5.运行这个新镜像,启动一个容器
[root@host1 multi-build]# docker run --rm go-hello-world
Hello World!