Docker构建: “go: 在当前目录或任何父目录中找不到go.mod文件”

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

Docker build : "go: go.mod file not found in current directory or any parent directory"

问题

我正在尝试构建一个包含delve调试器的Golang应用程序的Dockerfile,我想在Docker容器中调试我的Golang应用程序。

当我尝试构建我的Docker时,我一直遇到以下错误:

Step 5/9 : RUN go build -gcflags "all=-N -l" -o ./feedme
---> Running in a0579ec8a85c
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

在我的本地文件夹上,go build命令运行良好(参考下面的tree命令):

go build -gcflags "all=-N -l" -o ./feedme

这是我的文件夹结构和文件:

tree
.
├── Dockerfile
├── Makefile
├── docker-compose.yml
└── parsedata-xml-fp.go

0 directories, 4 files

单个应用程序文件parsedata-xml-fp.go(我将忽略它,因为我认为错误与它无关)

我的Dockerfile:

FROM golang:1.17 AS build
WORKDIR /
COPY . .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags "all=-N -l" -o ./feedme

EXPOSE 8000 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme /feedme

CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/feedme"]

有任何想法为什么会出现这个错误以及如何修复它吗?
我搜索了一下并尝试设置了一些环境变量:

  1. RUN GO111MODULE=off/on
  2. RUN CGO_ENABLED=0

似乎它们都不起作用。

编辑:
我修改了我的Dockerfile,添加了'go mod init feedme',然后我得到了一些新的错误:

新的Dockerfile:

FROM golang:1.17 AS build

WORKDIR /
COPY . .
RUN go mod init feedme
RUN go mod tidy

RUN GO111MODULE=auto
RUN go build -gcflags="all=-N -l" -o /feedme

EXPOSE 8000 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme /feedme

CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/feedme"]

错误消息是invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

docker build --network=host -f Dockerfile -t fdebug .
Sending build context to Docker daemon  7.056MB
Step 1/10 : FROM golang:1.17 AS build
---> 57ac3b44728a
Step 2/10 : WORKDIR /
---> Using cache
---> 59325512e59b
Step 3/10 : COPY . .
---> Using cache
---> 8e744ca7fb7f
Step 4/10 : RUN go mod init feedme
---> Using cache
---> c9a5fd57769c
Step 5/10 : RUN go mod tidy
---> Using cache
---> cbc9d9ca142b
Step 6/10 : RUN go build -gcflags="all=-N -l" -o /feedme
---> Using cache
---> 38e79d1b85c9
Step 7/10 : EXPOSE 8000 2345
---> Using cache
---> fd4950bcf8c9
Step 8/10 : COPY --from=build /go/bin/dlv /dlv
invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

请问为什么会出现这个错误,我该如何修复它?

英文:

I am trying to build a Dockerfile of my Golang application which includes delve debugger - I want to debug my Golang application in Docker container.

When I try to build my Docker I consistently go error below:

Step 5/9 : RUN go build -gcflags "all=-N -l" -o ./feedme
 ---> Running in a0579ec8a85c
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

The command go build runs well on my local folder(refer tree commands below)

go build -gcflags "all=-N -l" -o ./feedme

Here are my folder structure and files:

tree
.
├── Dockerfile
├── Makefile
├── docker-compose.yml
└── parsedata-xml-fp.go

0 directories, 4 files

Single application file parsedata-xml-fp.go (I will dismiss it , since I think the error has nothing to do with it)

My Dockerfile :

FROM golang:1.17 AS build
WORKDIR /
COPY . .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags "all=-N -l" -o ./feedme

EXPOSE 8000 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme /feedme

CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/feedme"]

Any idea why this error occur and how can I fix it ?
I searched for it and tried to set some env variable:

1) RUN GO111MODULE=off/on
2) RUN CGO_ENABLED=0

Seems like none of them works

Edit:
I modified my Dockerfile added 'go mod init feedme' and I got some new error:

New Dockerfile

FROM golang:1.17 AS build

WORKDIR /
COPY . .
RUN go mod init feedme
RUN go mod tidy

RUN GO111MODULE=auto
RUN go build -gcflags="all=-N -l" -o /feedme

EXPOSE 8000 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme /feedme

CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/feedme"]

Error message is :invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

docker build --network=host -f Dockerfile -t fdebug .
Sending build context to Docker daemon  7.056MB
Step 1/10 : FROM golang:1.17 AS build
 ---> 57ac3b44728a
Step 2/10 : WORKDIR /
 ---> Using cache
 ---> 59325512e59b
Step 3/10 : COPY . .
 ---> Using cache
 ---> 8e744ca7fb7f
Step 4/10 : RUN go mod init feedme
 ---> Using cache
 ---> c9a5fd57769c
Step 5/10 : RUN go mod tidy
 ---> Using cache
 ---> cbc9d9ca142b
Step 6/10 : RUN go build -gcflags="all=-N -l" -o /feedme
 ---> Using cache
 ---> 38e79d1b85c9
Step 7/10 : EXPOSE 8000 2345
 ---> Using cache
 ---> fd4950bcf8c9
Step 8/10 : COPY --from=build /go/bin/dlv /dlv
invalid from flag value build: pull access denied for build, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

答案1

得分: 3

这里是我翻译好的内容:

感谢@TheFool,感谢提供的文档和指导。
我阅读了官方的Docker文档:docs.docker.com/language/golang/,还有multi staging build blog

以下是我提出的解决方案,我可以在本地启动我的Go应用程序容器。

我的Dockerfile:

FROM golang:1.17 AS build

WORKDIR /
COPY . .

RUN go mod init feedme
RUN go mod tidy

RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags="all=-N -l" -o /feedme
RUN echo $(ls /go/bin)

FROM gcr.io/distroless/base-debian10
WORKDIR /

EXPOSE 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ "/dlv" ]
CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "~/feedme"]

我使用以下命令启动容器:

docker run -p 2345:2345 <docker image ID>

然后我尝试使用curl访问它,确实有响应:

curl http://localhost:2345

[编辑] 根据TheFool的建议,我直接在容器中使用了我的本地副本go.mod和go.sum。将它从我的本地工作空间复制到容器中(而不是在容器中生成go.mod),以避免将来出现任何意外的问题。

这是改进后的Dockerfile版本:

FROM golang:1.17 AS build

WORKDIR /
COPY go/app/parsedata-xml-fp.go .
COPY go.mod .   # 只复制本地的go.mod
COPY go.sum .

RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags="all=-N -l" -o /feedme
RUN echo $(ls /go/bin)

FROM gcr.io/distroless/base-debian10
WORKDIR /

EXPOSE 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ "/dlv" ]
CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "~/feedme"]
英文:

The credit goes to @TheFool, thank you for the documents and guidance.
I read official Docker documents : docs.docker.com/language/golang/ and also multi staging build blog

Here is the solution I came up with and I can boot my Go application container locally

Dockerfile of mine:

FROM golang:1.17 AS build

WORKDIR /
COPY . .

RUN go mod init feedme
RUN go mod tidy

RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
RUN echo $(ls /go/bin)

FROM gcr.io/distroless/base-debian10
WORKDIR /

EXPOSE 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ &quot;/dlv&quot; ]
CMD [&quot;/dlv&quot;, &quot;--listen=:2345&quot;, &quot;--headless=true&quot;, &quot;--api-version=2&quot;, &quot;--accept-multiclient&quot;, &quot;exec&quot;, &quot;~/feedme&quot;]

I boot my container using :

docker run -p 2345:2345 &lt;docker image ID&gt;

Then I tried to curl to it , it does have response:

curl http://localhost:2345

[Edit] Per suggestion from TheFool, I used my local copy of go.mod and go.sum directly in my container. COPY it from my local workspace to container,(rather than generate go.mod in the container) to avoid any unexpected surprise in the future:

Here is the improved version of Dockerfile

FROM golang:1.17 AS build

WORKDIR /
COPY go/app/parsedata-xml-fp.go .
COPY go.mod .   # just copy local go.mod
COPY go.sum .

RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
RUN echo $(ls /go/bin)

FROM gcr.io/distroless/base-debian10
WORKDIR /

EXPOSE 2345 

COPY --from=build /go/bin/dlv /dlv
COPY --from=build /feedme ~/feedme
#ENTRYPOINT [ &quot;/dlv&quot; ]
CMD [&quot;/dlv&quot;, &quot;--listen=:2345&quot;, &quot;--headless=true&quot;, &quot;--api-version=2&quot;, &quot;--accept-multiclient&quot;, &quot;exec&quot;, &quot;~/feedme&quot;]

huangapple
  • 本文由 发表于 2022年6月4日 21:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/72500216.html
匿名

发表评论

匿名网友

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

确定