Docker容器不响应http请求

发布于 2025-01-10 01:15:58 字数 1677 浏览 0 评论 0原文

我正在尝试通过 axios 从本地主机(节点服务器)发送 http 请求到属于 docker 网络并由特定 IP 标识的 docker 容器(节点中也包含一个简单的服务器)。

我使用过 postman、xmlhttprequests 和 axios,但似乎没有任何效果。我也尝试过获取和发布请求,但其中任何一个都从容器端得到任何答案。

你知道我做错了什么吗?

我运行来启动容器的 .sh 文件是:

docker build -t connectimg .
docker network create --subnet=119.18.0.0/16 mynet
docker run -d --name instance2 -p 4002:4000 --net mynet --ip 119.18.0.2 connectimg

实例启动后的 docker 日志结果是:

{
  lo: [
    {
      address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '127.0.0.1/8'
    }
  ],
  eth0: [
    {
      address: '119.18.0.2',
      netmask: '255.255.0.0',
      family: 'IPv4',
      mac: '02:42:77:12:00:02',
      internal: false,
      cidr: '119.18.0.2/16'
    }
  ]
}
Example app listening on port 3000

我的 Docker 实例节点应用程序代码是:

const express = require('express')
const app = express()
const port = 3000
const cors = require('cors')
var os = require('os');

app.use(cors());
app.use(express.json());

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

app.get('/listen', (req,res) => {
    console.log('got it');
})


var networkInterfaces = os.networkInterfaces();

console.log(networkInterfaces);

我的节点服务器代码段负责向实例发送 get 请求是:

const connect  = (req,res) => {
    axios.get('http://119.18.0.2:3000/listen').then(resp => {
    console.log(resp.data);
});
}

我不断收到的错误是:

ETIMEDOUT 119.18.0.2:3000    
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)

I'm trying to send an http request through axios, from my localhost (node server) to a docker container (which contains a simple server in node too) which belongs to a docker network, and identified by an specific IP.

I have used postman, xmlhttprequests, and axios but nothing seems to work. I have also tried with get and post requests but any of those get any answer from the container side.

Do you have any Idea of what am I doing wrong?

the .sh file that Im running to launch the container is:

docker build -t connectimg .
docker network create --subnet=119.18.0.0/16 mynet
docker run -d --name instance2 -p 4002:4000 --net mynet --ip 119.18.0.2 connectimg

and the docker logs result for the instance post-launch is:

{
  lo: [
    {
      address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '127.0.0.1/8'
    }
  ],
  eth0: [
    {
      address: '119.18.0.2',
      netmask: '255.255.0.0',
      family: 'IPv4',
      mac: '02:42:77:12:00:02',
      internal: false,
      cidr: '119.18.0.2/16'
    }
  ]
}
Example app listening on port 3000

My Docker Instance Node app code is:

const express = require('express')
const app = express()
const port = 3000
const cors = require('cors')
var os = require('os');

app.use(cors());
app.use(express.json());

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

app.get('/listen', (req,res) => {
    console.log('got it');
})


var networkInterfaces = os.networkInterfaces();

console.log(networkInterfaces);

And my Node server piece of code responsible of sending the get request to the instance is:

const connect  = (req,res) => {
    axios.get('http://119.18.0.2:3000/listen').then(resp => {
    console.log(resp.data);
});
}

and the error I keep getting is:

ETIMEDOUT 119.18.0.2:3000    
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2025-01-17 01:15:58

编辑2:我必须撤回此声明并更正下面的其他声明。

首先,您的 URI http://119.18.0.2:3000/listen 不正确。这
docker网络无法直接访问,因为它不是一个网络
楼主知道的。

主机确实看到了 Docker 网络!

选项 -p 4002:4000 是将 docker 容器暴露给网络的。 4002 是向网络公开的端口,而端口 4000 是容器在​​ docker 网络内部公开的端口 您的

端口分配不匹配。您可以使用 const port = 3000 在节点应用程序中设置端口,并在 Docker 中使用 -p 4002:4000 公开容器的端口 4000运行命令。

使用 const port = 4000 更改节点应用程序以公开端口 4000

使用以下命令更改 docker run 命令以公开容器的端口 3000 -p 4002:3000

此后,要从主机访问容器,您的 URI 将变为 http://119.18.0.2:4000/listen 或保持 http://119.18.0.2:3000/listen ,取决于您在上面选择的选项。但是,由于您公开端口 4002http://localhost:4002/listen 也可以工作。
要从同一网络上的不同计算机访问容器,URI 将变为 http://:4002/listen。您可以在 Windows 上的命令提示符中使用 ipconfig 查找您的 IP,或者在基于 Linux 的系统上在终端中使用 ip a 查找您的 IP。

Docker 网络一开始可能有点令人困惑(显然,自从我们解决了这个问题以来,我已经编辑了这篇文章两次)。阅读它们或查看文档(非常有用),它将对您将来的开发很有帮助。 :)

编辑 1:您可以使用类似于以下的 DockerFile 正确容器化您的节点应用程序:

FROM node:lts-alpine3.15
LABEL maintainer="MyDevName"
WORKDIR /usr/app
COPY ./myNodeApp ./
RUN npm install
CMD ["npm", "start"]

以便您的节点应用程序在启动时自动运行。

EDIT 2: I have to withdraw this statement and correct other statements made below.

Firstly, your URI http://119.18.0.2:3000/listen is incorrect. The
docker network cannot be accessed directly as it is not a network that
the host knows of.

The host does indeed see the Docker networks!

The option -p 4002:4000 is what is exposing your docker container to your network. 4002 is the port exposed to the network and port 4000 is the port your container is exposing INSIDE the docker network

Your port allocations are mismatched. You set the port in your node app using const port = 3000 and exposed port 4000 of the container using -p 4002:4000 in your docker run command.

Either change your node application to expose port 4000 using const port = 4000

OR

Change your docker run command to expose port 3000 of the container by using -p 4002:3000.

Thereafter to access the container from the host your URI would become http://119.18.0.2:4000/listen or remain http://119.18.0.2:3000/listen, depending on which option you chose above. However since you are exposing port 4002, http://localhost:4002/listen would also work.
To access the container from a different machine on the same network the URI would become http://<ip-address-of-this-machine>:4002/listen. You can find your IP using ipconfig in command prompt on Windows, or ip a in terminal on Linux based systems.

Docker networks can be a bit confusing at first (evidently, I've edited this post twice since we fixed the issue). Read up on them or check the documentation (extremely useful), it will serve you well in future development. :)

EDIT 1: You can properly containerize your node application using a DockerFile similar to this:

FROM node:lts-alpine3.15
LABEL maintainer="MyDevName"
WORKDIR /usr/app
COPY ./myNodeApp ./
RUN npm install
CMD ["npm", "start"]

So that your node app runs automatically on start.

小女人ら 2025-01-17 01:15:58

我运行来启动容器的 .sh 文件是:

docker build -t connectimg 。
docker网络创建--subnet=119.18.0.0/16 mynet
docker run -d --name instance2 -p 4002:4000 --net mynet --ip 119.18.0.2 connectimg

如果将利用 docker-compose,您可能不需要该脚本。

我正在尝试通过 axios 从我的本地主机(节点服务器)发送一个 http 请求到属于 docker 网络并由特定 IP 标识的 docker 容器(节点中也包含一个简单的服务器)。

似乎有两件事需要调整:

  1. 在 Docker 化服务器中使用 0.0.0.0:4200
  2. 验证与容器关联的网络是否可以从主机操作系统访问。如果docker网络不是那么重要,你可以放弃它

the .sh file that Im running to launch the container is:

docker build -t connectimg .
docker network create --subnet=119.18.0.0/16 mynet
docker run -d --name instance2 -p 4002:4000 --net mynet --ip 119.18.0.2 connectimg

if will leverage docker-compose, you might not need the script.

I'm trying to send an http request through axios, from my localhost (node server) to a docker container (which contains a simple server in node too) which belongs to a docker network, and identified by an specific IP.

seems like 2 things need to be tweaked:

  1. use 0.0.0.0:4200 in the dockerized server.
  2. verify the network that you associate with the container is reachable from your host operating system. if the docker network is not that important, you can just ditch it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文