我如何在Docker容器中持久使用1.11模块? [英] How can I persist go 1.11 modules in a Docker container?

查看:54
本文介绍了我如何在Docker容器中持久使用1.11模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Go 1.10应用程序迁移到Go 1.11.这还包括从 dep 迁移到

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

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

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

自Go 1.11以来,沟渠 GOPATH (当使用模块时)我以为我可以做以下事情:

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

这可行,但是每次我 docker-compose up (或任何其他调用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).

如何改善设置,以便Docker容器保留 go 工具正在下载的模块?

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

推荐答案

有关模块的Wiki文章中未提及此内容,而是阅读了

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.

这意味着对于我的本地开发人员设置,我可以1.在容器中定义 GOPATH 并2.将本地 $ GOPATH/pkg/mod 安装到容器的GOPATH.

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

这篇关于我如何在Docker容器中持久使用1.11模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆