Building golang project in docker - cannot find package in any of $GOPATH or $GOROOT

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 (specifically, 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).

You have not added you code /go/src/github.com/grafana/grafana/ inside the container.

Try running an explicit go get $SRC_DIR/...

I assume he did:

COPY . $SRC_DIR

@NobbZ Yes I didn’t see it. I take it back. But I don’t see the directory being created.

@calebt,

You are executing the multi stage docker build incorrectly and as @NobbZ mentioned go get your dependencies.

FROM golang:1.7.3 as builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go    .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"] 

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.