CC00114.CloudKubernetes——|KuberNetes&Docker操作.V12|——|docker.v02|优化小镜像|

一、怎么去优化小镜像
### --- 使用多阶段构建,使用多个FROM参数

~~~     使用多阶段构建:就是多个FROM
~~~     编译操作和生成最终镜像的操作分开
二、下载镜像版本
### --- 使用go语言去做镜像

[root@k8s-master01 dockerfiles]# docker pull golang:1.14.4-alpine
三、配置go参数
### --- 协议个go语言的参数

[root@k8s-master01 dockerfiles]# vim main.go
package main
import "fmt"

func main() {
 fmt.Println("hello.golang")
}
四、单阶段构建—基于alpine:创建dockerfile
### --- 配置dockerfile

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

WORKDIR /opt

COPY main.go /opt

RUN go build /opt/main.go  

CMD "./main"
五、单阶段构建—基于alpine:构建镜像
### --- 构建镜像

[root@k8s-master01 dockerfiles]# docker build -t hello:single_build .
Sending build context to Docker daemon  6.656kB
Step 1/5 : FROM golang:1.14.4-alpine
 ---> 3289bf11c284
Step 2/5 : WORKDIR /opt
 ---> Running in 2e54278dd489
Removing intermediate container 2e54278dd489
 ---> 14621984bf34
Step 3/5 : COPY main.go /opt
 ---> 72a6ef2fd24e
Step 4/5 : RUN go build /opt/main.go
 ---> Running in 27f1de553f68
Removing intermediate container 27f1de553f68
 ---> 7764459737f9
Step 5/5 : CMD "./hello"
 ---> Running in b02772a2178c
Removing intermediate container b02772a2178c
 ---> f0bac85c72b5
Successfully built f0bac85c72b5
Successfully tagged hello:single_build
六、单阶段构建—基于alpine:运行构建的镜像
### --- 进入容器运行镜像

[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build sh
/opt # ls
main     main.go                    // 可以查看到当前目录下已经生成了main文件
[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build
hello.golang                        // 可以查看到它是可以执行main里面的命令的
七、单阶段构建—基于alpine:查看制作的镜像大小
### --- 查看它的镜像包大小
~~~     可以看到生成的这个包是372M,包是非常大的。

[root@k8s-master01 dockerfiles]# docker images
REPOSITORY                                                        TAG             IMAGE ID       CREATED              SIZE
hello                                                             single_build    ba14d9831a6a   About a minute ago   372MB
### --- 查看它的二进制包
~~~     在生产环境下是不可以这样的。二进制包只有2M,而镜像包有372M,是不可以这样部署的。

[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build sh
/opt # ls -lh
total 2M     
-rwxr-xr-x    1 root     root        2.0M Apr 14 07:14 main     // 而它的二进制包只有2M
八、多阶段构建—基于alpine:优化小镜像:在生产环境下,优化镜像大小
### --- 在生产环境下,我们只需要main的二进制包就可以执行我们go的这个环境,设置以下方案

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

WORKDIR /opt

COPY main.go /opt

RUN go build /opt/main.go

CMD "./main"

#create real app image

FROM alpine:3.8

COPY --from=0 /opt/main /

CMD "./opt/main"
九、多阶段构建—基于alpine:制作小镜像:构建镜像
### --- 执行构建镜像

[root@k8s-master01 dockerfiles]# docker build -t hello:alpine .
Step 6/8 : FROM alpine:3.8
 ---> c8bccc0af957
Step 7/8 : COPY --from=0 /opt/main /
 ---> c49e54a6dbb2
Step 8/8 : CMD "./opt/main"
 ---> Running in c07bd08cbcb0
Removing intermediate container c07bd08cbcb0
 ---> 2ec9b3e54af1
Successfully built 2ec9b3e54af1
Successfully tagged hello:alpine
十、多阶段构建—基于alpine:优化小镜像:运行构建的镜像
### --- 运行容器

[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:alpine sh
/ # ls
main                            // 可以看到main已经拷贝过来了
/ # ./main
hello.golang
### --- 查看一下镜像大小
~~~     实现了同样的结果,单阶段和分阶段构建文件大小相差200多MB,
~~~     所以建议采用分阶段构建比较合理;特别是go语言或者编译操作

[root@k8s-master01 dockerfiles]# docker images
REPOSITORY    TAG             IMAGE ID       CREATED          SIZE
hello         alpine          3b4a7c8ca51c   55 seconds ago   6.48MB // 分阶段构建
hello         single_build    ba14d9831a6a   9 minutes ago    372MB  // 单阶段构建
十一、多阶段构建—基于alpine:优化小镜像:编辑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 alpine:3.8

COPY --from=builder /opt/main /                 # 将上面制作的镜像拷贝到/目录下

CMD "./opt/main"
十二、多阶段构建—基于alpine:制作小镜像:运行容器
### --- 运行容器

[root@k8s-master01 dockerfiles]# docker build -t hello:alpine .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值