docker frontend can’t access backend: ERR_NAME_NOT_RESOLVED

  docker, docker-compose, reactjs, ruby-on-rails

I have 2 services running through docker-compose:

  1. backend, ports 3000:3000, Rails
  2. frontend, port 8000:8000, React

The frontend cannot connect to the backend due to these errors:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED on google chrome, and something related with CORS on firefox.

both failed on the url: http://backend:3000

Actually, it makes sense because the browser has no idea what is that url, the backend service name is only known within docker. I can access localhost:3000 on the browser, and it works.

Already added:

"proxy": "http://localhost:3000

to the frontend package.json and

origins('*')
resource '*', headers: :any, methods: [:get, :post, :put, :delete, :options]

to the backend rails cors configuration.

I think the proxy is supposed to be http://backend:3000 and the API url http://127.0.0.1:3000. But the application doesn’t even start with these settings, I get this error:

Error: connect ECONNREFUSED 127.0.0.1:3000

Any idea what am I doig wrong?

Source: Docker Questions

One Reply to “docker frontend can’t access backend: ERR_NAME_NOT_RESOLVED”

LEAVE A COMMENT