我正在使用 ubuntu 镜像在 docker 容器上的端口 26657 上运行 Tendermint HTTP RPC 服务器。
docker run -itd --name t1 -p 26657:26657endermint
当我在容器内执行curl 操作时,我从 RPC 服务器获取 JSON 响应。
docker exec -it t1 curl localhost:26657/status
但是当它尝试在主机上使用curl操作时,我收到错误:“curl:(52)来自服务器的空回复”
curl localhost:26657/status
最初我在 Windows 机器上运行 docker。我认为 Windows 可能存在无法与 WSL Linux 通信的问题(docker 在 Windows 中用于 ubuntu)。因此,我在运行 Ubuntu 20.04 的 Linux Virtual Box 中尝试了相同的命令。主机ubuntu仍然无法使用curl/web浏览器访问容器服务器。
我尝试在容器内的端口 80 运行 apache2 服务器并映射到端口 5000 (-p 5000:80)。这有效,我能够访问主机中 localhost:5000 的 apache2 。当我尝试在 RPC 服务器上将 26657 映射到 5000 (-p 5000:26657) 时,会发生同样的问题。因此,问题仅存在于容器中除端口 80 之外的端口。
我在主机上尝试了“netstat -an”,它显示:“TCP 0.0.0.0:26657 0.0.0.0:0 LISTENING”。
尝试运行一个简单的 Angular 11 HTTP lite 服务器,该服务器在 4200 上运行并映射到主机 4200。它也无法映射此端口。
还尝试远程登录到主机上的端口 26657,并能够使用“telnet localhost 26657”成功连接。当容器终止时,telnet 将无法连接,这是理想的情况。但仍然无法使用curl/web浏览器连接主机并从容器获取JSON响应。
我刚刚尝试在 Amazon EC2 实例上运行endermint RPC 服务器。我无法使用公共(public) IP 访问端口 26657。我猜想这与26657端口上的协议(protocol)有关。
最佳答案
尝试运行这个docker run -itd --name t1 -p 26657:26657endermint/tendermint
curl 本地主机:26657/status
{
"jsonrpc": "2.0",
"id": -1,
"result": {
"node_info": {
"protocol_version": {
"p2p": "8",
"block": "11",
"app": "1"
},
"id": "e0d34bb67c18d9025f6944de285f24e904de8c23",
"listen_addr": "tcp://0.0.0.0:26656",
"network": "dockerchain",
"version": "",
"channels": "40202111433038606100",
"moniker": "dockernode",
"other": {
"tx_index": "on",
"rpc_address": "tcp://0.0.0.0:26657"
}
},
"sync_info": {
"latest_block_hash": "5E59DCD523F574AA5B3961A9523B660716FDF9A3D90C72294285106E99614F18",
"latest_app_hash": "0000000000000000",
"latest_block_height": "170",
"latest_block_time": "2021-05-16T12:08:26.1797685Z",
"earliest_block_hash": "746D1580F6B7012D042230791CCED002AC327422D144BEE7BC2B203E1ECD6F39",
"earliest_app_hash": "",
"earliest_block_height": "1",
"earliest_block_time": "2021-05-16T12:06:43.2085282Z",
"catching_up": false
},
"validator_info": {
"address": "ACC91433A4B84C4A53547A58418001CD55677F28",
"pub_key": {
"type": "tendermint/PubKeyEd12519",
"value": "+hGKJ2w6dRevN0t2OEf/1uHoInggirrSHCDzyhwJuOo="
},
"voting_power": "10"
}
}
}
它可以在我的带有 wsl2 + ubuntu 20.04 的 Windows 10 计算机上运行..
关于docker - 无法向主机公开端口 80 以外的 docker 容器端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67556056/