【问题标题】:Golang Dockerfile : Unable to find packages in docker build but runs normallyGolang Dockerfile:无法在 docker build 中找到包但运行正常
【发布时间】:2019-04-25 13:04:31
【问题描述】:

我有以下 dockerfile 设置,用于我的 golang 微服务项目的多阶段构建

FROM golang:alpine as builder

RUN apk --no-cache add git

WORKDIR /app/vessel-service

COPY . .

RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o vessel-service

# Second Stage
...

我的 main.go 中有以下导入

import (
  "context"
  "errors"
  "fmt"

  pb "github.com/thededlier/go-micro-shippy/vessel-service/proto/vessel"
  micro "github.com/micro/go-micro"
)

船舶服务之一用于当前项目。

在运行docker build -t vessel-service . 时出现以下错误

Step 5/12 : RUN go mod download
 ---> Running in 1d0121039462
warning: pattern "all" matched no module dependencies
Removing intermediate container 1d0121039462
 ---> b66add421d26
Step 6/12 : RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o vessel-service
 ---> Running in ef50eff44a3b
main.go:9:3: cannot find package "github.com/micro/go-micro" in any of:
  /usr/local/go/src/github.com/micro/go-micro (from $GOROOT)
  /go/src/github.com/micro/go-micro (from $GOPATH)
main.go:8:3: cannot find package "github.com/thededlier/go-micro-shippy/vessel-service/proto/vessel" in any of:
  /usr/local/go/src/github.com/thededlier/go-micro-shippy/vessel-service/proto/vessel (from $GOROOT)
  /go/src/github.com/thededlier/go-micro-shippy/vessel-service/proto/vessel (from $GOPATH)

但我确实有~/go/src/github.com/micro/go-micro。在尝试直接运行 main.go 时,它运行没有任何问题。

这是我的环境设置的问题还是有其他问题?

这是我的go.mod的摘要

    module github.com/thededlier/go-micro-shippy

    go 1.12

    require (
      ...
      github.com/micro/go-micro v1.1.0
      ...
    )
    replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go v0.0.0-20190108154635-47c0da630f72

    replace sourcegraph.com/sourcegraph/go-diff => github.com/sourcegraph/go-diff v0.5.1

    replace github.com/golang/lint => golang.org/x/lint v0.0.0-20190409202823-959b441ac422

    replace github.com/Sirupsen/logrus => github.com/sirupsen/logrus v1.4.1

【问题讨论】:

  • 这可能是go mod 问题。在开始构建之前尝试导出GO111MODULE=on;。这应该可以解决您的问题。
  • 你的 dockerfile 对我有用。请提供您的 go.mod 文件
  • @AkhilThayyil 这似乎对我不起作用。
  • @MolecularMan ,添加了 go.mod 源
  • 在我看来,输出的关键行是这一行 -> warning: pattern "all" matched no module dependencies。但我无法重现它......

标签: go dockerfile


【解决方案1】:

您需要将 go.mod 文件和 go.sum 文件复制到容器中,并将 ENV 变量 GO111MODULE 设置为 on,如下所示:ENV GO111MODULE=on

一个完整的 Dockerfile 示例:

FROM golang:1.12

ENV GO111MODULE=on
ENV PORT=8090
WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

EXPOSE 8090
ENTRYPOINT ["/app/your-app-name"] 

如果仍然无法正常工作,请尝试将 Golang 版本更改为特定的最新版本,如上例所示。我之前在使用 Golang 版本时遇到过问题。但是你得到它的错误是因为容器中不存在 go.mod 文件。

【讨论】:

  • 我实际上正在使用一个带有go.mod 的整体式存储库。将此服务初始化为一个单独的包,然后复制 go.modgo.sum 似乎可行,但是对于一个单一的 go.mod 的整体存储库有什么解决方案吗?
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多