问题
I have two Docker containers, one running a React app (built using create-react-app) and another with a Node app API. I have a docker-compose file set up, and according to the documentation I should be able to use the names of the services to communicate between containers.
However, when I try to send a request to the /login
endpoint of my API from the React app I received a net::ERR_NAME_NOT_RESOLVED
error. I'm using Unirest to send the request.
I've done a bunch of digging around online and have come across a few things describing similar issues but still haven't been able to find a solution. When I run cat /etc/resolve.conf
(see this issue) in my React container the container with my API doesn't show up, but Docker is still fairly new to me so I'm not sure if that's part of the issue. I've also tried using links and user-defined networks in my compose file but to no avail.
I've included gists of my docker-compose.yml
file as well as the code snippet of my request. Any help is much appreciated!
docker-compose.yml
Unirest request to /login
回答1:
As discussed in the comments on the original post this was a DNS issue. Configuring the DNS was a little to involved for the use case of this project, so I've solved my problem by using an environment variable to set the URL that is used to make calls to my API container based on whether I'm running on a dev or prod environment:
(process.env.REACT_APP_URL_ROOT || '/app')
I set REACT_APP_URL_ROOT
to my localhost address when running locally, and have an nginx container configured to proxy to /app
when I build and deploy the React app.
来源:https://stackoverflow.com/questions/48573612/unable-to-have-docker-containers-communicate-with-each-other