docker - 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 的情况下在本地运行二进制文件,那么我可以向应用程序发送请求。
有什么建议吗?我做了一些谷歌搜索和一些防火墙问题。但我不确定如何进行。
解决方案
如果你这样做:
http.ListenAndServe("localhost:8081", nil)
这将监听来自环回接口的连接。在容器中运行时,这将只接受来自该容器内的连接(或者如果您在 k8s pod 中运行它,则在同一个 pod 中)。所以:
http.ListenAndServe(":8081", nil)
这将接受环回和外部连接(容器外部)。
推荐阅读
- arrays - Excel:遍历列数组以查找不同列数组和对数组中的重复值
- python - 带有烧瓶的 webhook 上的 Telegram 机器人
- sql - SQL 获取另一个表中最接近整数的记录
- javascript - 检查文件夹名称中的一些特殊字符
- javascript - Vue CLI 3 应用程序:NPM 构建上未捕获的 SyntaxError
- android - 位置更新在设备管理员/托管工作配置文件模式下不起作用
- sql - 在列中搜索一个值并给出另一个值
- visual-studio-code - VSCode和flutter,如何连接多个设备?
- web-applications - 网站和Web应用程序之间有什么区别?
- java - 检查 pdf 文档是否包含嵌入文件的最佳方法是什么?