Docker:Go服务器无响应

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

Docker: Go server does not respond

问题

我正在学习如何将Go应用程序进行Docker化。我创建了一个简单的REST API。

package main

import "github.com/gin-gonic/gin"

func main() {
	server := gin.Default()
	server.GET("/", func(ctx *gin.Context) {
		ctx.JSON(200, "HELLO SERVER")
	})
	server.Run("127.0.0.1:3000")
}

这是Dockerfile的内容:

FROM golang:1.18.3-alpine3.16

RUN mkdir /app

COPY . /app

WORKDIR /app

RUN go mod download

RUN go build -o main .

EXPOSE 3000

CMD "/app/main"

问题是,当构建和运行容器时,应用程序似乎正常启动,如往常一样:

[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    /                         --> main.main.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 127.0.0.1:3000

但问题在于,当我使用Postman并向127.0.0.1:3000/发出GET请求时,它显示无法获取任何响应(即当没有可连接的服务器时得到的响应)。

当我使用"go run main.go"运行应用程序时,它正常工作。

如果你能帮我解决这个问题,那将非常好。

英文:

I was learning how to dockerize Go Apps. I created a simple REST API

package main

import "github.com/gin-gonic/gin"

func main() {
	server := gin.Default()
	server.GET("/", func(ctx *gin.Context) {
		ctx.JSON(200, "HELLO SERVER")
	})
	server.Run("127.0.0.1:3000")
}

and here is the Dockerfile

FROM golang:1.18.3-alpine3.16

RUN mkdir /app

COPY . /app

WORKDIR /app

RUN go mod download

RUN go build -o main .

EXPOSE 3000

CMD "/app/main"

Thing is, when build and run the container, it appears the app starts normally as usual

[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    /                         --> main.main.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 127.0.0.1:3000

But here is the issue, when I use Postman and make a get request to 127.0.0.1:3000/, it shows it could not get any response (i.e, the response it gets when there is no such server to connect)

When I run the app using 'go run main.go' it works fine.

It would be really great if you could help me out with this

答案1

得分: 2

也许你应该监听 0.0.0.0:3000

func main() {
    server := gin.Default()
    server.GET("/", func(ctx *gin.Context) {
        ctx.JSON(200, "HELLO SERVER")
    })
    // server.Run("127.0.0.1:3000")
    server.Run("0.0.0.0:3000")
}
英文:

maybe you should listen on 0.0.0.0:3000

func main() {
    server := gin.Default()
    server.GET("/", func(ctx *gin.Context) {
        ctx.JSON(200, "HELLO SERVER")
    })
    // server.Run("127.0.0.1:3000")
    server.Run("0.0.0.0:3000")
}

huangapple
  • 本文由 发表于 2022年6月28日 16:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/72783444.html
匿名

发表评论

匿名网友

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

确定