首页 > 解决方案 > Docker 端口暴露问题,Recv 失败:对等方重置连接

问题描述

我正在尝试在 docker 容器中运行 Go 应用程序二进制文件。该应用程序有一些 gRPC 请求正在监听和服务器:

http.ListenAndServe("localhost:8081", nil)

在我的 docker-compose.yaml 中。我有一个映射到 8081 的应用服务:

  golangAPP:
    build:
      context: .
      dockerfile: ./docker/golangAPP/Dockerfile
    depends_on:
      - setup
    ports:
      - 8081:8081

docker-compose up我看到正在提供应用程序的详细信息之后。

但我仍然无法达到它。curl -X OPTIONS http://localhost:8081返回

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

如果我在没有 docker 的情况下在本地运行二进制文件,那么我可以向应用程序发送请求。

有什么建议吗?我做了一些谷歌搜索和一些防火墙问题。但我不确定如何进行。

标签: dockergonetworking

解决方案


如果你这样做:

http.ListenAndServe("localhost:8081", nil)

这将监听来自环回接口的连接。在容器中运行时,这将只接受来自该容器内的连接(或者如果您在 k8s pod 中运行它,则在同一个 pod 中)。所以:

http.ListenAndServe(":8081", nil)

这将接受环回和外部连接(容器外部)。


推荐阅读