错误 go.mod:没有此文件或目录 编译器 docker compose 本地包

huangapple go评论171阅读模式
英文:

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

问题

我在使用Docker Compose构建一个应用程序时遇到了问题,该应用程序具有本地依赖项以创建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 . .

# 设置操作系统为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"]

我的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

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是存在的!

英文:

I have a problem when I build with docker compose an application with local dependencies to create a docker image.

My 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)

Projects are in the same folder:

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

enter image description here

The go build/run work fine.

Error 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 

NOTE: The file /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod exists!

答案1

得分: 4

Dockerfile位于$GOPATH/src/github.com/julianskyline/motorcars-core-business中,这意味着其中的COPY . .只会将$GOPATH/src/github.com/julianskyline/motorcars-core-business复制到Docker镜像中。

go.mod文件包含了replace指令,引用了不在$GOPATH/src/github.com/julianskyline/motorcars-core-business下的文件夹(例如$GOPATH/src/github.com/julianskyline/errors),这会导致编译错误,因为这些文件夹在Docker镜像中不存在。

要解决这个问题,你可以:

  • 将整个julianskyline文件夹复制到镜像中(将Docker文件移到父文件夹中,通过命令行指定上下文,或使用docker-compose)。
  • 删除replace指令,让go从GitHub上拉取文件。

根据评论中的要求提供答案;我认为评论中提供的信息已经足够帮助问题提出者解决问题了。

英文:

The Dockerfile is in $GOPATH/src/github.com/julianskyline/motorcars-core-business which means that the COPY . . within it will only copy $GOPATH/src/github.com/julianskyline/motorcars-core-business into the docker image.

The go.mod contains replace directives that reference folders not under $GOPATH/src/github.com/julianskyline/motorcars-core-business (e.g. $GOPATH/src/github.com/julianskyline/errors); this leads to compilation errors because those folders are not present within the docker image.

To resolve this you can:

  • Copy the entire julianskyline folder into the image (by moving Docker file into the parent folder, specifying the context on the command line or using docker-compose).
  • Remove the replace directives and letting go pull the files from github.

Posting answer as this was requested in the comments; I believe the comments provided sufficient info for the OP.

huangapple
  • 本文由 发表于 2021年3月7日 04:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/66510494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定