2

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

  • Does pinging `api` host on docker works? – LMC Feb 01 '18 at 23:56
  • @LuisMuñoz Yes. If I run `docker exec -it amplifydemo_reactapp_1 ping api` I can see responses coming through. – Turner Vink Feb 01 '18 at 23:59
  • Good, so where is the error appearing then? – LMC Feb 02 '18 at 00:02
  • When using the React app in my browser. I have a login form that makes a call to the `/login` endpoint of my API running in the `api` container. When I submit the form I get `http://api:8080/login net::ERR_NAME_NOT_RESOLVED` in the browser console. – Turner Vink Feb 02 '18 at 00:03
  • DNS problem! You have to replace `api` with the hostname resolvable from your browser! – LMC Feb 02 '18 at 00:06
  • What hostname would that be? Am I able to set it in my compose file or is it set by Docker? – Turner Vink Feb 02 '18 at 00:08
  • Ideally it's a public know hostname but for development purposes you can use docker instance IP or add an entry on your /etc/hosts file (at your browser's box) – LMC Feb 02 '18 at 00:10
  • When I inspect the api container to get it's IP address it's empty. Is that where I'm supposed to find the address? I ran `docker inspect ` where I got the ID from `docker ps` – Turner Vink Feb 02 '18 at 00:23
  • Not an expert on docker but `docker exec -it api ip addr` or `docker exec -it api netstat -nlt` could help – LMC Feb 02 '18 at 00:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164375/discussion-between-luis-munoz-and-turner-vink). – LMC Feb 02 '18 at 00:32

1 Answers1

0

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.