当我尝试运行docker-compose up -d时,出现了错误。

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

I got error when try to run docker-compose up -d

问题

我刚刚开始学习Golang,并尝试使用docker-compose构建go gin应用程序。以下是我的Dockerfile内容:

  1. FROM golang:1.18
  2. WORKDIR /docker-go
  3. COPY ./app/main.go ./
  4. COPY . /docker-go
  5. RUN go mod init shipping-go
  6. RUN go mod tidy
  7. RUN go mod vendor
  8. RUN go mod download && go mod verify
  9. COPY . .
  10. RUN go build -v -o /docker-go/app .
  11. RUN chmod +x /docker-go
  12. USER root

这是我的docker-compose内容:

  1. version: "3.7"
  2. services:
  3. go-web:
  4. build:
  5. context: ./
  6. dockerfile: Dockerfile
  7. restart: 'no'
  8. working_dir: /docker-go
  9. ports:
  10. - "8080:8080"
  11. entrypoint: ["./start.sh"]
  12. volumes:
  13. - ./:/docker-go

当我使用以下命令检查容器日志时,出现了错误:

  1. docker logs learn-docker-go_go-web_1

错误信息为:

  1. /docker-go
  2. go: cannot find main module, but found .git/config in /docker-go
  3. to create a module there, run:
  4. go mod init
  5. /docker-go

看起来go无法找到模块文件,但我在Dockerfile中已经安装了模块。我将代码推送到了我的代码仓库中,你可以在这里查看详细信息:https://github.com/duyanh1904/learn-docker-go

英文:

Im just getting started with Golang and am trying to build go gin with docker-compose.
Here is my Dockerfile

  1. FROM golang:1.18
  2. WORKDIR /docker-go
  3. # pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
  4. # COPY all file go ./
  5. COPY ./app/main.go ./
  6. COPY . /docker-go
  7. # To initialize a project with go module, create go.mod
  8. RUN go mod init shipping-go
  9. # Add missing and/or remove unused modules
  10. RUN go mod tidy
  11. # This will bring all the vendors to your projects /vendor directory so that
  12. # you don't need to get the modules again if working from another machine on this project.
  13. RUN go mod vendor
  14. RUN go mod download && go mod verify
  15. COPY . .
  16. RUN go build -v -o /docker-go/app .
  17. RUN chmod +x /docker-go
  18. USER root

and my docker-compose

  1. version: "3.7"
  2. services:
  3. go-web:
  4. build:
  5. context: ./
  6. dockerfile: Dockerfile
  7. restart: 'no'
  8. working_dir: /docker-go
  9. ports:
  10. - "8080:8080"
  11. entrypoint: ["./start.sh"]
  12. volumes:
  13. - ./:/docker-go

And i got error when i check logs container with command

  1. docker logs learn-docker-go_go-web_1
  2. /docker-go
  3. go: cannot find main module, but found .git/config in /docker-go
  4. to create a module there, run:
  5. go mod init
  6. /docker-go

its seem go can't find module file but i have install in Dockerfile.
For Detail i push code in my repository here
https://github.com/duyanh1904/learn-docker-go

答案1

得分: 3

你的 Dockerfiledocker-compose.yml 存在多个问题,我无法复制你遇到的问题(但你的设置对我也不起作用)。我看到的一些问题包括:

  • COPY . /docker-go 将当前文件夹(包括子文件夹)复制到镜像的 /docker-go/ 目录中。这将导致一个名为 /docker-go/app 的文件夹存在。这个文件夹的存在意味着 go build -v -o /docker-go/app . 将把可执行文件存储为 /docker-go/app/shipping-go(你试图执行一个文件夹)。
  • 你的意图是将可执行文件放在 /docker-go 中,但你又用挂载 (./:/docker-go) 遮盖了这个文件夹。根据文档的说明,"如果你将挂载绑定到容器中的一个非空目录,那么该目录的现有内容将被挂载遮盖"。
  • 你的 start.sh(问题中未显示)没有启动可执行文件(参见文档)。
  • main.go 中(问题中也未显示),将 router.Run("127.0.0.1:8080") 替换为 router.Run(":8080")。监听 127.0.0.1 意味着只接受本地连接;在容器中,这意味着只接受来自容器本身的连接。

这是一个可工作的设置,可以让你开始(这可能不是最佳解决方案,但应该提供一个起点,让你能够进一步实验)。请注意,你需要首先在 main.go 中进行上述更改。

Dockerfile

  1. FROM golang:1.18
  2. WORKDIR /docker-go
  3. # 注意:更好的解决方案是将现有的 go.mod 复制到镜像中
  4. RUN go mod init shipping-go
  5. COPY ./app/main.go ./
  6. # 确定所需的模块并下载它们
  7. RUN go mod tidy
  8. RUN go build -v -o /docker-go/app
  9. RUN chmod +x /docker-go/app

docker-compose.yml

  1. version: "3.7"
  2. services:
  3. go-web:
  4. build:
  5. dockerfile: Dockerfile
  6. restart: 'no'
  7. ports:
  8. - "8080:8080"
  9. command: ["./app"]
英文:

There are multiple issues with your Dockerfile and docker-compose.yml and I have been unable to replicate the issue you are seeing (but your setup does not work for me either). A few of the problems I see are:

  • COPY . /docker-go will copy the current folder (including subfolders) into the image at /docker-go/. This will result in a folder /docker-go/app. The existence of this folder means that go build -v -o /docker-go/app . will store the executable as /docker-go/app/shipping-go (you are trying to execute a folder).
  • Your intention is for the executable to be in /docker-go but you then obscure that folder with a mount (./:/docker-go). As per the docs "If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.".
  • Your start.sh (not shown in the question) is not starting the executable (see the docs).
  • in main.go (also not shown in the question) replace router.Run("127.0.0.1:8080") with router.Run(":8080"). Listening on 127.0.0.1 means that only local connections will be accepted; within a container that means only connections from the container itself.

Here is a working setup to get you going (this is probably not optimal but should provide a starting point that will enable you to experiment further). Note that you need to make the above change in main.go first.

Dockerfile

  1. FROM golang:1.18
  2. WORKDIR /docker-go
  3. # Note: A better solution would be to copy an existing go.mod into the image
  4. RUN go mod init shipping-go
  5. COPY ./app/main.go ./
  6. # Determine required modules and download them
  7. RUN go mod tidy
  8. RUN go build -v -o /docker-go/app
  9. RUN chmod +x /docker-go/app

docker-compose.yml

  1. version: "3.7"
  2. services:
  3. go-web:
  4. build:
  5. dockerfile: Dockerfile
  6. restart: 'no'
  7. ports:
  8. - "8080:8080"
  9. command: ["./app"]

huangapple
  • 本文由 发表于 2022年8月29日 00:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/73520510.html
匿名

发表评论

匿名网友

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

确定