一、怎么去优化小镜像
### --- 使用多阶段构建,使用多个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 .