Google-api-nodejs-client: Docker image running have getaddrinfo eai_again maps.googleapis.com:443

Created on 12 Oct 2017  路  7Comments  路  Source: googleapis/google-api-nodejs-client

Im using the Express generator to generate the pug view
Im using the npm 5.4.2 and nodejs 6.x
Im using the npm start my app without any problem
but when I build this app into a docker image, and then run it.
It comes out the problem like dns problem.

image

  function createGoogleOptions(query){
    var options = {
        hostname: 'maps.googleapis.com',
        port: 443,
        path: '/maps/api/geocode/json?',
        method: 'GET'
    };

    var str = 'address=' + query +'&key=' + 'AIzaSyA9wNCIiINgXZITnyP1p3MGMWTdaWpJtMk';
    options.path += str;
    console.log(options.path);
    return options;
  }


var express = require('express');
var router = express.Router();
const request =require('request');

var https = require('https');
var http = require('http');

/* GET home page. */
router.get('/', function(appReq, appRes, next) {
  console.log(appReq.url);
  console.log(appReq.path);
  console.log(appReq.query.location);


  function createGoogleOptions(query){
    var options = {
        hostname: 'maps.googleapis.com',
        port: 443,
        path: '/maps/api/geocode/json?',
        method: 'GET'
    };

    var str = 'address=' + query +'&key=' + 'AIzaSyA9wNCIiINgXZITnyP1p3MGMWTdaWpJtMk';
    options.path += str;
    console.log(options.path);
    return options;
  }

    function createYelpOptions(term,lat, lng){

    var options = {
      hostname: 'api.yelp.com',
      port: 443,
      path: '/v3/businesses/search?',
      method: 'GET',
      headers: {
        'Authorization' : 'Bearer lTy6fRnbm063oo_X3itFala7Y5yb6DS8wIlX_oteHqDr0xn5yHNwXCXu14cLMKj6WobnGVCOSMSNQIvXxRyvdkwYRwu3BKEP13GbiQCWcOCm72C3jf2ee8tkvy3GWXYx'
        //'user-key' : 'eefc2a80e3a6ccdee95930f59d476b56'
      }
    };

    term= 'hotel';

    var str = 'term='+ term + '&latitude=' + lat + '&longitude=' + lng ;
    options.path += str;
    console.log(options.path);

    return options;
  }

  var query = appReq.query.location.replace(/ /g, "+");
  var options = createGoogleOptions(query);
  var googleReq = https.request(options, function(googleRes){

        var body = [];
        googleRes.on('data', function(chunk){
            body.push(chunk);
        });

        googleRes.on('end', function(){
            var bodyString = body.join("");
            var geoRsp = JSON.parse(bodyString);
            console.log("Google result : " + geoRsp.status);            
            if(geoRsp.status == "OK"){
                var location_result = geoRsp.results[0].geometry.location;
                console.log("Geolocation API response" + appReq.query.location);
                console.log("Lat : " + location_result.lat + " Lng : " + location_result.lng);
                var yelpOptions = createYelpOptions('hotel',location_result.lat,location_result.lng);
                var yelpReq = https.request(yelpOptions, function(yelpRes){
                  console.log('headers: ', yelpRes.headers);
                  console.log('statusCode: ', yelpRes.statusCode);
                  var body = '';
                  yelpRes.on('data',function(chunk) {
                    body += chunk;
                  });
                  yelpRes.on('end', function() {
                    var yelpRsp = JSON.parse(body);
                    console.log("Result found :" + yelpRsp.total);
                    appRes.setHeader("content-type","text/html");
                    appRes.setHeader("Access-Control-Allow-Origin", "*");
                    appRes.render('hotel',{yelpRsp});
                    appRes.end();
                  });
                });

                yelpReq.on('error', (e) => {
                    console.error(e);
                });

                yelpReq.end();

            } else {
                console.log("Result not found !!!!");
                appRes.setHeader("Content-Type", "text/html");
                appRes.setHeader("Access-Control-Allow-Origin", "*");
                var hotelResult = {'totalHotelCount' : 0
                };
                appRes.render('index',{});
                appRes.end();
            }
        });
    });

    googleReq.on('error', (e) => {
       console.error(e);
    });

    googleReq.end();
});
module.exports = router;

Thanks!

needs more info

Most helpful comment

Restarted docker - solved it for me.

All 7 comments

@kody2015 Unfortunately, this seems to be a problem on Docker rather than the library. Can you recheck the configuration, proxy, etc. on Docker?

how I can upload my project to here...I m checking the configuration of Docker.
the proxy I think has no problem, as im using the QUT network, and my home network in AU.
But I just dont know how to solve it..

@kody2015 if you could create a small GitHub that just has the necessary code, including your Dockerfile that would be super helpful. I highly doubt this is a problem with the library, but if I know what Docker image you're using we can make double sure.

hi, I got same problem like this. Till now I still don't know how to solve. I have project API run on docker. After I run it on my host, then hit API via Postman the connection internet on my host computer being slowed also the response via Postman too. After waiting for about 10 minutes, Postman got the response with error

getaddrinfo EAI_AGAIN

and the connection internet on my host computer being normal too. Here I give Dockerfile I used
FROM mhart/alpine-node:7 WORKDIR /src ADD . /src EXPOSE 1337 CMD ["npm", "start"]

Please help me how to solved

Greetings folks! I don't think there's anything special we can do in this library to help with DNS issues inside of docker. The one suggestion I will make is to be very careful about which docker base image you use. You probably should only be using the official images from the node.js project:
https://hub.docker.com/_/node/

I know this sounds silly, but I'd also suggest googling about for "docker dns" and see what comes up. Y'all aren't the only ones having these issues, and there are some great suggestions out there. Sorry I don't have a better answer!

Restarted docker - solved it for me.

@MartinMuzatko answer solved this issue when you have variable names for mapping url to ip best to give docker a restart and then it should be able to resolve them .
sudo systemctl restart docker

Was this page helpful?
0 / 5 - 0 ratings