[英]Why does “go build” fail for my Docker project?
我有一個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'
構建說明說, go 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'
我曾嘗試解析所述幫助文件中的內容,但不確定哪一部分適用於我的情況。
因此,我想知道丟失GOROOT
是否值得修復 。 我這樣做:
~/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圖片,並且在build
和get
都遇到了相同的問題。
我也可以通過NPM重新安裝,但是我遇到了問題(該問題可能超出了Golang問題的范圍)。
我的問題被確定為該問題的可能重復。 我認為這個問題本質上是這里提到的第二個問題的重復,正如我前面提到的,這可能是一個紅色鯡魚。 如果有任何細節說明第一個錯誤的答案,那將是一個更好的路標。
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.
但是,它似乎仍然可以編譯。 我擔心的是,與文檔相比,我必須要做的步驟要多得多,這使我認為我使這一過程變得比需要的更加復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.