前提
减少安装包大小的2个准则:
- 减少镜像层数,层数越多构建时间也会越长
- COPY、ADD和RUN语句都会在镜像中新建一层,层数越多则 image 文件将越大,所以需要尽量减少这三个操作指令
- 基础镜像内不要包含和应用无关的文件,尽量减少单个镜像大小
先下结论
镜像层数从原来的12,优化到了4层。
镜像大小层原来的 189M,优化到了 102 MB
版本一
FROM node:12.18.0-alpine3.10
LABEL MAINTAINER="davis@diancun.net"
# 设置时区
RUN rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN npm config set registry https://registry.npm.taobao.org
RUN mkdir -p /opt/bin/byteark-gateway \
&& mkdir -p /data/byteark-gateway/logs
# 设置环境变量
ENV NODE_ENV=production \
ENABLE_NODE_LOG=YES
WORKDIR /opt/bin/byteark-gateway
# install dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install
# 清除npm缓存文件
# TODO: 为什么要清缓存?
# RUN npm cache clean --force && npm cache verify
COPY . .
EXPOSE 7001
CMD [ "npm", "start" ]
docker build -t byteark .
docker history byteark
IMAGE CREATED CREATED BY SIZE COMMENT
5283ca362af6 8 minutes ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
3ecd78fd410b 8 minutes ago /bin/sh -c #(nop) EXPOSE 7001 0B
b33b0e4ac085 8 minutes ago /bin/sh -c #(nop) COPY dir:5d92d7ad373baf10a… 2.09MB
cd81d2c8b573 8 minutes ago /bin/sh -c npm install 97MB
52ac42ac9feb 8 minutes ago /bin/sh -c #(nop) COPY file:cb9104041f336f56… 546kB
b48e2a832b66 8 minutes ago /bin/sh -c #(nop) COPY file:d0773186f6341f65… 1.8kB
b86df8c5ab16 8 minutes ago /bin/sh -c #(nop) WORKDIR /opt/bin/byteark-g… 0B
82f8901df0fa 8 minutes ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
ea12f4631665 8 minutes ago /bin/sh -c mkdir -p /opt/bin/byteark-gateway… 0B
dc891d3264ae 8 minutes ago /bin/sh -c npm config set registry https://r… 96B
fb0e6d9b2d8a 8 minutes ago /bin/sh -c rm -rf /etc/localtime && ln -s /u… 33B
6327e2bcd7ae 8 minutes ago /bin/sh -c #(nop) LABEL MAINTAINER=davis@di… 0B
c8fe5b06428c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
<missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui… 7.62MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
<missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu… 75.9MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
<missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
docker images | grep byteark
byteark latest 5283ca362af6 7 minutes ago 189MB
在基础镜像上构建了 12 层,包的大小为 189MB
版本二
修改内容:减少 RUN 和 COPY 次数
FROM node:12.18.0-alpine3.10
LABEL MAINTAINER="davis@diancun.net"
RUN rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& mkdir -p /opt/bin/byteark-gateway /data/byteark-gateway/logs \
&& npm config set registry https://registry.npm.taobao.org
WORKDIR /opt/bin/byteark-gateway
COPY . .
RUN npm ci --production
ENV NODE_ENV=production \
ENABLE_NODE_LOG=YES
EXPOSE 7001
CMD [ "npm", "start" ]
docker build -t byteark_01 .
docker history byteark_01
af40b5f867bb 37 seconds ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
8caf12e9fb16 37 seconds ago /bin/sh -c #(nop) EXPOSE 7001 0B
b4a1ad488a24 37 seconds ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
5a4b6e94cacd 39 seconds ago /bin/sh -c npm ci --production 96.3MB
c774b1ee3c60 53 seconds ago /bin/sh -c #(nop) COPY dir:ac52b17cc6eeff9fb… 2.09MB
a69423b07f99 53 seconds ago /bin/sh -c #(nop) WORKDIR /opt/bin/byteark-g… 0B
c99a852da585 53 seconds ago /bin/sh -c rm -rf /etc/localtime && ln -… 129B
6327e2bcd7ae 15 minutes ago /bin/sh -c #(nop) LABEL MAINTAINER=davis@di… 0B
c8fe5b06428c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
<missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui… 7.62MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
<missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu… 75.9MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
<missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
docker images | grep byteark_01
byteark_01 latest af40b5f867bb About a minute ago 188MB
在基础镜像上构建了 8层,包的大小为 188MB
版本三
修改内容:减少基础镜像大小
FROM node:12.18.0-alpine as build
LABEL MAINTAINER="davis@diancun.net"
RUN rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& mkdir -p /opt/bin/byteark-gateway /data/byteark-gateway/logs \
&& npm config set registry https://registry.npm.taobao.org
WORKDIR /opt/bin/byteark-gateway
COPY . .
RUN npm ci --production
FROM node:12.18.0-alpine
COPY --from=build /opt/bin/byteark-gateway /
ENV NODE_ENV=production \
ENABLE_NODE_LOG=YES
EXPOSE 7001
CMD [ "npm", "start" ]
docker build -t byteark_02
docker history byteark_02
IMAGE CREATED CREATED BY SIZE COMMENT
2c56beffd99f 57 seconds ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
29fb1f985600 57 seconds ago /bin/sh -c #(nop) EXPOSE 7001 0B
11b8de95c958 58 seconds ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
59ed505e78cf About a minute ago /bin/sh -c #(nop) COPY dir:e9b44ee6cc66f3d42… 69.5MB
1c342643aa5c 7 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 7 weeks ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B
<missing> 7 weeks ago /bin/sh -c apk add --no-cache --virtual .bui… 7.62MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.4 0B
<missing> 7 weeks ago /bin/sh -c addgroup -g 1000 node && addu… 76.2MB
<missing> 7 weeks ago /bin/sh -c #(nop) ENV NODE_VERSION=12.18.0 0B
<missing> 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:b91adb67b670d3a6f… 5.61MB
docker images | grep byteark_02
byteark_02 latest 2c56beffd99f 16 seconds ago 159MB
在基础镜像上构建了 4层,包的大小为 159 MB
版本四
修改内容:使用空镜像,所有依赖手动安装
FROM alpine:3.10
LABEL MAINTAINER="davis@diancun.net"
WORKDIR /app
COPY . .
RUN rm -rf /etc/localtime && \
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN apk add nodejs npm
RUN npm config set registry https://registry.npm.taobao.org && \
npm ci --production
FROM alpine:3.10
COPY --from=0 /app /app
RUN apk add --no-cache nodejs
ENV NODE_ENV=production ENABLE_NODE_LOG=YES
EXPOSE 7001
CMD [ "npm", "start" ]
docker history byteark_03
IMAGE CREATED CREATED BY SIZE COMMENT
43cb7a0abdca 4 minutes ago /bin/sh -c #(nop) CMD ["npm" "start"] 0B
5f53d382e93d 4 minutes ago /bin/sh -c #(nop) EXPOSE 7001 0B
ad7d17d7e5df 4 minutes ago /bin/sh -c #(nop) ENV NODE_ENV=production E… 0B
f75ed41ebd75 4 minutes ago /bin/sh -c apk add --no-cache nodejs 27.3MB
48bd277254e7 15 minutes ago /bin/sh -c #(nop) COPY dir:daa19cfcd84ee8f85… 69.5MB
be4e4bea2c2e 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:66a440394c2442570… 5.58MB
docker images | grep byteark_03
byteark_03 latest 43cb7a0abdca 5 minutes ago 102MB
在基础镜像上构建了 4层,包的大小为 102 MB