来自父目录的Docker生成出现错误-包不在GOROOT中
我正试图为它的这个示例项目结构创建一个docker映像,如下所示来自父目录的Docker生成出现错误-包不在GOROOT中,docker,go,dockerfile,go-modules,Docker,Go,Dockerfile,Go Modules,我正试图为它的这个示例项目结构创建一个docker映像,如下所示 module go-modules go 1.15 replace common => /go-modules-docker/common require ( common v0.0.0-00010101000000-000000000000 github.com/julienschmidt/httprouter v1.3.0 ) package main import ( "fm
module go-modules
go 1.15
replace common => /go-modules-docker/common
require (
common v0.0.0-00010101000000-000000000000
github.com/julienschmidt/httprouter v1.3.0
)
package main
import (
"fmt"
"log"
"net/http"
"go-modules/greet" // go-modules is our project namespace
"common/deps"
"github.com/julienschmidt/httprouter"
)
func main() {
r := httprouter.New()
r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "hello world, v%v %v", greet.Version, deps.Version2)
})
log.Println("listening to port *:8080. press ctrl + c to cancel.")
log.Fatal(http.ListenAndServe(":8080", r))
}
- 两个模块-通用模块和go模块
FROM golang as builder
ENV GO111MODULE=on
WORKDIR /go-modules-docker
COPY . .
COPY ./go-modules/go.mod .
COPY ./go-modules/go.sum .
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go
FROM alpine:3.8
WORKDIR /root/
COPY --from=builder /go-modules/app .
CMD ["./app"]
go.mod文件如下所示
module go-modules
go 1.15
replace common => /go-modules-docker/common
require (
common v0.0.0-00010101000000-000000000000
github.com/julienschmidt/httprouter v1.3.0
)
package main
import (
"fmt"
"log"
"net/http"
"go-modules/greet" // go-modules is our project namespace
"common/deps"
"github.com/julienschmidt/httprouter"
)
func main() {
r := httprouter.New()
r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "hello world, v%v %v", greet.Version, deps.Version2)
})
log.Println("listening to port *:8080. press ctrl + c to cancel.")
log.Fatal(http.ListenAndServe(":8080", r))
}
Main.go如下所示
module go-modules
go 1.15
replace common => /go-modules-docker/common
require (
common v0.0.0-00010101000000-000000000000
github.com/julienschmidt/httprouter v1.3.0
)
package main
import (
"fmt"
"log"
"net/http"
"go-modules/greet" // go-modules is our project namespace
"common/deps"
"github.com/julienschmidt/httprouter"
)
func main() {
r := httprouter.New()
r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "hello world, v%v %v", greet.Version, deps.Version2)
})
log.Println("listening to port *:8080. press ctrl + c to cancel.")
log.Fatal(http.ListenAndServe(":8080", r))
}
从父目录运行此命令时-go modules docker
docker build-t go mod docker-f go模块/Dockerfile.
我得到以下错误
[+] Building 2.2s (13/15)
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.8 0.8s
=> [internal] load metadata for docker.io/library/golang:latest 0.8s
=> [internal] load build context 0.0s
=> => transferring context: 618B 0.0s
=> [builder 1/7] FROM docker.io/library/golang@sha256:cf46c759511d0376c706a923f2800762948d4ea1a9290360720d5124a730ed63 0.0s
=> [stage-1 1/3] FROM docker.io/library/alpine:3.8@sha256:2bb501e6173d9d006e56de5bce2720eb06396803300fe1687b58a7ff32bf4c14 0.0s
=> CACHED [builder 2/7] WORKDIR /go-modules-docker 0.0s
=> [builder 3/7] COPY . . 0.0s
=> [builder 4/7] COPY ./go-modules/go.mod . 0.0s
=> [builder 5/7] COPY ./go-modules/go.sum . 0.0s
=> [builder 6/7] RUN go mod download 0.8s
=> ERROR [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go 0.5s
------
> [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go:
#14 0.434 go-modules/main.go:8:2: package go-modules/greet is not in GOROOT (/usr/local/go/src/go-modules/greet)
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go]: runc did not terminate sucessfully
有人能建议可以做什么以及如何解决这个问题吗?通过将
WORKDIR
文件再次更新到go modules
(基本上是做一个CD-change目录)并构建整个模块来解决这个问题
将docker文件更改为此解决了此问题
FROM golang as builder
ENV GO111MODULE=on
WORKDIR /go-modules-docker
COPY . .
WORKDIR /go-modules-docker/go-modules
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o /go-modules-docker/app
FROM alpine:3.8
WORKDIR /root/
COPY --from=builder /go-modules-docker/app .
CMD ["./app"]
它是否打印出您附加的图像文件,或者打印出一些文本格式错误?您能用错误消息的实际文本替换屏幕截图吗?请参阅请勿发布文本图像。您正在使用单个文件名运行
go build
,该文件名不会使用模块。使用模块内的软件包运行go build
。@DavidMaze编辑了该问题,以将错误图像替换为错误文本。@JimB如您所述,我认为问题在于build命令是从模块外运行的。因为它是一个多模块,并且在主模块中使用公共模块。。我必须从模块外部运行build命令(复制Dockerfile中的公共文件夹)。