容器化 API 套接字挂起

Jer*_*emy 3 rest containers go docker postman

我刚刚完成 Go REST API 的开发,我想将我的代码移至容器中。Error: socket hang up我已经完成了这一点,但由于抛出错误:我无法通过邮递员到达我的端点。

\n

根据这个SO线程,这可能是由我的防火墙引起的,运行后ufw status我注意到我的防火墙没有启动,所以我不确定是什么导致了这个错误。

\n

我的应用程序似乎运行正常,并且我将容器映射到本地计算机中的端口 8080。

\n
\xe2\x94\x94\xe2\x94\x80(19:14:04 on master \xe2\x9c\xad)\xe2\x94\x80\xe2\x94\x80> docker ps                                                                                                                     \xe2\x94\x80\xe2\x94\x80(Tue,Feb02)\xe2\x94\x80\xe2\x94\x98\nCONTAINER ID   IMAGE        COMMAND       CREATED          STATUS          PORTS                    NAMES\nb001f19e2957   go-library   "./library"   15 minutes ago   Up 15 minutes   0.0.0.0:8080->8080/tcp   cranky_kare\n
Run Code Online (Sandbox Code Playgroud)\n

我还确保我的请求正在使用此端口。

\n
GET http://localhost:8080/books/3\nError: socket hang up\nRequest Headers\nUser-Agent: PostmanRuntime/7.26.10\nAccept: */*\nPostman-Token: 190a5849-b859-4324-a65e-37a7b69b49fb\nHost: localhost:8080\nAccept-Encoding: gzip, deflate, br\n
Run Code Online (Sandbox Code Playgroud)\n

leo*_*het 7

我的问题是我正在启动服务器127.0.0.1,但端口映射没有指向该接口。在所有接口 ( ) 上启动它对0.0.0.0我有用:

addr := fmt.Sprintf("0.0.0.0:%v", settings.Port)
server := &http.Server{
    Addr:         addr,
    Handler:      app.Router,
    IdleTimeout:  120 * time.Second,
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 5 * time.Second,
}
Run Code Online (Sandbox Code Playgroud)