#######减小镜像尺寸有两种方法
- 使用链式指令
- 分离编译镜像和部署镜像
<h6>使用链式指令&&</h6>

个人觉得镜像会变大的其中一个原因就是元数据的大量化,试想一下对于每个指令的执行,docker都会为该指令所在的层次打上元数据标签。#####################即对于每一条指令都会打上元数据,镜像总和=元数据总和+每一层镜像总和
试想最坏的情况,待到有很多条构建指令时,且每一条构建语句中只有一条构建指令,那样的元数据总和就会变得非常大了
######################为了解决这个问题,我们使用&&进行链式指令,即将多条指令使用&&链接起来,使得一条构建语句中包含多个执行指令,这样的话能够有效减少元数据总和
当然还有一点不得不提的是:################为了整个镜像层尺寸,清除操作应该在同一层中执行,即执行完当前指令需要清理环境时尽量在当前同一层使用&&进行清除之前使用的环境
<h6>分离编译镜像和部署镜像</h6>
书中提到,docker镜像的另一类无用文件就是编译过程中的依赖文件,例如在编译过程中应用到的依赖的源代码库,如编译文件和头文件。一旦应用程序编译完毕,这些文件就不再用到了。
书中提到个例子:
mkdir ~/docker/goapp
cd ~/docker/goapp
创建两个文件hello.go和Dockerfile

vim hello.go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter,r *http.Request) {
fmt.Fprintf(w,"hello docker for go")
}
func main(){
http.HandleFunc("/",handler)
http.ListenAndServe(":8080",nil)
}

vim Dockerfile
FROM golang:1.4.2
ADD hello.go hello.go
RUN go build hello.go
EXPOSE 8080
ENTRYPOINT ["./hello"]

然后开始编译这个镜像$ docker build -t go/largeapp .

#############################整个镜像569MB大小
运行镜像容器:$docker run --name golargeapp -d -p 8080:8080 go/largeapp
对比实际运行时的应用程序尺寸$ docker exec -it golargeapp /bin/ls -lh

#############################查看到go的可执行文件只用5.6M.png
#############可以看出相差了100多倍的大小,依赖环境占了99%,真是可怕!
为此,我们可以选择优化这个最终的Docker镜像打包最后的hello可执行文件(即应用程序)和相关依赖包 部署到生产环境
1.首先复制容器中的可执行文件到宿主机中
1.1 创建build文件夹$mkdir ~/docker/goapp/build
1.2 复制hello $ cd ~/docker/goapp/
$docker cp -L golargeapp:/go/hello ./build
2.添加运行时的依赖环境(静态库等)使程序能够直接运行。
2.1 首先查看运行该程序需要哪些静态库$ docker exec -it golargeapp /usr/bin/ldd hello

linux-vdso.so.1 (0x00007ffc26796000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f501a17f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5019dd6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f501a39c000)
2.2 从容器中复制这些静态库
$docker cp -L golargeapp:/lib/x86_64-linux-gnu/libpthread.so.0 ./build
$docker cp -L golargeapp:/lib/x86_64-linux-gnu/libc.so.6 ./build
$docker cp -L golargeapp:/lib64/ld-linux-x86-64.so.2 ./build

3.到~/docker/goapp/build目录去新建一个Dockerfile
$ cd ./build
$ vim Dockerfile
FROM scratch
这个是只有二进制的镜像
ADD hello /app/hello
ADD libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0
ADD libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
ADD ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
EXPOSE 8080
ENTRYPOINT ["/app/hello"]


编译构建镜像
$ docker build -t gobinaryapp .

##################构建镜像并查看到构建的镜像只有7.85MB,相比之前的569MB,镜像足足小了100倍
###########小小对比一下效果
先运行之前的镜像

在运行后来编译的二进制镜像

可见应用程序效果一样,镜像大小却相差百倍呢~~>
网友评论