为什么Go不能在Docker中访问8080端口?

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

Why go can't access port 8080 in docker

问题

所以我想在Cloud Run中部署我的简单Go后端。我使用Gin来处理路由。我的处理函数看起来像这样:

func main() {
    r := gin.Default()
    r.GET("/api/health", handlers.HealthCheckHandler())
    r.POST("/questions", handlers.CreateQuestionHandler(client))
    r.GET("/questions/:level", handlers.GetAllQuestionHandler(client))
    r.Run("0.0.0.0:8080")
}

我尝试使用Docker构建它,并且成功了。我的Dockerfile如下所示:

FROM golang:1.20
ENV GIN_MODE=release

WORKDIR /app

# 下载Go模块
COPY go.mod go.sum ./
RUN go mod download

COPY . .

# 构建
RUN go build -o /docker-api

EXPOSE 8080

# 运行
CMD ["/docker-api"]

所以我尝试在Google Cloud CLI中使用docker run运行它,看起来运行得很好:

docker run -p 8080:8080 gcr.io/matharc/math-arc-api
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /api/health               --> matharc.com/m/handlers.HealthCheckHandler.func1 (3 handlers)
[GIN-debug] POST   /questions                --> matharc.com/m/handlers.CreateQuestionHandler.func1 (3 handlers)
[GIN-debug] GET    /questions/:level         --> matharc.com/m/handlers.GetAllQuestionHandler.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on localhost:8080

但是当我尝试在端口8080上预览它时,我无法访问它:

为什么Go不能在Docker中访问8080端口?
为什么Go不能在Docker中访问8080端口?

我尝试只在Cloud Run中部署它,但毫不奇怪,它不起作用。我得到以下错误:

STARTUP HTTP probe failed 1 time consecutively for container "math-arc-api-1" on path "/api/health". The instance was not started.

我做错了什么?

英文:

So I want to deploy my simple Go backend in Cloud Run. I use Gin to handle the routing. My main function that handle it look like this:

func main() {
	r := gin.Default()
	r.GET("/api/health", handlers.HealthCheckHandler())
	r.POST("/questions", handlers.CreateQuestionHandler(client))
	r.GET("/questions/:level", handlers.GetAllQuestionHandler(client))
	r.Run("0.0.0.0:8080")
}

I try to build it using docker and it success. My Dockerfile look like this:

FROM golang:1.20
ENV GIN_MODE=release

WORKDIR /app

# Download Go modules
COPY go.mod go.sum ./
RUN go mod download


COPY . .

# Build
RUN  go build -o /docker-api

EXPOSE 8080

# Run
CMD ["/docker-api"]

So I try to run it with docker run in Google Cloud CLI, and it seems to run pretty fine:

 docker run -p 8080:8080  gcr.io/matharc/math-arc-api
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /api/health               --> matharc.com/m/handlers.HealthCheckHandler.func1 (3 handlers)
[GIN-debug] POST   /questions                --> matharc.com/m/handlers.CreateQuestionHandler.func1 (3 handlers)
[GIN-debug] GET    /questions/:level         --> matharc.com/m/handlers.GetAllQuestionHandler.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on localhost:8080

But when I try to preview it on port 8080. I can't access it:

为什么Go不能在Docker中访问8080端口?
为什么Go不能在Docker中访问8080端口?

I try to just deploy it in Cloud Run, but to no surprise, it's not working. I get:

STARTUP HTTP probe failed 1 time consecutively for container "math-arc-api-1" on path "/api/health". The instance was not started.

What did I do wrong?

答案1

得分: -1

所以评论中的@Hans Kilian的答案是正确的。问题是因为我在代码中使用了localhost而不是0.0.0.0。我以为我已经将其更改为0.0.0.0,但在构建时似乎犯了一些错误。

英文:

So the answer from @Hans Kilian in the comment is the right one. The problem is because I use localhost instead of 0.0.0.0. I thought that I has changed it to 0.0.0.0 in the code but seems like I did some mistake when I built it.

huangapple
  • 本文由 发表于 2023年7月17日 00:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76699352.html
匿名

发表评论

匿名网友

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

确定