$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wayou/my-app latest f8b6ba45c68f 21 seconds ago 603MB
<none> <none> fe1ff8163c15 23 hours ago 1.44GB
<none> <none> 6e0fb94473a1 3 days ago 1.44GB
######## step 1 ########
FROM node:14-alpine AS BUILD_IMAGE
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
# this is for npm
# COPY package*.json ./
# but we use yarn
COPY ["package.json", "yarn.lock", "./"]
# npm comes with node
# RUN npm install
# if we use yarn, we need firstly install it
# RUN curl -o- -L https://yarnpkg.com/install.sh | bash
# equivalent for `npm ci`, see https://stackoverflow.com/a/58525708/1553656
# If you are building your code for production
# RUN npm ci --only=production
RUN yarn install --frozen-lockfile
# Bundle app source
COPY . .
RUN yarn build
######## step 2 ########
FROM node:14-alpine
WORKDIR /usr/src/app
COPY --from=BUILD_IMAGE /usr/src/app/dist ./dist
COPY --from=BUILD_IMAGE /usr/src/app/node_modules ./node_modules
EXPOSE 3000
CMD [ "node", "dist/main" ]
再次生成镜像后查看大小,又减小了一半,来到 300+M,
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wayou/my-app latest c0ffda4c908a 19 seconds ago 351MB
<none> <none> f8b6ba45c68f 18 minutes ago 603MB
<none> <none> fe1ff8163c15 23 hours ago 1.44GB
优化 node 依赖
去掉 devDependency,更新安装的命令:
- RUN yarn install --frozen-lockfile
+ RUN yarn install --frozen-lockfile --production=true
RUN yarn build
+ # remove development dependencies
+ RUN npm prune --production
再次查看大小,减小了一半多:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wayou/my-app latest 9aba2a428703 5 seconds ago 129MB
<none> <none> c0ffda4c908a 11 minutes ago 351MB
<none> <none> f8b6ba45c68f 29 minutes ago 603MB
<none> <none> fe1ff8163c15 23 hours ago 1.44GB
<none> <none> 6e0fb94473a1 3 days ago 1.44GB
FROM node:14-alpine AS BUILD_IMAGE
# install node-prune (https://github.com/tj/node-prune)
RUN curl -sf https://gobinaries.com/tj/node-prune | sh
# Create app directory
WORKDIR /usr/src/app
…
RUN yarn build
# remove development dependencies
RUN npm prune --production
# run node prune
RUN /usr/local/bin/node-prune
FROM node:14-alpine
WORKDIR /usr/src/app
…
注意,运行上面的 Dockerfile 后会报如下错误:
=> ERROR [build_image 2/9] RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash 0.4s
------
> [build_image 2/9] RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash:
#6 0.366 /bin/sh: curl: not found
#6 0.366 /bin/sh: bash: not found
那是因为切换到 node:12-alpine 镜像后其中不再包含 curl 命令,需要单独安装下:
FROM node:14-alpine AS BUILD_IMAGE
# alpine doesn't come with curl
RUN apk update && apk add curl bash && rm -rf /var/cache/apk/*
# install node-prune (https://github.com/tj/node-prune)
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
# Create app directory
WORKDIR /usr/src/app
…
这次优化后的大小减少了几 M:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wayou/my-app latest 113ca4503190 3 minutes ago 126MB
<none> <none> 9aba2a428703 46 minutes ago 129MB
<none> <none> c0ffda4c908a 57 minutes ago 351MB
<none> <none> f8b6ba45c68f About an hour ago 603MB
<none> <none> fe1ff8163c15 24 hours ago 1.44GB
…
# run node prune
RUN /usr/local/bin/node-prune
# remove unused dependencies
RUN rm -rf node_modules/rxjs/src/
RUN rm -rf node_modules/rxjs/bundles/
RUN rm -rf node_modules/rxjs/_esm5/
RUN rm -rf node_modules/rxjs/_esm2015/
FROM node:14-alpine
…
查看大小,减小了 3M,聊胜于无:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wayou/my-app latest 2c54b4ee27cd 4 seconds ago 123MB
<none> <none> 113ca4503190 9 minutes ago 126MB
<none> <none> 9aba2a428703 52 minutes ago 129MB
<none> <none> c0ffda4c908a About an hour ago 351MB
<none> <none> f8b6ba45c68f About an hour ago 603MB
<none> <none> fe1ff8163c15 24 hours ago 1.44GB
<none> <none> 6e0fb94473a1 3 days ago 1.44GB
使用 Docker 部署 Node 应用 - 镜像文件尺寸的优化
前面 使用 Docker 部署 Node 应用 一文中完成了镜像的创建和运行,不过生成的镜像还有些粗糙,需要进一步优化。
镜像的优化
通过
docker images
看到简单的一个 node 服务端应用,超过 1G 大小,因此需要优化一下使其更加轻量。通过如下命令查看镜像文件里都有什么文件以及分别占用的空间大小:
使用更小的基础镜像
通过 node 在 Docker 市场的界面可看到,其中包含很多可使用的选择,
其中 jessie-, buster- and stretch- 基于 Debian 系统,alpine- 则是 Alpine Linux。一般使用 alpine 即可。
再次查看大小,减少了一半多来到 600+M
多次编译
详情及原理参见 Use multi-stage builds。简单来说,构建镜像文件包含很多步骤,中间步骤依赖使用的东西其实在最后成品中并不需要。
因此,我们可以将整个构建过程分成多步,得到一些中间结果。而这些中间结果是下一步所需要的,但生成这些中间结果的依赖却是下一步不需要的,就可以舍弃。
优化后的 Dockerfile:
再次生成镜像后查看大小,又减小了一半,来到 300+M,
优化 node 依赖
去掉
devDependency
,更新安装的命令:但是,如果编译过程需要编译 TypeScript,跑测试,跑 Lint 等,此方法就不适用,因为这些依赖均在
devDependency
。凡事无绝对,虽然安装时不能去掉,但可以在用完之后去掉。
所以在
yarn build
之后使用npm prune
来手动删除这些无用的依赖:再次查看大小,减小了一半多:
node-prune
使用 node-prune 进一步删除依赖中的无用文件,比如 markdown,测试文件,TypeScript 的类型文件等。
更新后的 Dockerfile:
注意,运行上面的 Dockerfile 后会报如下错误:
那是因为切换到
node:12-alpine
镜像后其中不再包含 curl 命令,需要单独安装下:这次优化后的大小减少了几 M:
手动删除
根据 Honey, I shrunk the node_modules! ...and improved app’s performance in the process 里的介绍再手动删除一些依赖文件:
查看大小,减小了 3M,聊胜于无:
相关资源