为什么我的Docker项目的“继续构建”失败了?

时间:2019-02-03 16:19:06

标签: docker go

我有一个npm二进制文件,我想将其打包在Docker容器中。我有这个配置:

# Docker image for the Mongo Stitch command

FROM golang:alpine

# Do a system update
RUN apk update

RUN apk add git

# Declare base dir
WORKDIR /root

# The console binary for Mongo Stitch
RUN git clone https://github.com/10gen/stitch-cli.git

WORKDIR /root/stitch-cli

RUN go build

CMD ["/bin/sh"]

我收到此错误:

main.go:8:2: cannot find package "github.com/10gen/stitch-cli/commands" in any of:
    /usr/local/go/src/github.com/10gen/stitch-cli/commands (from $GOROOT)
    /go/src/github.com/10gen/stitch-cli/commands (from $GOPATH)
main.go:9:2: cannot find package "github.com/10gen/stitch-cli/utils" in any of:
    /usr/local/go/src/github.com/10gen/stitch-cli/utils (from $GOROOT)
    /go/src/github.com/10gen/stitch-cli/utils (from $GOPATH)
main.go:11:2: cannot find package "github.com/mitchellh/cli" in any of:
    /usr/local/go/src/github.com/mitchellh/cli (from $GOROOT)
    /go/src/github.com/mitchellh/cli (from $GOPATH)

这是意外的,因为我认为官方的Go容器将满足我的所有需求。我设置了这些变量:

~ # set | grep GO
GOLANG_VERSION='1.11.5'
GOPATH='/go'

build instructionsgo build足以使此工作正常。我玩过go get,并输出了各种错误,但是我认为这可能是个大难题,因为如果我必须手动获取依赖项,指令就会这样说。

尽管有什么价值,但如果我在容器外壳中发出go get,我会得到:

~/stitch-cli # go get
go get: no install location for directory /root/stitch-cli outside GOPATH
    For more details see: 'go help gopath'

我已经尝试解析帮助文件中的内容,但是我不确定哪一部分适用于我的情况。

所以,我想知道missing GOROOT was worth fixing。我是这样做的:

~/stitch-cli # which go
/usr/local/go/bin/go
~/stitch-cli # GOROOT=/usr/local/go
~/stitch-cli # export GOROOT
~/stitch-cli # go build
# _/root/stitch-cli
./main.go:25:3: cannot use commands.NewWhoamiCommandFactory(ui) (type "github.com/10gen/stitch-cli/vendor/github.com/mitchellh/cli".CommandFactory) as type "github.com/mitchellh/cli".CommandFactory in map value
./main.go:26:3: cannot use commands.NewLoginCommandFactory(ui) (type "github.com/10gen/stitch-cli/vendor/github.com/mitchellh/cli".CommandFactory) as type "github.com/mitchellh/cli".CommandFactory in map value
./main.go:27:3: cannot use commands.NewLogoutCommandFactory(ui) (type "github.com/10gen/stitch-cli/vendor/github.com/mitchellh/cli".CommandFactory) as type "github.com/mitchellh/cli".CommandFactory in map value
./main.go:28:3: cannot use commands.NewExportCommandFactory(ui) (type "github.com/10gen/stitch-cli/vendor/github.com/mitchellh/cli".CommandFactory) as type "github.com/mitchellh/cli".CommandFactory in map value
./main.go:29:3: cannot use commands.NewImportCommandFactory(ui) (type "github.com/10gen/stitch-cli/vendor/github.com/mitchellh/cli".CommandFactory) as type "github.com/mitchellh/cli".CommandFactory in map value
~/stitch-cli # 

好的,也许还有一点,但是我现在坚定地处于“尝试随机事物”领域。我可以做些什么使它开箱即用吗?

我还尝试了手动安装Go的普通Alpine(v3.9),甚至没有设置GOPATH,但是遇到了同样的“找不到包”错误。接下来我可以尝试什么进行编译?

我还尝试过切换到Golang图片的全脂版本,而不是切换到较轻的Alpine图片,并且在buildget上也遇到了相同的问题。

我也可以通过NPM重新安装,但是我遇到了问题(该问题可能超出了Golang问题的范围)。

可能重复

我的问题已被确认为可能重复的of this question。我认为这个问题本质上是这里提到的第二个问题的重复,正如我前面提到的,这可能是一个红色鲱鱼。如果有任何细节说明第一个错误的答案,那可能是一个更好的路标。

1 个答案:

答案 0 :(得分:1)

弗林齐(Flimzy)友善地向我指出了另一个答案(如果没有专业的帮助,我将永远找不到)。由于我认为构建过程很简单,尤其是对于像我这样的非Gopher而言,我将发布新的Dockerfile

# Docker image for the Mongo Stitch command

FROM golang:alpine

# Do a system update
RUN apk update

RUN apk add git curl

# Declare base dir
WORKDIR /root/src

RUN git clone https://github.com/10gen/stitch-cli.git

WORKDIR /root/src/stitch-cli

# Remove the old dependencies
RUN rm -rf vendor

# Fetch the dependencies
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
RUN GOPATH=$GOPATH:/root dep ensure

# Now it should build
RUN GOPATH=$GOPATH:/root go build

CMD ["/bin/sh"]

提取依赖项会导致此错误:

Warning: the following project(s) have [[constraint]] stanzas in Gopkg.toml:

  ✗  github.com/10gen/escaper
  ✗  github.com/dgrijalva/jwt-go
  ✗  github.com/ogier/pflag
  ✗  gopkg.in/mgo.v2

However, these projects are not direct dependencies of the current project:
they are not imported in any .go files, nor are they in the 'required' list in
Gopkg.toml. Dep only applies [[constraint]] rules to direct dependencies, so
these rules will have no effect.

Either import/require packages from these projects so that they become direct
dependencies, or convert each [[constraint]] to an [[override]] to enforce rules
on these projects, if they happen to be transitive dependencies.

但是,它似乎仍然可以编译。我担心的是,与文档相比,我必须执行更多的步骤,这使我认为我使此操作变得比需要的复杂。

Docker存储库

我已将我的Docker仓库永久设为available here