简体   繁体   English

如何使用 docker slim 或其他替代方案正确最小化 nestjs / nodejs 应用程序?

[英]How to minimize properly a nestjs / nodejs application with docker slim or other alternatives?

I am trying to improve my docker images by minimizing as much as possible their size.我正在尝试通过尽可能减小它们的大小来改进我的 docker 图像。 In order to do so I did the follow steps with a nestjs example as a use case:为了做到这一点,我以nestjs示例作为用例执行了以下步骤:

nest new testing-docker-slim

and then inside of it I created the following docker file, with multistage and also some differences on npm installation of packages in between production and development:然后在其中我创建了以下 docker 文件,在生产和开发之间使用多阶段以及 npm 安装包的一些差异:

FROM node:17-alpine as base 
 
FROM base as development 
WORKDIR /app 
COPY . . 
RUN npm i 
CMD ["sh", "-c", "npm run start:dev"] 
 
FROM base as staging 
WORKDIR /app 
COPY . . 
RUN npm i 
RUN npm run build 
CMD ["sh", "-c", "npm run start:prod"] 
 
FROM base as production 
WORKDIR /app 
COPY --from=staging /app/dist ./dist 
COPY --from=staging /app/package.json /app/package-lock.json ./ 
RUN npm i --production 
CMD ["sh", "-c", "npm run start:prod"]

Added a docker ignore file with the following entries:添加了带有以下条目的 docker 忽略文件:

node_modules
.git
.idea

Created a docker images with the following command:使用以下命令创建了 docker 映像:

docker build . --target production -t testing-docker-slim 

And also with this command another images:并且还使用此命令另一个图像:

docker build . --target development -t testing-docker-slim-dev

The images have this size:图像具有以下尺寸:

testing-docker-slim-dev    latest       4408308966c3   22 minutes ago      471MB
testing-docker-slim    latest       c688ff1bedc2   22 minutes ago      185MB

So we already have a reasonable improvement on size of image from 471Mb to 185Mb... Still I did a bit more of research and found out about 2 tools, dive and docker slim .所以我们已经对图像大小从 471Mb 到 185Mb 进行了合理的改进......但我还是做了更多的研究,发现了大约 2 个工具, divedocker slim

dive in both images says that it is a 99% image efficiency score... so did not found out much with it that I could do with the image to improve.在两张图片中潜水都说它是 99% 的图像效率得分......所以没有发现太多我可以用它来改进图像。

then I saw docker slim which claims to have an extraordinary level of compression of images, and I run the following command:然后我看到 docker slim 声称具有非凡的图像压缩水平,我运行以下命令:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/docker-slim build testing-docker-slim --expose 3000 --http-probe=false --continue-after=1

creating a beautiful image of this size:创建一个这样大小的漂亮图像:

testing-docker-slim.slim    latest       99a51a88a0f2   9 minutes ago       89.9MB

this size of image is a reasonable one but when I run the image to see if it is working as expected, it crashes with this error:这种大小的图像是合理的,但是当我运行图像以查看它是否按预期工作时,它会因以下错误而崩溃:

> testing-docker-slim@0.0.1 start:prod
> node dist/main

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'iterare'
Require stack:
- /app/node_modules/@nestjs/common/pipes/validation.pipe.js
- /app/node_modules/@nestjs/common/pipes/parse-array.pipe.js
- /app/node_modules/@nestjs/common/pipes/index.js
- /app/node_modules/@nestjs/common/index.js
- /app/node_modules/@nestjs/core/discovery/discovery-module.js
- /app/node_modules/@nestjs/core/discovery/index.js
- /app/node_modules/@nestjs/core/index.js
- /app/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/app/node_modules/@nestjs/common/pipes/validation.pipe.js:5:19)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/node_modules/@nestjs/common/pipes/validation.pipe.js',
    '/app/node_modules/@nestjs/common/pipes/parse-array.pipe.js',
    '/app/node_modules/@nestjs/common/pipes/index.js',
    '/app/node_modules/@nestjs/common/index.js',
    '/app/node_modules/@nestjs/core/discovery/discovery-module.js',
    '/app/node_modules/@nestjs/core/discovery/index.js',
    '/app/node_modules/@nestjs/core/index.js',
    '/app/dist/main.js'
  ]
}

Node.js v17.4.0

the same happens when I use docker slim on the multistage for development, but the docker multistage without using docker slim works perfectly fine.当我在多级上使用 docker slim 进行开发时,也会发生同样的情况,但是不使用 docker slim 的 docker 多级工作得很好。

Did you use docker multistage with docker slim for production stage with a nodejs if possible nestjs application?如果可能的话,您是否将 docker 多级和docker slim用于带有 nodejs 的生产阶段? Do you have any further advice to improve image size of a docker nodejs / nestjs application?您对提高 docker nodejs / nestjs 应用程序的图像大小有任何进一步的建议吗?

As mentioned by the previous comments I took into consideration the Dockerfile of this template https://github.com/jmcdo29/nest-docker-template Some of the key points on it were the order of the stages + the node prune created by TJ.正如前面的评论所提到的,我考虑了这个模板的Dockerfile https://github.com/jmcdo29/nest-docker-template 上面的一些关键点是阶段的顺序 + TJ 创建的节点修剪.

Last but not least for making docker slim to work I needed to add a flag to the line of docker slim that mentions in their documentation to make it work:最后但并非最不重要的是,为了使 docker slim 工作,我需要在 docker slim 的行中添加一个标志,该标志在他们的文档中提到以使其工作:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/docker-slim build testing --expose 3000 --http-probe=false --continue-after=1 --include-path=/app

the last flat --include-path=/app did effectively add more size to the slim version of the docker image but made it work as expected, making a good gain on size overall.最后一个平面--include-path=/app确实有效地为 docker 图像的超薄版本增加了更多尺寸,但使其按预期工作,整体尺寸得到了很好的提升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM