「CONTAINER-IMAGE」- 镜像大小优化

构建产出的镜像较大,改如何排查?

通过 docker history 命令,查看镜像个构建历史。

示例如下,我们发现导致镜像增大的指令:

# docker history 0xa0000/proxy_exporter:0.0.5
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
6e5fb0db3577        4 hours ago         /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "\"py…   0B
40a3b4bd0dc9        4 hours ago         /bin/sh -c #(nop)  ENV PORT=1923                0B
f926695ca75f        4 hours ago         /bin/sh -c pip3 install -r requirements.txt     7.08MB
006c03ee97e5        4 hours ago         /bin/sh -c #(nop) ADD multi:d3ed25191265c52e…   19.1kB
a9c8c4ce9fcf        4 hours ago         /bin/sh -c #(nop) ADD file:6eade3ef219d74a8b…   14.2MB
8a7453e4653c        4 hours ago         /bin/sh -c apt-get update && apt-get install…   403MB
b96ea74726a8        4 hours ago         /bin/sh -c #(nop) WORKDIR /config               0B
56def654ec22        7 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           7 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B
<missing>           7 weeks ago         /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B
<missing>           7 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B
<missing>           7 weeks ago         /bin/sh -c #(nop) ADD file:4974bb5483c392fb5…   63.2MB

构建更小的镜像

选择更小的基础镜像

for Java

  • OpenJDK 镜像,最小 200M+ 空间。
  • application.jar 文件,占用 200M+ 空间。