无法通过节点应用程序访问docker容器内的url,但是卷曲到相同的url可以工作

时间:2018-10-29 09:17:20

标签: node.js docker proxy docker-compose request-promise

我正在尝试访问URL的微服务。设置了授权和内容类型的标题。 NPM运行正常。

使用curl使用Authorization标头访问相同的URL也会得到结果。但是我在通过完整代码访问URL时遇到此错误。

"message":"3. Error while making request - Error: tunneling socket could not be established, cause=connect EINVAL 0.0.12.56:80 - Local (0.0.0.0:0)","level":"error"}

代码使用request-promise-native模块

const request = require('request-promise-native')
const logger = require(process.env.APP_SERVICES_DIR + 'logger');

function getRequestAPI(task) {

  console.log(`Basic ${Buffer.from(
    `${process.env.USERNAME}:${process.env.PASSWORD}`
  ).toString('base64')}`);

  return {
    url: getQueryForTask(task),
    headers: {
      Authorization: `Basic ${Buffer.from(
        `${process.env.USERNAME}:${process.env.PASSWORD}`
      ).toString('base64')}`,
      'Content-Type': 'application/json'
    },
    resolveWithFullResponse: true
  };
}


function makePortalRequest(task) {
  logger.info(`Making request for task ${task.task_key}`);

  return request(getRequestAPI(task))
    .then(response => {
      // logger.info('Response received');
      console.log("get request API" );
      try {
        const responseBody = JSON.parse(response.body);

        // Parse result and store
        return parseResultAndStore(responseBody, task).then(() => {
          return {
            success: true
          };
        });
      } catch (e) {
        logger.error(
          `Fetch Task: Error in parsing response from portal - ${e.message}`,
          e
        );
        return {
          success: false,
          message: e.message
        };
      }
    })
    .catch(e => {
      let errResponse = e.response || e.message;

      if (errResponse.body) {
        let responseBody = '';
        // wrap JSON parse in try catch
        try {
          responseBody = JSON.parse(errResponse.body);
          errResponse = `${
            errResponse.statusMessage
          } : ${responseBody.errorMessages.toString()}`;
          logger.error(`1. Error while making request - ${errResponse}`);
        } catch (e) {
          // if JSON parse fails log the string
          logger.error(
            `2. Error while making request - ${errResponse.body}`
          );
          errResponse = errResponse.body;
        }
      } else {
        logger.error(`3. Error while making request - ${errResponse}`);
      }

      return {
        success: false,
        message: errResponse
      };
    });
}

此服务在docker容器中运行。服务组件由docker-compose文件描述,并且所有环境变量均已适当设置。通过运行以下命令,我可以在docker容器中打开bash。

docker-compose exec run service-name bash

curl命令能够从docker bash内部访问URL。

curl -X GET 'https://url-to-fetch/?params=xyz' -H 'Authorization: Basic aDEwMjZzNzpXaXB0ZWxAMjAxOF8='

但是在运行的代码中,我无法从url获得输出响应。

"message":"3. Error while making request - Error: tunneling socket could not be established, cause=connect EINVAL 0.0.12.56:80 - Local (0.0.0.0:0)","level":"error"}

如何解决此问题?该错误的可能原因是什么?

0 个答案:

没有答案
相关问题