I'm currently working with a setup that includes three Docker containers: one for a Vite frontend, one for a Node backend, and one running MongoDB. I'm encountering an `ERR_NAME_NOT_RESOLVED` error when trying to make a GET request from the frontend to the backend at the URL `http://mern-backend:4531/api/workouts`. Interestingly, when I run `wget http://mern-backend:4531/api/workouts` from the CLI of the frontend container, it fetches the JSON response just fine. My frontend makes the request using an asynchronous function, but it seems to fail when attempting to fetch the backend URL directly. The backend connects to the database without issue, so I'm puzzled about why this DNS resolution error is occurring with the front end. Also, I'd like to share my `compose.yaml` file for more context.
2 Answers
You're right that the frontend code is actually running in your browser. Because of that, it doesn't have access to the same DNS records as what's inside the Docker containers. You'll need to change `mern-backend` to `localhost` or another valid hostname in your fetch request. This should resolve the DNS issue you're facing!
Hey! I think the confusion here comes from how you're accessing your frontend. When you're saying "frontend," you're probably running it in a browser. Just so you know, the name `mern-backend` only works within the Docker Compose network, not in your browser. Here are a few options to fix this:
1. **Reverse Proxy Setup**: Consider setting up a reverse proxy that exposes your backend, so the URL would look something like `http://localhost/api/workouts`.
2. **Frontend as Proxy**: Another way is to have the frontend container act as a reverse proxy during development.
3. **Expose Backend Port**: You could also expose the backend port (4531) directly to the host's network. Then, the URL would be `http://localhost:4531/api/workouts`.
Hope this points you in the right direction!

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically