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)
2
Answers
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.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 option
-p 4002:4000
is what is exposing your docker container to the host(and the network you’re connected to).4002
is the port exposed to the host and port4000
is the port your container is exposing INSIDE the docker networkTo access the container from the host your URI would become
http://localhost:4002/listen
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, orifconfig
in terminal on Linux based systems.Secondly, your 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
.Docker networks can be a bit confusing at first. Read up on them or check the documentation(hella useful), it will serve you well in future development. 🙂
EDIT: You can properly containerize your node application using a DockerFile similar to this:
So that your node app runs automatically on start.