简体   繁体   English

错误 go.mod:没有这样的文件或目录

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

I have a problem when I build with docker compose an application with local dependencies to create a docker image.当我使用 docker 构建具有本地依赖项的应用程序以创建 docker 映像时,我遇到了问题。

My Dockerfile:我的 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 $GOPATH/src/github.com/julianskyline/errors $GOPATH/src/github.com/julianskyline/motorcars-core-business

enter image description here在此处输入图像描述

The go build/run work fine. go 构建/运行正常。

Error sudo docker-compose build:错误 sudo docker-compose 构建:

 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!注意:文件 /home/julianmarin/proyectos/go/src/github.com/julianskyline/errors/go.mod 存在!

The Dockerfile is in $GOPATH/src/github.com/julianskyline/motorcars-core-business which means that the COPY. . Dockerfile$GOPATH/src/github.com/julianskyline/motorcars-core-business这意味着COPY. . COPY. . within it will only copy $GOPATH/src/github.com/julianskyline/motorcars-core-business into the docker image.其中只会将$GOPATH/src/github.com/julianskyline/motorcars-core-business复制到 docker 图像中。

The go.mod contains replace directives that reference folders not under $GOPATH/src/github.com/julianskyline/motorcars-core-business (eg $GOPATH/src/github.com/julianskyline/errors ); go.mod包含引用不在$GOPATH/src/github.com/julianskyline/motorcars-core-business下的文件夹的replace指令(例如$GOPATH/src/github.com/julianskyline/errors ); this leads to compilation errors because those folders are not present within the docker image.这会导致编译错误,因为 docker 映像中不存在这些文件夹。

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 ).将整个julianskyline文件夹复制到图像中(通过将Docker文件移动到父文件夹中, 在命令行上指定上下文或使用docker-compose )。
  • Remove the replace directives and letting go pull the files from github.删除replace指令并让go从 github 中提取文件。

Posting answer as this was requested in the comments;按照评论中的要求发布答案; I believe the comments provided sufficient info for the OP.我相信这些评论为 OP 提供了足够的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 构建:“go:在当前目录或任何父目录中找不到 go.mod 文件” - Docker build : "go: go.mod file not found in current directory or any parent directory" Docker compose $GOPATH/go.mod 存在但不应该 - Docker compose $GOPATH/go.mod exists but should not Docker在go.mod文件中找不到依赖项 - Docker can't find dependencies in go.mod file $GOPATH/go.mod 存在但不应该在 Docker - $GOPATH/go.mod exists but should not on Docker “去:go.mod 文件在当前目录中找不到”但它已经存在于目录中 - "go: go.mod file not found in current directory" but it already exist in the directory 如何将 go 模块与本地 package 和 docker 一起使用? - How to use go mod with local package and docker? Docker:go 命令上的“go.mod:权限被拒绝” - Docker : 'go.mod: permission denied' on go commands Docker:复制失败:在构建上下文中找不到文件或被 .dockerignore 排除:stat go.mod - Docker: COPY failed: file not found in build context or excluded by .dockerignore: stat go.mod 如何在子文件夹中使用 go.mod 构建 golang docker 图像? - How to build a golang docker image with go.mod in a sub folder? Jenkins 中的 Docker-compose 错误“docker-compose:没有这样的文件或目录” - Docker-compose error in Jenkins "docker-compose: No such file or directory"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM