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

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

Why go can't access port 8080 in docker

问题

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

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

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

  1. FROM golang:1.20
  2. ENV GIN_MODE=release
  3. WORKDIR /app
  4. # 下载Go模块
  5. COPY go.mod go.sum ./
  6. RUN go mod download
  7. COPY . .
  8. # 构建
  9. RUN go build -o /docker-api
  10. EXPOSE 8080
  11. # 运行
  12. CMD ["/docker-api"]

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

  1. docker run -p 8080:8080 gcr.io/matharc/math-arc-api
  2. [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  3. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
  4. - using env: export GIN_MODE=release
  5. - using code: gin.SetMode(gin.ReleaseMode)
  6. [GIN-debug] GET /api/health --> matharc.com/m/handlers.HealthCheckHandler.func1 (3 handlers)
  7. [GIN-debug] POST /questions --> matharc.com/m/handlers.CreateQuestionHandler.func1 (3 handlers)
  8. [GIN-debug] GET /questions/:level --> matharc.com/m/handlers.GetAllQuestionHandler.func1 (3 handlers)
  9. [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
  10. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
  11. [GIN-debug] Listening and serving HTTP on localhost:8080

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

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

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

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

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

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

  1. FROM golang:1.20
  2. ENV GIN_MODE=release
  3. WORKDIR /app
  4. # Download Go modules
  5. COPY go.mod go.sum ./
  6. RUN go mod download
  7. COPY . .
  8. # Build
  9. RUN go build -o /docker-api
  10. EXPOSE 8080
  11. # Run
  12. CMD ["/docker-api"]

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

  1. docker run -p 8080:8080 gcr.io/matharc/math-arc-api
  2. [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  3. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
  4. - using env: export GIN_MODE=release
  5. - using code: gin.SetMode(gin.ReleaseMode)
  6. [GIN-debug] GET /api/health --> matharc.com/m/handlers.HealthCheckHandler.func1 (3 handlers)
  7. [GIN-debug] POST /questions --> matharc.com/m/handlers.CreateQuestionHandler.func1 (3 handlers)
  8. [GIN-debug] GET /questions/:level --> matharc.com/m/handlers.GetAllQuestionHandler.func1 (3 handlers)
  9. [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
  10. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
  11. [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:

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

确定