admin管理员组

文章数量:1193969

I am running a Node.js application inside a Docker container, and I'm using Axios to make HTTP requests to an external API. The same request works fine when running the app on my local machine, but when I try to make the request from inside the Docker container, I get the following error:

AxiosError: Request failed with status code ENOTFOUND

Here's the Axios code I’m using:

const axios = require('axios');

const fetchData = async () => {
    try {
        const response = await axios.get('');
        console.log(response.data);
    } catch (error) {
        console.error('Error: ', error.message);
    }
};

fetchData();

The container is able to access the internet for other operations like pulling Docker images, but when trying to connect to the external API, it fails with the ENOTFOUND error.

  1. I tried using --network host to run the container on the host machine's network, but the error still occurs.

  2. I expected the request to be able to connect to the external API from inside the Docker container, just as it does on my local machine.

本文标签: