前提

减少安装包大小的2个准则:

  1. 减少镜像层数,层数越多构建时间也会越长
    1. COPY、ADD和RUN语句都会在镜像中新建一层,层数越多则 image 文件将越大,所以需要尽量减少这三个操作指令
  2. 基础镜像内不要包含和应用无关的文件,尽量减少单个镜像大小

先下结论

镜像层数从原来的12,优化到了4层。

镜像大小层原来的 189M,优化到了 102 MB


版本一

  1. FROM node:12.18.0-alpine3.10
  2. LABEL MAINTAINER="davis@diancun.net"
  3. # 设置时区
  4. RUN rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  5. RUN npm config set registry https://registry.npm.taobao.org
  6. RUN mkdir -p /opt/bin/byteark-gateway \
  7. && mkdir -p /data/byteark-gateway/logs
  8. # 设置环境变量
  9. ENV NODE_ENV=production \
  10. ENABLE_NODE_LOG=YES
  11. WORKDIR /opt/bin/byteark-gateway
  12. # install dependencies
  13. COPY package.json .
  14. COPY package-lock.json .
  15. RUN npm install
  16. # 清除npm缓存文件
  17. # TODO: 为什么要清缓存?
  18. # RUN npm cache clean --force && npm cache verify
  19. COPY . .
  20. EXPOSE 7001
  21. CMD [ "npm", "start" ]
  1. docker build -t byteark .
  2. docker history byteark
  3. IMAGE CREATED CREATED BY SIZE COMMENT
  4. 5283ca362af6 8 minutes ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
  5. 3ecd78fd410b 8 minutes ago /bin/sh -c #(nop) EXPOSE 7001 0B
  6. b33b0e4ac085 8 minutes ago /bin/sh -c #(nop) COPY dir:5d92d7ad373baf10a… 2.09MB
  7. cd81d2c8b573 8 minutes ago /bin/sh -c npm install 97MB
  8. 52ac42ac9feb 8 minutes ago /bin/sh -c #(nop) COPY file:cb9104041f336f56… 546kB
  9. b48e2a832b66 8 minutes ago /bin/sh -c #(nop) COPY file:d0773186f6341f65… 1.8kB
  10. b86df8c5ab16 8 minutes ago /bin/sh -c #(nop) WORKDIR /opt/bin/byteark-g… 0B
  11. 82f8901df0fa 8 minutes ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
  12. ea12f4631665 8 minutes ago /bin/sh -c mkdir -p /opt/bin/byteark-gateway 0B
  13. dc891d3264ae 8 minutes ago /bin/sh -c npm config set registry https://r 96B
  14. fb0e6d9b2d8a 8 minutes ago /bin/sh -c rm -rf /etc/localtime && ln -s /u 33B
  15. 6327e2bcd7ae 8 minutes ago /bin/sh -c #(nop) LABEL MAINTAINER=davis@di… 0B
  16. c8fe5b06428c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
  17. <missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
  18. <missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
  19. <missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui 7.62MB
  20. <missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
  21. <missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu 75.9MB
  22. <missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
  23. <missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
  24. <missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
  25. docker images | grep byteark
  26. byteark latest 5283ca362af6 7 minutes ago 189MB

在基础镜像上构建了 12 层,包的大小为 189MB


版本二

修改内容:减少 RUN 和 COPY 次数

  1. FROM node:12.18.0-alpine3.10
  2. LABEL MAINTAINER="davis@diancun.net"
  3. RUN rm -rf /etc/localtime \
  4. && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  5. && mkdir -p /opt/bin/byteark-gateway /data/byteark-gateway/logs \
  6. && npm config set registry https://registry.npm.taobao.org
  7. WORKDIR /opt/bin/byteark-gateway
  8. COPY . .
  9. RUN npm ci --production
  10. ENV NODE_ENV=production \
  11. ENABLE_NODE_LOG=YES
  12. EXPOSE 7001
  13. CMD [ "npm", "start" ]
  1. docker build -t byteark_01 .
  2. docker history byteark_01
  3. af40b5f867bb 37 seconds ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
  4. 8caf12e9fb16 37 seconds ago /bin/sh -c #(nop) EXPOSE 7001 0B
  5. b4a1ad488a24 37 seconds ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
  6. 5a4b6e94cacd 39 seconds ago /bin/sh -c npm ci --production 96.3MB
  7. c774b1ee3c60 53 seconds ago /bin/sh -c #(nop) COPY dir:ac52b17cc6eeff9fb… 2.09MB
  8. a69423b07f99 53 seconds ago /bin/sh -c #(nop) WORKDIR /opt/bin/byteark-g… 0B
  9. c99a852da585 53 seconds ago /bin/sh -c rm -rf /etc/localtime && ln -… 129B
  10. 6327e2bcd7ae 15 minutes ago /bin/sh -c #(nop) LABEL MAINTAINER=davis@di… 0B
  11. c8fe5b06428c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
  12. <missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
  13. <missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
  14. <missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui 7.62MB
  15. <missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
  16. <missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu 75.9MB
  17. <missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
  18. <missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
  19. <missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
  20. docker images | grep byteark_01
  21. byteark_01 latest af40b5f867bb About a minute ago 188MB

在基础镜像上构建了 8层,包的大小为 188MB


版本三

修改内容:减少基础镜像大小

  1. FROM node:12.18.0-alpine as build
  2. LABEL MAINTAINER="davis@diancun.net"
  3. RUN rm -rf /etc/localtime \
  4. && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  5. && mkdir -p /opt/bin/byteark-gateway /data/byteark-gateway/logs \
  6. && npm config set registry https://registry.npm.taobao.org
  7. WORKDIR /opt/bin/byteark-gateway
  8. COPY . .
  9. RUN npm ci --production
  10. FROM node:12.18.0-alpine
  11. COPY --from=build /opt/bin/byteark-gateway /
  12. ENV NODE_ENV=production \
  13. ENABLE_NODE_LOG=YES
  14. EXPOSE 7001
  15. CMD [ "npm", "start" ]
  1. docker build -t byteark_02
  2. docker history byteark_02
  3. IMAGE CREATED CREATED BY SIZE COMMENT
  4. 2c56beffd99f 57 seconds ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
  5. 29fb1f985600 57 seconds ago /bin/sh -c #(nop) EXPOSE 7001 0B
  6. 11b8de95c958 58 seconds ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
  7. 59ed505e78cf About a minute ago /bin/sh -c #(nop) COPY dir:e9b44ee6cc66f3d42… 69.5MB
  8. 1c342643aa5c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
  9. <missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
  10. <missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
  11. <missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui 7.62MB
  12. <missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
  13. <missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu 76.2MB
  14. <missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
  15. <missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
  16. <missing> 3 months ago /bin/sh -c #(nop) ADD file:b91adb67b670d3a6f… 5.61MB
  17. docker images | grep byteark_02
  18. byteark_02 latest 2c56beffd99f 16 seconds ago 159MB

在基础镜像上构建了 4层,包的大小为 159 MB


版本四

修改内容:使用空镜像,所有依赖手动安装

  1. FROM alpine:3.10
  2. LABEL MAINTAINER="davis@diancun.net"
  3. WORKDIR /app
  4. COPY . .
  5. RUN rm -rf /etc/localtime && \
  6. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  7. RUN apk add nodejs npm
  8. RUN npm config set registry https://registry.npm.taobao.org && \
  9. npm ci --production
  10. FROM alpine:3.10
  11. COPY --from=0 /app /app
  12. RUN apk add --no-cache nodejs
  13. ENV NODE_ENV=production ENABLE_NODE_LOG=YES
  14. EXPOSE 7001
  15. CMD [ "npm", "start" ]
  1. docker history byteark_03
  2. IMAGE CREATED CREATED BY SIZE COMMENT
  3. 43cb7a0abdca 4 minutes ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
  4. 5f53d382e93d 4 minutes ago /bin/sh -c #(nop) EXPOSE 7001 0B
  5. ad7d17d7e5df 4 minutes ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
  6. f75ed41ebd75 4 minutes ago /bin/sh -c apk add --no-cache nodejs 27.3MB
  7. 48bd277254e7 15 minutes ago /bin/sh -c #(nop) COPY dir:daa19cfcd84ee8f85… 69.5MB
  8. be4e4bea2c2e 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
  9. <missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
  10. docker images | grep byteark_03
  11. byteark_03 latest 43cb7a0abdca 5 minutes ago 102MB

在基础镜像上构建了 4层,包的大小为 102 MB