dougou7008 2018-06-14 21:02
浏览 591

在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>
  • 写回答

1条回答 默认 最新

  • dqf60304 2018-06-16 03:41
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败