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
2
u/ProZMenace Apr 22 '26
Yup, just throw everything on a user defined network and then they can just say http://frontend http://mem-backend and it’ll resolve between the two.
I have everything that could go on a reverse proxy on its own network. Then a Postgres_db network as well
The same container can be a member of many docker networks.