如何优化Docker镜像大小并减少推送到Harbor的时间?

huangapple go评论88阅读模式
英文:

How to Optimize Docker Image Size and Reduce Push Time to Harbor?

问题

我目前在我的公司中使用Harbor作为Docker镜像配置管理工具。在在Linux服务器上运行Next.js Docker镜像之前,我会将Docker镜像推送到Harbor,然后在服务器上执行它。

然而,我面临一个问题,即Next.js Docker镜像中的node_modules文件大小和Docker镜像的总体大小都很大。结果,将镜像推送到Harbor需要很长时间,平均超过8分钟。

为了减小镜像大小,我已尝试减少运行步骤并使用技巧来优化项目,比如使用构建器,运行 'npm install --production',以及删除 '.next/cache' 目录。然而,这些努力并没有显著解决问题。

团队坚持要减小镜像大小,因为构建时间很长。然而,根据我的当前知识,我认为这个优化水平已经是我能达到的最好水平了。

如果有人遇到类似的问题,我会非常感谢他们提供的任何提示或解决方法。

谢谢。

附言:我不擅长英语,所以我将其翻译成了gpt。可能存在不正确的语法。

我想加快镜像构建速度并缩短推送到Harbor的时间。

英文:

I am currently using Harbor as a Docker image configuration management tool in my company. Before running the Next.js Docker image on the Linux server, I push the Docker image to Harbor and then execute it on the server.

However, I am facing a problem where both the size of the node_modules file in the Next.js Docker image and the overall size of the Docker image are large. As a result, it takes a long time to push the image to Harbor, averaging more than 8 minutes.

To reduce the image size, I have tried reducing the running steps and optimizing the project using techniques like using a builder, running 'npm install --production', and removing the '.next/cache' directory. However, these efforts have not significantly resolved the issue.

The team keeps insisting on reducing the image size due to the lengthy build time. However, based on my current knowledge, I believe that this level of optimization is the best I can achieve.

If anyone has encountered a similar problem, I would appreciate any hints or workarounds they can provide.

Thank you.

ps: I don't speak English well, so I translated it to gpt. There may be incorrect grammar.

I want to speed up image builds and reduce time to push to harbor.

答案1

得分: 1

以下是要翻译的内容:

这是您需要执行的5个步骤:

  1. 使用最小的基础映像(阅读 https://snyk.io/blog/choosing-the-best-node-js-docker-image/
  2. 您应该使用多阶段构建(multistage builds)
  3. 使用 .Dockerignore
  4. 仔细检查依赖项
  5. 使镜像层尽可能小

您绝对应该尝试使用 Dive(https://github.com/wagoodman/dive),它用于探索 Docker 镜像、层内容,并发现缩小 Docker/OCI 镜像大小的方法。

在检查了这些步骤后,无论您使用 Harbor 还是其他任何镜像仓库都不重要。

英文:

These are 5 steps you need to perform:

  1. use minimal base images(read https://snyk.io/blog/choosing-the-best-node-js-docker-image/)
  2. you should use multistagebuilds
  3. use .Dockerignore
  4. Double-check the dependencies
  5. Make image layer as small as possible

You should definetly try Dive(https://github.com/wagoodman/dive),which serves for exploring a docker image, layer contents, and discovering ways to shrink the size of your Docker/OCI image.

After checking these steps it does not matter if your are using Harbor or any other image repository.

huangapple
  • 本文由 发表于 2023年7月17日 09:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定