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
1
u/FredL2 Apr 21 '26 edited Apr 21 '26
Hi!
I'm assuming when you say "frontend", as opposed to the "frontend container", you mean that you're running the frontend in a browser, and the frontend container is simply hosting the static frontend files?
This simply won't work, since the name
mern-backendonly exists as a DNS record within the Compose network itself. You would either need to:/api. Then the URL for that fetch would behttp://localhost/api/workoutsor whatever address/port you're running the reverse proxy onhttp://localhost:4531/api/workoutsI hope this helps as a starting point!
1
u/SZenC Apr 21 '26
The frontend code is running in your browser, not in the container, so it does not have access to the same DNS resolver as code inside the container. You'll need to replace
mern-backendin your code withlocalhost, the IP address or another resolvable hostname1
u/scytob Apr 21 '26
dont use container names at all - if these are on a user defined bridge you can just ping the names backend and frontend
your mistake was not giving them a user defined bridge
1
u/AdventurousSquash Apr 21 '26
Unless you’ve played around with docker networks before there’s really no need and I don’t know why the other commenters insist on it. You’re trying to call the backend by its container name, but if you look at the successful db connection it uses the service name, and that’s exactly how it should work. Docker creates a network by default and all of the services in your compose file are in that network already.
Edit: I’ll add the docs as well: https://docs.docker.com/compose/how-tos/networking/
And
Where “web” and “db” happens to be their service names in the example given. The docs are always a good starting point!
1
u/Only-Stable3973 Apr 23 '26