dtu72460
2018-08-26 12:18
浏览 41

我如何在Docker容器中持久使用1.11模块?

I am migrating a Go 1.10 app to Go 1.11. This also includes migrating from dep to mod for managing dependencies.

As the application depends on a database, I am using a docker-compose to set up the local development environment. With Go 1.10 I simply mounted the local repository (including the vendor folder) into the correct location in the container's GOPATH:

web:
  image: golang:1.10
  working_dir: /go/src/github.com/me/my-project
  volumes:
    - .:/go/src/github.com/me/my-project
  environment:
    - GOPATH=/go
    - PORT=9999
  command: go run cmd/my-project/main.go

Since Go 1.11 ditches GOPATH (when using modules that is) I thought I could just do the following:

web:
  image: golang:1.11rc2
  working_dir: /app
  volumes:
    - .:/app
  environment:
    - PORT=9999
  command: go run cmd/my-project/main.go

This works, but every time I docker-compose up (or any other command that calls through to the Go tool) it will resolve and re-download the dependency tree from scratch. This does not happen (rather only once) when I run the command outside of the container (i.e. on my local OS).

How can I improve the setup so that the Docker container persists the modules being downloaded by the go tool?

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

我正在将Go 1.10应用迁移到Go 1.11。 这还包括从 dep 迁移到 mod 用于管理依赖项。

因为应用程序取决于 在数据库上,我正在使用 docker-compose 设置本地开发环境。 使用Go 1.10,我只需将本地存储库(包括 vendor 文件夹)安装到容器的 GOPATH 中的正确位置:

 < 代码>网络:
图像:golang:1.10 
 working_dir:/go/src/github.com/me/my-project
卷:
-。:/ go / src / github.com / me / my  -project 
环境:
-GOPATH = / go 
-PORT = 9999 
命令:运行cmd / my-project / main.go 
   
 
 

从Go 1.11开始,沟渠 GOPATH (使用模块时)我认为我可以执行以下操作:

  web:
图片:golang  :1.11rc2 
 working_dir:/ app 
卷:
-。:/ app 
环境:
-PORT = 9999 
命令:进入运行cmd / my-project / main.go 
   
 
 

这可行,但是每次我 docker-compose up (或任何其他调用Go工具的命令)时,它将解析并重新下载 依赖树从头开始。 当我在容器外部(即在本地操作系统上)运行命令时,这不会发生(而是仅发生一次)。

如何改善设置以使Docker容器持久保存 go 工具正在下载哪些模块?

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

2条回答 默认 最新

  • dsour68888 2018-08-26 15:12
    已采纳

    This is not mentioned in the wiki article on modules, but from reading the updated docs on the go tool, I found out that when using Go modules, the go tool will still use GOPATH to store the available sources, namely $GOPATH/pkg/mod.

    This means that for my local dev setup, I can 1. define the GOPATH in the container and 2. mount the local $GOPATH/pkg/mod into the container's GOPATH.

    web:
      image: golang:1.11rc2
      working_dir: /app
      volumes:
        - .:/app
        - $GOPATH/pkg/mod:/go/pkg/mod
      environment:
        - GOPATH=/go
        - PORT=9999
      command: go run cmd/my-project/main.go
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanpingzu7194 2018-09-24 17:34

    You can use a volume instead of your local GOPATH. the docker-compose.yml is like:

    version: '3'
    
    services:
      web:
        image: golang:1.11
        working_dir: /app
        volumes:
            - .:/app
            - cache:/go
        environment:
            - PORT=9999
        command: go run cmd/my-project/main.go
    
    volumes:
      cache:
    

    The volume cache is going to persist all the changes on the GOPATH for the container.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题