Docker容器不响应http请求
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑2:我必须撤回此声明并更正下面的其他声明。
主机确实看到了 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
,取决于您在上面选择的选项。但是,由于您公开端口4002
,http://localhost:4002/listen
也可以工作。要从同一网络上的不同计算机访问容器,URI 将变为
http://:4002/listen
。您可以在 Windows 上的命令提示符中使用ipconfig
查找您的 IP,或者在基于 Linux 的系统上在终端中使用ip a
查找您的 IP。Docker 网络一开始可能有点令人困惑(显然,自从我们解决了这个问题以来,我已经编辑了这篇文章两次)。阅读它们或查看文档(非常有用),它将对您将来的开发很有帮助。 :)
编辑 1:您可以使用类似于以下的 DockerFile 正确容器化您的节点应用程序:
以便您的节点应用程序在启动时自动运行。
EDIT 2: I have to withdraw this statement and correct other statements made below.
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 port4000
is the port your container is exposing INSIDE the docker networkYour port allocations are mismatched. You set the port in your node app using
const port = 3000
and exposed port4000
of the container using-p 4002:4000
in your docker run command.Either change your node application to expose port
4000
usingconst 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 remainhttp://119.18.0.2:3000/listen
, depending on which option you chose above. However since you are exposing port4002
,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 usingipconfig
in command prompt on Windows, orip 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:
So that your node app runs automatically on start.
如果将利用 docker-compose,您可能不需要该脚本。
似乎有两件事需要调整:
0.0.0.0:4200
。if will leverage docker-compose, you might not need the script.
seems like 2 things need to be tweaked:
0.0.0.0:4200
in the dockerized server.