Ask Your Question

Revision history [back]

To send API requests to a different Docker container using NextJS and NodeJS, you can follow the steps below:

  1. First, ensure that both containers are running on the same Docker network.
  2. In the container that will be making the API requests, install the axios package using npm install axios.
  3. Import the axios package and create an instance of it, ensuring that the baseURL of the instance is set to the IP address of the container that you want to send requests to.
  4. Use the axios instance to send requests to the desired endpoint on the other container.

Here is some code that demonstrates how to make an API request to a container running a simple NodeJS server:

import axios from 'axios';

// Create an instance of axios with the baseURL of the container you want to send requests to
const apiClient = axios.create({
  baseURL: 'http://<ip_address_of_container>:<port_number>'
});

// Make a GET request to the desired endpoint on the other container
apiClient.get('/api/data').then(response => {
  console.log(response.data); // The response from the other container's endpoint
}).catch(error => {
  console.error(error);
});

You can customize this code to suit your specific needs, depending on the types of requests you need to make and the data you expect to receive in response.