API calls across Docker containers
P粉764003519
2023-08-29 19:43:11
<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>
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 usehttp//localhost:port
.http://webapp:3000
3000
Feel free to edit to add more useful information if necessary.