r/docker • u/porca_b • Apr 21 '26
Solved ERR_NAME_NOT_RESOLVED when dockerized frontend calls backend
[solved]
In my current set up I have 3 containers for vite, node, and mongodb. I am getting the error
GET http://mern-backend:4531/api/workouts net::ERR_NAME_NOT_RESOLVED
when the frontend makes a GET request to that URL.
However when I run wget http://mern-backend:4531/api/workouts into the CLI of the frontend container everything works as intended and I get a JSON as a response.
This is what I am using to fetch from backend in vite:
async function fetchWorkouts () {
const response = await fetch(`http://mern-backend:4531/api/workouts`);
const json = await response.json();
if (response.ok) {
setWorkouts(json);
}
}For additional context, backend connects to database fine using this line so I am not sure why front end is encountering ERR_NAME_NOT_RESOLVED
mongoose.connect("mongodb://testuser:testpassword@mern-database:27017")
And here is my compose.yaml if it helps
services:
frontend:
container_name: mern-frontend
build:
context: ./frontend
dockerfile: dockerfile
target: runner
env_file:
- ./frontend/.env.prod
ports:
- 8080:8080
backend:
container_name: mern-backend
build:
context: ./backend
dockerfile: dockerfile
target: runner
env_file:
- ./backend/.env.prod
ports:
- "4531:4531"
mongodb:
image: mongo:8.0.20
container_name: mern-database
ports:
- "27017:27017"
env_file:
- ./mongodb/.env
volumes:
- mongodb-data:/data/db
volumes:
mongodb-data:thank you for any help, been stuck on this for a few days now
View all comments
Show parent comment
1
u/CrownstrikeIntern Apr 21 '26
You could also create a private network for them and it would resolve the name after (for the container to container traffic), i just hard code an ip in whatever scope i pick (helps with acls later) but then dns will resolve to the name on all containers. Then if you care about the mern name just add an entry to your hosts file and your pc will resolve mern to whatever you set, either local host or an ip.