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

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

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

问题

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

FROM golang:1.18

WORKDIR /docker-go

COPY ./app/main.go ./
COPY . /docker-go

RUN go mod init shipping-go

RUN go mod tidy

RUN go mod vendor

RUN go mod download && go mod verify

COPY . .

RUN go build -v -o /docker-go/app .

RUN chmod +x /docker-go
USER root

这是我的docker-compose内容:

version: "3.7"
services:
  go-web:
    build:     
      context: ./
      dockerfile: Dockerfile
    restart: 'no'
    working_dir: /docker-go
    ports:
      - "8080:8080"
    entrypoint: ["./start.sh"]
    volumes:
      - ./:/docker-go

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

docker logs learn-docker-go_go-web_1

错误信息为:

/docker-go
go: cannot find main module, but found .git/config in /docker-go
	to create a module there, run:
	go mod init
/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

FROM golang:1.18

WORKDIR /docker-go

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
# COPY all file go ./

COPY ./app/main.go ./
COPY . /docker-go
# To initialize a project with go module, create go.mod

RUN go mod init shipping-go

# Add missing and/or remove unused modules
RUN go mod tidy

# This will bring all the vendors to your projects /vendor directory so that 
# you don't need to get the modules again if working from another machine on this project.
RUN go mod vendor

RUN go mod download && go mod verify

COPY . .
RUN go build -v -o /docker-go/app .

RUN chmod +x /docker-go
USER root

and my docker-compose

version: "3.7"
services:
  go-web:
    build:     
      context: ./
      dockerfile: Dockerfile
    restart: 'no'
    working_dir: /docker-go
    ports:
      - "8080:8080"
    entrypoint: ["./start.sh"]
    volumes:
      - ./:/docker-go

And i got error when i check logs container with command

docker logs learn-docker-go_go-web_1

/docker-go
go: cannot find main module, but found .git/config in /docker-go
	to create a module there, run:
	go mod init
/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

FROM golang:1.18

WORKDIR /docker-go

# 注意:更好的解决方案是将现有的 go.mod 复制到镜像中
RUN go mod init shipping-go
COPY ./app/main.go ./
# 确定所需的模块并下载它们
RUN go mod tidy
RUN go build -v -o /docker-go/app
RUN chmod +x /docker-go/app

docker-compose.yml

version: "3.7"
services:
  go-web:
    build:
      dockerfile: Dockerfile
    restart: 'no'
    ports:
      - "8080:8080"
    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

FROM golang:1.18

WORKDIR /docker-go

# Note: A better solution would be to copy an existing go.mod into the image
RUN go mod init shipping-go
COPY ./app/main.go ./
# Determine required modules and download them
RUN go mod tidy
RUN go build -v -o /docker-go/app
RUN chmod +x /docker-go/app

docker-compose.yml

version: "3.7"
services:
  go-web:
    build:     
      dockerfile: Dockerfile
    restart: 'no'
    ports:
      - "8080:8080"
    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:

确定