dongmou9260
2017-08-25 06:18
浏览 205

获取Docker内部所有Go依赖包

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.

图片转代码服务由CSDN问答提供 功能建议

当我执行以下操作时,我可以获得所有依赖包:

   $ cd myrepo 
 $ go -d。/ ... 
   
 
 

但是在docker中做到这一点的最佳方法是什么? 我不希望所有回购都在我的docker-app中,因此我正在寻找一种方法可以在docker容器中将其执行到某个卷(或类似的东西)并重复使用。 < p>我在想类似的东西。 Dockerfile

  FROM golang:1.8 
 
WORKDIR / app 
ADD ./src
   
 
 

构建映像

  $ docker build -t myapp。
 
 $ docker run myapp go get -d。/ ... 
 <  / code>  
 
 

如何重用依赖项,而不必每次都提取/下载它们? 我希望它们有一定的体积。 我有点了解docker卷,但是在这种情况下我不知道如何使用它。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpang1898 2017-08-25 09:03
    已采纳

    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/

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题