Reputation: 25
I have 2 apps running as services under docker-compose.
I am trying to make an HTTP request from my React app to the Node.js server by calling:
fetch("http://backend:4000/")
I get GET http://backend:4000/ net::ERR_NAME_RESOLUTION_FAILED
in the browser when I access my React app under http://localhost:3000
This is my docker-compose file.
version: "3.7"
services:
frontend:
build:
context: ./frontend
volumes:
- ./frontend:/app
ports:
- "3000:3000"
depends_on:
- "backend"
backend:
build:
context: ./backend
volumes:
- ./backend:/app
I'm running Ubuntu and I have been having this problem for ages. Nothing really helps - creating a network and assigning services to them, creating links and many others.
One curious thing is that when I get into the React app container and I run bash command:
ping backend
or curl http://backend:4000
, the request actually works fine.
Upvotes: 0
Views: 6273
Reputation: 134
My guess is that, you are trying to connect your backend app through your browser not through frontend container. If that is the case, then you should reveal the backend port like;
backend:
container_name: backend
build:
context: ./backend
volumes:
- ./backend:/app
ports: #whatever the port number
- "4000:4000"
then you should change the url from http://backend:4000 to http://localhost:4000 on your react app.
Upvotes: 2
Reputation: 808
version: "3.7"
services:
frontend:
build:
context: ./frontend
volumes:
- ./frontend:/app
ports:
- "3000:3000"
depends_on:
- "backend"
links:
- backend
backend:
container_name: backend
build:
context: ./backend
volumes:
- ./backend:/app
Incorrect option
links:
- "backend:test"
"backend:test"
is the reason you're getting a name resolution error.
That is not the service name, backend
is.
Upvotes: 0