|NO.Z.00116|——————————|^^ 制作 ^^|——|KuberNetes&Docker操作.V14|——|docker.v04|基于scratch制作小镜像|

一、基于scratch创建镜像小镜像
### --- 下载scratch镜像版本包
~~~     可以查看到是没有这个镜像的,但是它可以被直接拿来用的。

[root@k8s-master01 dockerfiles]# docker pull scratch
Using default tag: latest
Error response from daemon: 'scratch' is a reserved name
二、基于scratch制作镜像:创建dockerfile
### --- 创建dockerfile

[root@k8s-master01 dockerfiles]# vim Dockerfile 
#build step
FROM golang:1.14.4-alpine as builder

WORKDIR /opt

COPY main.go /opt

RUN go build /opt/main.go

CMD "./main"

#create real app image

FROM scratch

COPY --from=builder /opt/main /

CMD "./main"
三、基于scratch制作镜像;构建镜像
### --- 构建镜像

[root@k8s-master01 dockerfiles]# docker build -t hello:scratch .
Step 6/8 : FROM scratch
 ---> 
Step 7/8 : COPY --from=builder /opt/main /
 ---> d37fdd61609b
Step 8/8 : CMD "./main"
 ---> Running in 0cf102ab122f
Removing intermediate container 0cf102ab122f
 ---> 34bb3db84f88
Successfully built 34bb3db84f88
Successfully tagged hello:scratch
四、基于scratch制作镜像:查看制作的镜像大小
### --- 查看镜像大小
~~~     镜像是非常小的,大小就是main的大小

[root@k8s-master01 dockerfiles]# docker images
REPOSITORY                                                        TAG                 IMAGE ID       CREATED             SIZE
hello                                                             scratch             34bb3db84f88   20 seconds ago      2.07MB
五、基于scratch制作镜像:运行制作的镜像
### --- 运行制作的镜像
~~~     也是可以执行的
~~~     go语言编译的时候是可以把所有东西都打入到二进制文件里面的,而且没有依赖任何中间库,全是静态的移植,就可以使用scratch
~~~     不管什么语言都会依赖到动态库或者系统环境,若是使用scratch可能会产生一些问题,解决方案,可以吧需要的拷贝到镜像里面。

[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:scratch /main
hello.golang                                                                        

六、镜像做了修改后如何配置:FROM golang:base
### --- 修改镜像dockerfile配置参数
~~~     修改镜像内容:FROM golang:base
~~~     这个镜像做了修改怎么办;若是这个镜像本地就已经有了,但是镜像内容做了修改该怎么办。
~~~     就是需要docker build --pull参数来进行重复它来把修改的内容重新加入

[root@k8s-master01 dockerfiles]# vim Dockerfile
#build step
FROM golang:1.14.4-alpine as builder

WORKDIR /opt

COPY main.go /opt

RUN go build /opt/main.go

CMD "./main"

#create real app image

FROM golang:base

COPY --from=builder /opt/main /

CMD "./main"
### --- 重新build,更新配置参数
~~~     更新配置参数

[root@k8s-master01 dockerfiles]# docker build -t --pull hello:scratch .

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-03-30 11:43  yanqi_vip  阅读(85)  评论(0)    收藏  举报

导航