dpojoxa5613 2019-05-20 14:28
浏览 293
已采纳

带有Go的Dockerfile找不到路径

I have an issue getting my local build to run as well as getting my Dockerfile configured.

My project structure looks like:

project
- cmd
  main.go
- internal
  - app
    app.go
Dockerfile

So, in main.go I say

import (
    "project/internal/app"
)

Then, when I say go build I can run locally perfectly.

However, in my Dockerfile I say

FROM golang
ENV GOPATH /go/src/github.com/project
COPY . /go/src/github.com/project
WORKDIR /go/src/github.com/project
RUN make linux

And I get the issue:

cmd/main.go:4:2: cannot find package "Slaxtract/internal/app" in any of:
    /usr/local/go/src/project/internal/app (from $GOROOT)
    /go/src/github.com/project/src/project/internal/app (from $GOPATH)

Why is Docker adding src to the GOPATH? And how can I configure it to look in the right spot?

If I change my main.go to be a relative path I can hack a fix - but then I can't run locally as I get

main.go:4:2: local import "../internal/app" in non-local package

Any and all help would be really appreciated.

  • 写回答

1条回答 默认 最新

  • dqy27359 2019-05-20 14:35
    关注

    By default Go expects to find folders like src, pkg and bin within GOPATH. However, you're pointing it into in fact your project folder. To fix it you just need to point your GOPATH into /go.

    So your Dockerfile should look like

    FROM golang
    ENV GOPATH /go
    COPY . /go/src/github.com/project
    WORKDIR /go/src/github.com/project
    RUN make linux
    

    You can find more information on GOPATH here. If you're tired of GOPATH you can give a try to gomodules

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵