dtddjq9637 2019-07-19 17:38
浏览 803
已采纳

在Docker中找不到Go mod文件夹

I'm very new to go modules and am trying to take a project and update it to use them as they seem like a really great way to finally handle my dependencies. However, I'm having an issue getting things to build in Docker.

I have a project with a main.go and a go.mod file (which references both private/local repos as well as public ones like go.uber.org/zap). I've named my module github.internal.com/team_name/project_name I've vendored up my dependencies. So, my project structure looks like:

project
- main.go
- internal
  - utils.go
- go.mod (github.internal.com/team_name/project_name)
- vendor
  - go.uber.org
  - private.repo.com
Dockerfile

Then, in main.go I import

import (
    "github.internal.com/team_name/project_name/internal"
    "go.uber.org/zap"
)

All of this works great and I can run go build no problem.

The issue comes when I try to build using Docker. In my Dockerfile I run

ENV GOPATH /go
COPY . /go/src/github.internal.com/team_name/project_name
WORKDIR /go/src/github.internal.com/team_name/project_name
RUN CGO_ENABLED=0 GOOS=linux go build -a -o slaxtract main.go

When I run docker build it returns:

main.go:7:2: cannot find package "github.internal.com/team_name/project_name/internal" in any of:
    /go/src/github.internal.com/project_name/team_name/vendor/github.internal.com/project_name/team_name/internal (vendor tree) 
    /usr/local/go/src/github.internal.com/team_name/project_name/internal (from $GOROOT)
    /go/src/github.internal.com/team_name/project_name/internal (from $GOPATH)

How do I get to the right directory to recognize all of my files in my module?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongqiaogouk86049 2019-07-19 17:45
    关注

    Make sure go has the go modules enabled, you can enforce this by setting the env var GO111MODULE to on.

    Also as you have vendored your modules dependencies you should build using the mod=vendor flag as: go build -mod=vendor

    Something like CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -mod=vendor -a -o slaxtract main.go should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测