【问题标题】:Docker build from parent directory is giving error - package is not in GOROOT从父目录构建的 Docker 给出错误 - 包不在 GOROOT 中
【发布时间】:2021-03-13 16:05:09
【问题描述】:

我正在尝试为它的这个示例项目结构创建一个 docker 映像,如下所示

  • 两个模块 - common 和 go-modules

以下是我的 Docker 文件

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
)

Main.go如下

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-modules/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

谁能建议可以做什么以及如何解决这个问题?

【问题讨论】:

  • 它是打印出您附加的图像文件,还是一些文本格式错误?你能用错误消息的实际文本替换屏幕截图吗?
  • 请不要发布文字图片。您正在使用单个文件名运行go build,它不会使用模块。使用模块内的包运行 go build
  • @DavidMaze 已编辑问题以用错误文本替换错误图像。
  • @JimB 正如您所提到的,我认为问题在于构建命令是从模块外部运行的。由于它是一个多模块并且在主模块中使用了公共模块。我必须从模块外部运行构建命令(复制 Dockerfile 中的公共文件夹)。

标签: docker go dockerfile go-modules


【解决方案1】:

通过再次将 Dockerfile 更新为 go-modules(基本上是做一张 CD - 更改目录)并构建整个模块来解决问题

将 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"]

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2021-10-31
    相关资源
    最近更新 更多