Go Lang Docker镜像与Node JS代码/生成器的依赖关系

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

Go Lang Docker Image with Dependencies on Node JS Code/Generators

问题

你好,我有一个GoLang应用程序,需要打包成Docker镜像。现在,这个应用程序依赖于Node.js包,在同一个Docker实例上通过npm安装后需要在本地运行。这些包/生成器保存在一个不同的git仓库中,但在创建GoLang Docker镜像时必须可用。目前,我们将这些Node文件以zip格式包含在Golang代码中,并在docker文件中包含所需的安装说明以包含这些NPM依赖项。

但是,我们现在希望自动化docker(Golang)镜像的创建,并寻找在创建Golang镜像时动态包含这些nodejs依赖项的最佳方法,而不是将它们作为zip文件重复包含。有什么好的解决方法吗?任何信息都将很有帮助。

谢谢,
Aakash

英文:

Hi I am having a GoLang application that needs to be packaged in to a docker image. Now, this application depends on Node JS Packages that are to be run locally after npm installs on same docker instance. These packages/generators are maintained in a different git repository but have to be available when we create GoLang Docker image. Right now we include these Node Files in a zip format as part of Golang Code and have included the required installations instructions as part of docker file to include these NPM dependencies.

But, we are now looking to automate the docker(Golang) image creation and looking at best way to include these nodejs dependencies dynamically while creating Golang image and not duplicate them as a zip file. What is the best way to address. Any information will be helpful.

thanks,
Aakash

答案1

得分: 0

如果你的最终镜像不需要Git本身,你可以使用多阶段构建。具体步骤如下:

  1. 在一个已经安装了Git的基于Node的镜像中,克隆Node仓库,并执行RUN ["npm", "install"]命令。
  2. 将第一步构建的结果通过COPY --from=builder /root/ ./命令复制到第二个基于Go的镜像中。

这样做的目的是得到一个只包含所需内容的最终镜像。

参考链接:使用多阶段构建

英文:

If your final image does not need Git itself, you could use a multi-stage build in which:

  • you clone the Node repository (using a node-based image with git installed) and do your RUN ["npm", "install"] there
  • you COPY --from=builder /root/ ./ (copy from the first image) the result of that build to your second image (based on Go)

The idea is to get a final image with only what you need.

huangapple
  • 本文由 发表于 2022年11月24日 14:34:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/74556509.html
匿名

发表评论

匿名网友

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

确定