Bypass DNS lookup axios/http – target via IP

  axios, dns, docker, node.js

I’m relatively new to how NodeJS handles DNS lookups/resolutions but is it possible to bypass NodeJS’ default behaviour of doing a DNS lookup when you send a HTTP request?

I have 5 NodeJS’ services running on the same DO box on the same docker network. These services need to communicate with each other via HTTP. They all, currently, use axios to make requests to these other services. I know the subnet of the docker network so when using docker run, I set the IP for the service (using --ip) to run on that network. Then, in it’s sibling services, I use these IPs to target them.

With axios I’ve found that the only way to send a request is via initially configuring it in the instance with baseURL opt or providing it on each request so my axios instance looks something like:

const axios = require('axios')
const http = require('http')

module.exports = axios.create({
        baseURL: 'http://172.18.0.6:3004',
        httpAgent: new http.Agent({
            keepAlive: true
        }),
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json'
        }
    })
}

Up until now, I have used a HTTP Agent to persist the connection so a DNS lookup is not done quite so often. However, I have 1 service (I’ll call ‘Service A’) whereby it’s now being overloaded with a lot more requests than it did previously &, as a result, I end up with a lot of ECONNRESET or EPIPE errors as the connections are been severed by that service. When this happens (which is frequent), I just retry the request until it succeeds.

It’s worth mentioning that 1 of the services (‘Service B’) that communicates with Service A, has many worker threads which all create their own axios instance & communicate with Service A. That is where the bulk of requests are coming from. Services C, D, & E, only send about 1 request to Service A every 30 secs.

Before I used the agent to persist the connection, I was getting an ENOTFOUND error on nearly every request send from Service B to Service A. I imagine this is being thrown by DNS lookup due to being synchronous & failing in some form.

So, because I already have the IPv4 that DNS lookup will give me back anyway, is it possible for a HTTP client (not married to the idea of axios) to target via IP & bypass DNS lookup?

My current step is to add an entry to the hosts file docker uses by using --add-host opt but I imagine the results will that I’ll get fewer errors but it’s still doing a DNS lookup & seems a bit irrelevant to be adding an entry to the hosts file to map the IP to the…well…IP?

Source: Docker Questions

LEAVE A COMMENT