search

Home  >  Q&A  >  body text

API calls across Docker containers

<p>I am trying to use the provided API from one docker container (let's say <code>container_A</code>) to access another container (<code>container_B</code>). These two containers are in the same Docker network. Before adding Docker I would of course make API calls to <code>http://localhost:{app_port}/{endpoint}</code> and now that I have Dockerized the application, each service is in a Running in a Docker container, I know from reading other questions and websites that you have to use the alias of the container if they are on the same network, or the service name if using <code>docker compose</code> For example <code>http://container_A:8000/books</code>. I've done this but it doesn't work. I configured CORS in all services and also tried accessing the endpoint from within the <em>REST Client</em> (which is a Postman-like VSCode extension) to see if that worked, and it did. I also did port mapping/port exposure. </p> <p>Just to provide a little more background, I want to use the API from one Docker container containing a Node.js application to access another container containing a React application. </p> <p>Any ideas on why this doesn't work? </p>
P粉764003519P粉764003519461 days ago498

reply all(1)I'll reply

  • P粉956441054

    P粉9564410542023-08-30 13:02:20

    Thanks to @DavidMaze's comment, I started experimenting with using localhost instead of the name of the React application container, since apparently React runs in the browser. In my case the URL for each container looks like this: http://containerName:port and I use that URL when requests are made between them (since all these containers are in the same Docker network), but whenever I want to make a request from the React container, I use http//localhost:port.

    • containerName is the container name for your application. For example: http://webapp:3000
    • port is the exposed port. For example: 3000

    Feel free to edit to add more useful information if necessary.

    reply
    0
  • Cancelreply