英文:
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本身,你可以使用多阶段构建。具体步骤如下:
- 在一个已经安装了Git的基于Node的镜像中,克隆Node仓库,并执行
RUN ["npm", "install"]
命令。 - 将第一步构建的结果通过
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论