我正在嘗試為我的 go 服務器創建一個 dockerfile,但它一直失敗,因為它無法識別一些本地依賴項(它們是代碼本身的模塊,而不是外部依賴項)。
例子:
import (
"<private-repo-url>/src/cmd/http-api/bootstrap" // this a local module that's part of the server
"go.uber.org/fx"
)
func main() {
fx.New(bootstrap.Module).Run()
}
這是錯誤:
=> ERROR [7/7] RUN go build -a -o ./server 0.3s
------
> [7/7] RUN go build -a -o ./server:
#10 0.295 server.go:4:2: no required module provides package <private-repo-url>/src/cmd/http-api/bootstrap; to add it:
#10 0.295 go get <private-repo-url>/src/cmd/http-api/bootstrap
------
executor failed running [/bin/sh -c go build -a -o ./server]: exit code: 1
請注意,此private-repo-url對應于此應用程式的存盤庫(它不是外部依賴項)。
這是 Dockerfile
FROM golang:1.17
WORKDIR /balrog
# Copy dependency definitions and download them
ADD go.mod .
ADD go.sum .
RUN go mod download
# Build the binary
ADD ./src .
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
RUN go build -a -o ./server
#Run the server
CMD ["/server"]
和 mod.go 檔案:
module <private-repo-url>
go 1.16
require (
github.com/gin-gonic/gin v1.7.7
github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/sirupsen/logrus v1.8.1
go.uber.org/fx v1.15.0
)
我讀過GO111MODULE說它應該打開,我還讀到它從 1.17(這里)默認啟用。
同樣根據官方docker鏡像(在dockerhub 中),正確的方法是在復制所有檔案后使用go get和go install。這種方法讓我遇到了一個稍微不同的問題,即 docker 無法訪問存盤庫(因為它是私有的),并且我想避免向 docker 添加憑據。
我嘗試使用環境變數GOVCS設定它的值,例如:
ENV GOVCS=github.com:git,gitlab.com:off
但它仍然失敗并出現相同的錯誤。
最后我嘗試了替換,我想如果我從本地依賴項中洗掉它會起作用,所以我執行了(在 Dockerfile 中)這個:
RUN go mod edit -replace <private-repo-url>=./
它再次失敗:
=> ERROR [builder 10/10] RUN go build -a -o ./server 0.3s
------
> [builder 10/10] RUN go build -a -o ./server:
#17 0.299 server.go:4:2: module <private-repo-url>/src provides package <private-repo-url>/src/cmd/http-api/bootstrap and is replaced but not required; to add it:
#17 0.299 go get <private-repo-url>/src
#17 0.299 server.go:5:2: no required module provides package go.uber.org/fx; to add it:
#17 0.299 go get go.uber.org/fx
Is there any way to prevent go builder/package installer to look for these files externally? As both go mod and go get go install try to access this private repository and fail as they do not have access. But they should not try to access it on the first place as it's the application's repository... Or is that I'm doing something wrong (clearly or I wouldn't be here), missing something?
uj5u.com熱心網友回復:
ADD ./src .- 將 的內容復制src到當前檔案夾,剝離src部分。
它應該只是 COPY . ./
另請注意,不建議src在源代碼樹中包含子檔案夾 - 包含的檔案夾go.mod已經是源代碼樹。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373119.html
標籤:go dockerfile go-modules go-get
上一篇:如何在資料庫插入后取回行值?
