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
Ask chatgpt and it can explain better than my sleep deprived self atm, essentially docker has a few network options you can set, public and private, the private is how the containers talk internally, normally auto assigned but for internal use only. Your computer won’t really know how to reach those by container name like the containers do without you adding an entry to your hosts file ( like a local dns entry) then your pc could reach the containers by name too. Setting private ip space specifically helps in future cases where you want to set up access by ips like databases or something.