DEV Community

Bart Walczak
Bart Walczak

Posted on

React app in Docker fetch can't find a server

I've got a React app build with Create-React-App running inside a docker container.

This code:

    const req = new Request(
        apiAdminUrl + '/login', {
        method: 'POST',
        body: formData
    }
    return fetch(req).then(processResp, processErr);
Enter fullscreen mode Exit fullscreen mode

Returns TypeError: Load failed, "Failed to load resource: A server with the specified hostname could not be found."

But the curl request from the same container works without issues:

curl -d "email=admin@some.etc" -d "pwd=xxx" -X POST http://api-admin:3000/v1/login
Enter fullscreen mode Exit fullscreen mode

CORS is enabled in the api code. apiAdminUrl is properly set as http://api-admin:3000/v1.

What am I missing?

Top comments (2)

Collapse
 
tqbit profile image
tq-bit

You can also add a hostname to your container, such as api.localhost. Or use a reverse proxy container, such as nginx or traefic.

Collapse
 
bartwalczak profile image
Bart Walczak

Ok, solved. As pointed to me elsewhere, obviously local browser environment doesn't see the network structure of the container. I need to use the local api address.