$ docker build -f Dockerfile -t zhaogaolong/centos-tools:v1 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:centos7.6.1810 ---> f1cb7c7d58b7 Step 2/4 : RUN yum makecache ---> Using cache ---> a9bebba7b06e Step 3/4 : RUN yum install vim net-tools dstat htop -y -q ---> Running in 162917d77b35 warning: /var/cache/yum/x86_64/7/base/packages/gpm-libs-1.20.7-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for gpm-libs-1.20.7-6.el7.x86_64.rpm is not installed Public key for perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm is not installed Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 install-info: No such file or directory for /usr/share/info/which.info.gz Removing intermediate container 162917d77b35 ---> c866064ab1b2 Step 4/4 : RUN yum clean all ---> Running in 17aaa7f6e6cb Loaded plugins: fastestmirror, ovl Cleaning repos: base extras updates Cleaning up list of fastest mirrors Removing intermediate container 17aaa7f6e6cb ---> a5181d724237 Successfully built a5181d724237 Successfully tagged zhaogaolong/centos-tools:v2
前后对比一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 基础镜像 $ docker image ls centos:centos7.6.1810 REPOSITORY TAG IMAGE ID CREATED SIZE centos centos7.6.1810 f1cb7c7d58b7 2 years ago 202MB
➜ docker image ls zhaogaolong/centos-tools:v1 REPOSITORY TAG IMAGE ID CREATED SIZE zhaogaolong/centos-tools v2 a5181d724237 About a minute ago 633MB
➜ docker image history zhaogaolong/centos-tools:v1 IMAGE CREATED CREATED BY SIZE COMMENT a5181d724237 4 minutes ago /bin/sh -c yum clean all 23.3MB c866064ab1b2 4 minutes ago /bin/sh -c yum install vim net-tools dstat h… 167MB a9bebba7b06e 7 minutes ago /bin/sh -c yum makecache 242MB f1cb7c7d58b7 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 2 years ago /bin/sh -c #(nop) ADD file:54b004357379717df… 202MB
FROM centos:centos7.6.1810 RUN yum makecache && yum install vim net-tools dstat htop -y && yum clean all
重新执行 docker build
1 2 3 4 5 6 7 8 9 10 11 12 13
$ docker image ls centos:centos7.6.1810 REPOSITORY TAG IMAGE ID CREATED SIZE centos centos7.6.1810 f1cb7c7d58b7 2 years ago 202MB $ docker image ls zhaogaolong/centos-tools:v2 REPOSITORY TAG IMAGE ID CREATED SIZE zhaogaolong/centos-tools v2 54026c4cd698 2 minutes ago 282MB
$ docker image history zhaogaolong/centos-tools:v2 IMAGE CREATED CREATED BY SIZE COMMENT 54026c4cd698 3 minutes ago /bin/sh -c yum makecache && yum install vim … 80.1MB f1cb7c7d58b7 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 2 years ago /bin/sh -c #(nop) ADD file:54b004357379717df… 202MB
FROM golang:1.15.6-buster as builder WORKDIR /workspace ENV GOPROXY https://goproxy.io,direct COPY . . RUN go mod vendor -v && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o server main.go
1 2 3 4 5 6 7 8 9
$ docker build -f Dockerfile -t zhaogaolong/latency:v1 . --no-cache $ docker image ls golang:1.15.6-buster REPOSITORY TAG IMAGE ID CREATED SIZE golang 1.15.6-buster 5f46b413e8f5 3 months ago 839MB
$ docker image ls zhaogaolong/latency:v1 REPOSITORY TAG IMAGE ID CREATED SIZE zhaogaolong/latency v1 141807be4b7a 11 minutes ago 890MB
想法:如果可以把二进制包 copy 到另外一个全新的环境中运行,而废弃构建的镜像。确实可以通过 copy from 的解决方案解决。
产出转移(copy from)
COPY FROM 是可以在构建的的过程中从临时镜像把产出(artifact)copy 到另外一个镜像里,我成前一个镜像为 build 镜像,后者为 release 镜像。
docker file 稍微修改一下
1 2 3 4 5 6 7 8 9 10 11 12
FROM golang:1.15.6-buster as builder WORKDIR /workspace ENV GOPROXY https://goproxy.io,direct COPY . . RUN go mod vendor -v RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o server main.go
# add release docker image FROM centos:7.9.2009 as runtime WORKDIR / COPY --from=builder /workspace/server /server ENTRYPOINT ["bash", "-c", "/server"]
$ docker image ls zhaogaolong/latency REPOSITORY TAG IMAGE ID CREATED SIZE REPOSITORY TAG IMAGE ID CREATED SIZE zhaogaolong/latency v2 d0bed0af30d7 7 minutes ago 210MB zhaogaolong/latency v1 141807be4b7a 17 minutes ago 890MB
FROM golang:1.15.6-buster as builder WORKDIR /workspace ENV GOPROXY https://goproxy.io,direct COPY . . RUN go mod vendor -v RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o server main.go
FROM centos:7.9.2009 as runtime WORKDIR / RUN yum makecache && yum install iproute -y -q && yum clean all # install iproute COPY --from=builder /workspace/server /server ENTRYPOINT ["bash", "-c", "/server"]
开始执行构建
1 2 3 4 5 6
$ docker build -f Dockerfile -t zhaogaolong/latency:v3 . --no-cache $ docker image ls zhaogaolong/latency REPOSITORY TAG IMAGE ID CREATED SIZE zhaogaolong/latency v3 12eb5e19b424 25 seconds ago 238MB zhaogaolong/latency v2 d0bed0af30d7 17 minutes ago 210MB zhaogaolong/latency v1 141807be4b7a 27 minutes ago 890MB
FROM golang:1.15.6-buster as builder WORKDIR /workspace ENV GOPROXY https://goproxy.io,direct COPY . . RUN go mod vendor -v RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o server main.go
FROM centos:7.9.2009 as runtime WORKDIR / RUN yum makecache RUN yum install iproute -y -q RUN yum clean all COPY --from=builder /workspace/server /server ENTRYPOINT ["bash", "-c", "/server"]