英文:
Get all dependent packages of Go inside docker
问题
我可以在执行以下操作时获取所有依赖包:
$ cd myrepo
$ go get -d ./...
但在Docker中,有什么最好的方法可以做到这一点呢?我不想将所有的仓库都放在我的Docker应用程序中,所以我正在寻找一种在Docker容器内执行此操作并将其重用到某个卷(或类似的东西)的方法。
我考虑了以下内容:
Dockerfile
FROM golang:1.8
WORKDIR /app
ADD ./src
构建镜像
$ docker build -t myapp .
$ docker run myapp go get -d ./...
如何在不每次都拉取/下载依赖项的情况下重用它们?我希望它们存在于某种卷中。我对Docker卷有一些了解,但不知道如何在这种情况下使用它。
英文:
I can get all my dependent packages when I do the following:
$ cd myrepo
$ go get -d ./...
But what is the best way to do this in docker? I don't want all the repo's inside my docker-app so I'm searching for a way to execute this inside a docker container to some volume (or something like that) and reuse it.
I was thinking about something like.
Dockerfile
FROM golang:1.8
WORKDIR /app
ADD ./src
Build image
$ docker build -t myapp .
$ docker run myapp go get -d ./...
How can I reuse the dependencies without pulling/downloading them every time? I want them in a sort of volume. I know docker volumes a bit but I don't know how to use it in this case.
答案1
得分: 4
这样做的最佳方式是使用构建器模式。
在第一张图片中,您下载所有依赖项并构建可执行文件。
在第二张图片中,您将可执行文件复制到一个新的镜像中。您可以使用scratch镜像作为基础,但alpine可能更好,因为它也很小,但提供了一个用于https证书等内容的shell和包管理器。
如果您首先复制源代码,然后安装依赖项,那么每次源代码更改时都会重新下载依赖项,因此最好使用类似go dep的工具。使用它,您可以复制Gopkg文件,安装依赖项,然后复制源代码。
官方文章链接:https://docs.docker.com/engine/userguide/eng-image/multistage-build/
更详细的信息请参考:https://blog.alexellis.io/mutli-stage-docker-builds/
英文:
The best way to do this is to use the builder pattern.
In the first image you download all the dependencies and build the executable.
In the second you copy the executable into a new image. You can use the scratch image as a base, but alpine is maybe better because it is also small but provides a shell and a packet manager for something like certificates for https.
If you first copy the sources and the install the dependencies, they will be downloaded with every source change, so it ist better to use something like go dep. With it you can copy the Gopkg file, install your dependencies and then copy the sources.
Official article https://docs.docker.com/engine/userguide/eng-image/multistage-build/
and in more detail
https://blog.alexellis.io/mutli-stage-docker-builds/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论