plaid / quickstart

Get up and running with Plaid Link and the API in minutes
https://plaid.com/docs/quickstart
MIT License
602 stars 645 forks source link

Unable to fetch link_token in Frontend UI: Connection refused to http://localhost:8000 #430

Closed jcranfordupgrade closed 9 months ago

jcranfordupgrade commented 1 year ago

I tried running docker-compose up node -d on Mac. When I open http://localhost:3000 on my host, I get an error in the UI page similar to #405.

Unable to fetch link_token: please make sure your backend server is running and that your .env file has been configured with your PLAID_CLIENT_ID and PLAID_SECRET.

However, it seems like the root cause and workaround might be different, and may require a fix in docker-compose.yml.

I did some googling, and it seems newer versions of Docker frown on using localhost for connections between containers. Docker seems to want people to use something else:

  1. Use container name (e.g. start-node-1) to point to a specific container.
  2. Use host.docker.internal to point to the host's localhost (i.e. not the container's localhost).
  3. Use extra_hosts?
  4. Probably other solutions?

To workaround the issue, I edited docker-compose.yml to set the REACT_APP_API_HOST environment variable in frontend service. After restarting docker-compose, the Frontend UI worked.

Launch Link

Both of these workarounds worked for me:

  1. REACT_APP_API_HOST=http://host.docker.internal:8000
  2. REACT_APP_API_HOST=http://start-node-1:8000

The second option is specific to using the node container. The first option might be more generic to work with other implementations of the API container (e.g. Java, Go, etc).

jcranfordupgrade commented 1 year ago

The versions I used:

% uname -a
Darwin J5R6K724LM-jcranford 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 arm64

% docker-compose --version
Docker Compose version v2.18.1

% docker --version 
Docker version 20.10.23, build 715524332f

Steps to reproduce the problem:

  1. Start API and Frontend containers: docker-compose up node -d
  2. From host, verify Frontend container is reachable via localhost: curl http://localhost:3000
  3. From host, verify API container is reachable via localhost: curl http://localhost:8000
  4. From host, open Frontend URL in a browser: http://localhost:3000/ => Displays this error within the page

    Unable to fetch link_token: please make sure your backend server is running and that your .env file has been configured with your PLAID_CLIENT_ID and PLAID_SECRET.

Steps to diagnose the problem:

  1. Open a shell into the Frontend container: docker exec -it start-frontend-1 sh
  2. Install curl: apk add curl
  3. Install nslookup: apk add net-tools
  4. Within the Frontend container, verify Frontend URL is reachable via localhost: curl http://localhost:3000
  5. Within the Frontend container, verify API URL is NOT reachable via localhost: curl http://localhost:8000
    curl: (7) Failed to connect to localhost port 8000 after 0 ms: Couldn't connect to server
  6. Within the Frontend container, verify API URL is reachable via start-node-1: curl http://start-node-1:8000 => It works!
  7. Within the Frontend container, verify API URL is reachable via host.docker.internal: curl http://host.docker.internal:8000 => It works!

Steps to apply the workaround:

  1. Edit docker-compose.yml
  2. For frontend service, set environment variable REACT_APP_API_HOST=http://start-node-1:8000
  3. Restart containers: docker-compose restart
  4. From host, open Frontend URL in a browser: http://localhost:3000/ => It works!
    Launch Link
phoenixy1 commented 1 year ago

Thanks for the report! I believe the issue here is the command used to start the Quickstart. When I tried starting the quickstart as you describe, using docker-compose up node -d, the command failed like you describe. When I tried starting the Quickstart according to the directions in the README, which specify using make up language=node (or whatever other language you want to use) it succeeded.