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

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

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

问题

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

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

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

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

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

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

  1. tree
  2. .
  3. ├── Dockerfile
  4. ├── Makefile
  5. ├── docker-compose.yml
  6. └── parsedata-xml-fp.go
  7. 0 directories, 4 files

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

我的Dockerfile:

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  5. RUN go build -gcflags "all=-N -l" -o ./feedme
  6. EXPOSE 8000 2345
  7. COPY --from=build /go/bin/dlv /dlv
  8. COPY --from=build /feedme /feedme
  9. 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:

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go mod init feedme
  5. RUN go mod tidy
  6. RUN GO111MODULE=auto
  7. RUN go build -gcflags="all=-N -l" -o /feedme
  8. EXPOSE 8000 2345
  9. COPY --from=build /go/bin/dlv /dlv
  10. COPY --from=build /feedme /feedme
  11. 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

  1. docker build --network=host -f Dockerfile -t fdebug .
  2. Sending build context to Docker daemon 7.056MB
  3. Step 1/10 : FROM golang:1.17 AS build
  4. ---> 57ac3b44728a
  5. Step 2/10 : WORKDIR /
  6. ---> Using cache
  7. ---> 59325512e59b
  8. Step 3/10 : COPY . .
  9. ---> Using cache
  10. ---> 8e744ca7fb7f
  11. Step 4/10 : RUN go mod init feedme
  12. ---> Using cache
  13. ---> c9a5fd57769c
  14. Step 5/10 : RUN go mod tidy
  15. ---> Using cache
  16. ---> cbc9d9ca142b
  17. Step 6/10 : RUN go build -gcflags="all=-N -l" -o /feedme
  18. ---> Using cache
  19. ---> 38e79d1b85c9
  20. Step 7/10 : EXPOSE 8000 2345
  21. ---> Using cache
  22. ---> fd4950bcf8c9
  23. Step 8/10 : COPY --from=build /go/bin/dlv /dlv
  24. 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:

  1. Step 5/9 : RUN go build -gcflags "all=-N -l" -o ./feedme
  2. ---> Running in a0579ec8a85c
  3. 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)

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

Here are my folder structure and files:

  1. tree
  2. .
  3. ├── Dockerfile
  4. ├── Makefile
  5. ├── docker-compose.yml
  6. └── parsedata-xml-fp.go
  7. 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 :

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  5. RUN go build -gcflags "all=-N -l" -o ./feedme
  6. EXPOSE 8000 2345
  7. COPY --from=build /go/bin/dlv /dlv
  8. COPY --from=build /feedme /feedme
  9. 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. 1) RUN GO111MODULE=off/on
  2. 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

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go mod init feedme
  5. RUN go mod tidy
  6. RUN GO111MODULE=auto
  7. RUN go build -gcflags="all=-N -l" -o /feedme
  8. EXPOSE 8000 2345
  9. COPY --from=build /go/bin/dlv /dlv
  10. COPY --from=build /feedme /feedme
  11. 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

  1. docker build --network=host -f Dockerfile -t fdebug .
  2. Sending build context to Docker daemon 7.056MB
  3. Step 1/10 : FROM golang:1.17 AS build
  4. ---> 57ac3b44728a
  5. Step 2/10 : WORKDIR /
  6. ---> Using cache
  7. ---> 59325512e59b
  8. Step 3/10 : COPY . .
  9. ---> Using cache
  10. ---> 8e744ca7fb7f
  11. Step 4/10 : RUN go mod init feedme
  12. ---> Using cache
  13. ---> c9a5fd57769c
  14. Step 5/10 : RUN go mod tidy
  15. ---> Using cache
  16. ---> cbc9d9ca142b
  17. Step 6/10 : RUN go build -gcflags="all=-N -l" -o /feedme
  18. ---> Using cache
  19. ---> 38e79d1b85c9
  20. Step 7/10 : EXPOSE 8000 2345
  21. ---> Using cache
  22. ---> fd4950bcf8c9
  23. Step 8/10 : COPY --from=build /go/bin/dlv /dlv
  24. 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:

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go mod init feedme
  5. RUN go mod tidy
  6. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  7. RUN go build -gcflags="all=-N -l" -o /feedme
  8. RUN echo $(ls /go/bin)
  9. FROM gcr.io/distroless/base-debian10
  10. WORKDIR /
  11. EXPOSE 2345
  12. COPY --from=build /go/bin/dlv /dlv
  13. COPY --from=build /feedme ~/feedme
  14. #ENTRYPOINT [ "/dlv" ]
  15. CMD ["/dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "~/feedme"]

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

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

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

  1. curl http://localhost:2345

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

这是改进后的Dockerfile版本:

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY go/app/parsedata-xml-fp.go .
  4. COPY go.mod . # 只复制本地的go.mod
  5. COPY go.sum .
  6. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  7. RUN go build -gcflags="all=-N -l" -o /feedme
  8. RUN echo $(ls /go/bin)
  9. FROM gcr.io/distroless/base-debian10
  10. WORKDIR /
  11. EXPOSE 2345
  12. COPY --from=build /go/bin/dlv /dlv
  13. COPY --from=build /feedme ~/feedme
  14. #ENTRYPOINT [ "/dlv" ]
  15. 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:

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY . .
  4. RUN go mod init feedme
  5. RUN go mod tidy
  6. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  7. RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
  8. RUN echo $(ls /go/bin)
  9. FROM gcr.io/distroless/base-debian10
  10. WORKDIR /
  11. EXPOSE 2345
  12. COPY --from=build /go/bin/dlv /dlv
  13. COPY --from=build /feedme ~/feedme
  14. #ENTRYPOINT [ &quot;/dlv&quot; ]
  15. 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 :

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

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

  1. 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

  1. FROM golang:1.17 AS build
  2. WORKDIR /
  3. COPY go/app/parsedata-xml-fp.go .
  4. COPY go.mod . # just copy local go.mod
  5. COPY go.sum .
  6. RUN go install github.com/go-delve/delve/cmd/dlv@latest
  7. RUN go build -gcflags=&quot;all=-N -l&quot; -o /feedme
  8. RUN echo $(ls /go/bin)
  9. FROM gcr.io/distroless/base-debian10
  10. WORKDIR /
  11. EXPOSE 2345
  12. COPY --from=build /go/bin/dlv /dlv
  13. COPY --from=build /feedme ~/feedme
  14. #ENTRYPOINT [ &quot;/dlv&quot; ]
  15. 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:

确定