Docker中构建Golang项目时遇到$GOPATH或$GOROOT找不到包的问题
Docker中构建Golang项目时遇到$GOPATH或$GOROOT找不到包的问题
我有一个项目路径为 /Users/me/Documents/dev/grafana/src/github.com/grafana/grafana。该项目使用了其他几个项目,例如:
/Users/me/Documents/dev/grafana/src/github.com/BurntSushi/toml
/Users/me/Documents/dev/grafana/src/github.com/Unknwon/com
在我的机器上可以正常构建所有内容,但当我尝试在 Docker 内构建时,出现了一系列 cannot find package 错误。
go install -v ./pkg/cmd/grafana-server
pkg/login/ldap_settings.go:7:2: cannot find package "github.com/BurntSushi/toml" in any of:
/usr/local/go/src/github.com/BurntSushi/toml (from $GOROOT)
/go/src/github.com/BurntSushi/toml (from $GOPATH)
pkg/services/notifications/codes.go:9:2: cannot find package "github.com/Unknwon/com" in any of:
/usr/local/go/src/github.com/Unknwon/com (from $GOROOT)
/go/src/github.com/Unknwon/com (from $GOPATH)
当我自行构建时,我的 $GOPATH=/Users/me/Documents/dev/grafana/——在我的 Dockerfile 中配置如下:
FROM golang:latest AS build
RUN go version
ENV SRC_DIR=/go/src/github.com/grafana/grafana/
ENV GIT_SSL_NO_VERIFY=1
COPY . $SRC_DIR
WORKDIR $SRC_DIR
[... dependency installations ...]
# Building of Grafana
RUN npm run build
RUN go run build.go setup
RUN go run build.go build
我无法理解为什么这个步骤(特别是 RUN go run build.go setup 步骤)一直报告无法访问这些包。
我查阅了类似的问题,但几乎所有相关问题都没有明确说明在 Docker 中构建(而那些涉及 Docker 的对此场景也没有太大帮助)。
更多关于Docker中构建Golang项目时遇到$GOPATH或$GOROOT找不到包的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html
您没有将代码路径 /go/src/github.com/grafana/grafana/ 添加到容器内部。
更多关于Docker中构建Golang项目时遇到$GOPATH或$GOROOT找不到包的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
尝试运行显式的 go get $SRC_DIR/...
func main() {
fmt.Println("hello world")
}
petkop:
你没有在容器内添加你的代码 /go/src/github.com/grafana/grafana/。
我猜他这样做了:
COPY . $SRC_DIR


