douzhi3454 2019-04-25 13:04
浏览 160
已采纳

Golang Dockerfile:无法在Docker构建中找到软件包,但可以正常运行

I have the following dockerfile setup for a multistage build for my golang microservice project

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
...

I have the following imports in my main.go

import (
  "context"
  "errors"
  "fmt"

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

where the vessel-service one is for the current project.

On running docker build -t vessel-service . I get the following error

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)

But I do have ~/go/src/github.com/micro/go-micro. On trying to just directly run main.go, it runs without any issues.

Is this an issue with my environment setup or is there something more to this?

Here's a summary of my 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
  • 写回答

1条回答 默认 最新

  • douxunwei7083 2019-04-25 16:00
    关注

    You need copy the go.mod file and go.sum files into the container and also set the ENV variable GO111MODULE to on like this: ENV GO111MODULE=on.

    A complete example 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"] 
    

    If still it does not work, try changing the Golang version to a specific latest version like in the example above. I had the problem before with Golang version. But the error you are getting it's because of the go.mod file that does not exist in the container.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题