在docker中构建golang项目-在$ GOPATH或$ GOROOT中找不到软件包

This question already has an answer here:

I have a project with the path /Users/me/Documents/dev/grafana/src/github.com/grafana/grafana. This project uses several other projects, for example:

/Users/me/Documents/dev/grafana/src/github.com/BurntSushi/toml
/Users/me/Documents/dev/grafana/src/github.com/Unknwon/com

I can build everything fine on my machine, but when I try to build within Docker, I get a bunch of cannot find package errors.

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)

When I build myself, I have $GOPATH=/Users/me/Documents/dev/grafana/ -- in my Dockerfile I have:

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

I can't figure out why this step (Wich starts in the RUN go run build.go setup step) keeps reporting that it can't access the packages.

I've looked around for similar questions, but almost everything related doesn't specify building in Docker (and the ones that do aren't super helpful for this scenario).

</div>

Can you try the below and see if that works.

Step 1: Install go dep (go get -u github.com/golang/dep/cmd/dep)

Step 2: Run command dep init within your project. This will create a Gopkg.toml and Gopkg.lock

Step 3: Change your docker file

FROM golang:latest AS build

RUN go version

WORKDIR /go/src/github.com/grafana/grafana/
ENV GIT_SSL_NO_VERIFY=1

COPY . .
RUN go get -u github.com/golang/dep/cmd/dep && \
    dep ensure && \
    npm run build && \
    RUN go run build.go setup && \
    RUN go run build.go build && \

How dep works is when you run command dep ensure it pulls all the dependencies and puts them inside the vendor directory and can be easily accessed by your code.