curl: (56) Recv failure: Connection reset by peer in golang with docker

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

curl: (56) Recv failure: Connection reset by peer in golang with docker

问题

我正在使用 Docker 容器化一个 Golang 应用程序,并尝试访问该应用程序。应用程序在端口 8007 上运行。

我使用以下命令运行容器:

docker run --name sample_go_app -p 7000:8007 sample_go

尝试执行 curl http://localhost:7000,但是出现错误:

curl: (56) Recv failure: Connection reset by peer

main.go

...
srv := &http.Server {
Handler: router,
Addr:    "localhost:8007",
}
...
英文:

I am dockerized a golang application and i am trying to access the application. Application running in port 8007

I am running the container the following command

docker run --name sample_go_app -p 7000:8007 sample_go

After tried curl http://localhost:7000 but getting error

curl: (56) Recv failure: Connection reset by peer

main.go

...
srv := &http.Server {
Handler: router,
Addr:    "localhost:8007",
}
...

答案1

得分: 3

你正在将套接字绑定到本地主机地址,这个地址无法从容器外部访问。你应该只在地址中添加端口部分,这样你的进程才能接受来自任何网络接口的连接。你可以按照以下方式定义你的服务器:

srv := &http.Server {
    Handler: router,
    Addr:    ":8007",
}
英文:

You're binding the socket to localhost address which cannot be reached from outside of your container. You should only add the port part in the address so that your process accepts connection from any network interface. You can define your server this way:

srv := &http.Server {
Handler: router,
Addr:    ":8007",
}

huangapple
  • 本文由 发表于 2022年7月8日 18:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/72910177.html
匿名

发表评论

匿名网友

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

确定