0

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
  • You don't say what your host OS is, but on most platforms and environments the Docker-internal IP addresses are unusable and it's not useful to either specify them or look them up. Your browser application needs to connect to the host's DNS name (in a development setup where the browser and server container are on the same host, localhost will work) and the published docker run -p port 4002. Commented Feb 26, 2022 at 11:12
  • 1
    BTW: 119.18.0.0/16 is not a valid private ip range, it belongs to the "APNIC" and should not be used for private networks. Commented Feb 26, 2022 at 13:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Its working now with the request sent to: localhost:4002/listen, the dockerfile port expose as: -p 4002:4000 and instance server: const port = 4000 Thanks a lot
Glad I could help. Don't forget to mark the question as solved and select an answer. :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.