错误 go.mod: no such file or directory Compiler docker compose local package

时间:2021-03-06 20:47:40

标签: docker go build docker-compose microservices

当我使用 docker 构建一个具有本地依赖项的应用程序以创建 docker 镜像时遇到问题。

我的 Dockerfile:

FROM golang:alpine AS build 

ENV GOPATH=$GOPATH
#GOPROXY
ENV GOPROXY=http://proxy.golang.org
ENV GO111MODULE=on


WORKDIR $GOPATH/src/github.com/julianskyline/motorcars-core-business

COPY . . 


# Set OS as linux
RUN GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go

FROM alpine
COPY --from=build $GOPATH/bin/github.com/julianskyline/motorcars-core-business $GOPATH/bin/github.com/julianskyline/motorcars-core-business
ENTRYPOINT [ "/go/bin/motorcars-core-business" ]
My go.mod

module github.com/julianskyline/motorcars-core-business

go 1.15

replace (
    github.com/julianskyline/errors => /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors
    github.com/julianskyline/motorcars-db => /home/julianmarin/proyectos/go/src/github.com/julianskyline/motorcars-db
    github.com/julianskyline/motorcars-models => /home/julianmarin/proyectos/go/src/github.com/julianskyline/motorcars-models)

项目在同一个文件夹中:

$GOPATH/src/github.com/julianskyline/errors $GOPATH/src/github.com/julianskyline/motorcars-core-business

enter image description here

go build/run 工作正常。

错误 sudo docker-compose build:

 Step 6/9 : RUN GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go
 ---> Running in 45227441dfdd
go: github.com/julianskyline/errors@v0.0.0-00010101000000-000000000000 (replaced by /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors): reading /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod: open /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod: no such file or directory
The command '/bin/sh -c GOOS=linux go build -o $GOPATH/bin/github.com/julianskyline/motorcars-core-business main.go' returned a non-zero code: 1
ERROR: Service 'api' failed to build 

注意:文件 /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod 存在!

0 个答案:

没有答案