Docker: 无法打开端口 8080

Docker: cannot open port 8080

我在容器内有一个 REST API,绑定到 /rest/getMail,其中 returns 一个 JSON 字符串。

我可以在容器内调用这个 API,使用 curl localhost:8080/rest/getMail 没有问题。

但是,我无法从外部调用此 API,即使我已经公开端口并进行了外部到内部端口绑定:docker run -d -i -p 2500:8080 myimage,或 docker run -d -i --expose 8080 -p 2500:8080 myimage。它显示:连接被拒绝。

我什至无法使用 IP 地址调用 api:curl 172.17.0.2:8080/rest/getMail

可能是什么问题?这真的很奇怪。 谢谢。

编辑:

当我执行 curl localhost:2500 时,它显示:

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

telnet localhost:2500

Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

curl localhost:2500/rest/getMail 给了我同样的结果。

看起来你应该点击 host_ip:host_port URL。您是主机上的 运行 第二个 curl 命令吗?如果是这样,端口应该是映射端口(即 2500,在您的示例中)。

你能在主机上试试运行吗:

curl localhost:2500/rest/getMail

还需要检查的一件事是主机上是否有阻止此连接的防火墙。