首页 > 解决方案 > 使用 wget 访问本地服务器时出现读取错误

问题描述

我有一个节点应用程序在端口 5000 上运行(来自 docker 容器),但我无法访问它。

我在炮击服务器后尝试运行wget http://localhost:5000,但出现以下错误:

--2021-09-09 18:44:44--  http://localhost:5000/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

--2021-09-09 18:45:17--  (try: 2)  http://localhost:5000/
Connecting to localhost (localhost)|::1|:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.```

When I run the container on my local computer, I have no trouble accessing it this way, so I'm guessing it's something on the server, but I don't know where to look next.


EDITS

Output of `sudo netstat -anp --tcp | grep 5000 | grep LISTEN`


tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      16480/docker-proxy
tcp6       0      0 :::5000                 :::*                    LISTEN      16486/docker-proxy

标签: node.jsdockerubuntu

解决方案


在服务器上,检查端口 5000 是否正在侦听

netstat -anp --tcp | grep 5000 | grep LISTEN

它应该告诉你有东西在听127.0.0.1:50000.0.0.0:5000。前者是环回地址(只能在服务器内部访问),后者是服务器上具有 IP 的任何接口(因此,如果本地防火墙允许,它可以在服务器外部访问)。

如果 5000 上没有任何东西在监听,那么 docker 容器很可能没有暴露端口。

  • 您可以放入EXPOSE 5000您的 Dockerfile。
  • 您可以添加--expose 5000到您的docker run命令
  • 您可以添加-p 5000:5000到您的docker run命令

如果您在本地计算机上运行它时可以访问端口 5000,则它可能正在工作,因为您添加了一个环境变量来自动公开该端口。


推荐阅读