Golang Dockerfile:无法在docker build中找到包但正常运行

问题描述 投票:0回答:1

我为golang微服务项目设置了多个版本的dockerfile

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 dockerfile
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.