ConnectionError on multi-Docker app (Streamlit + fastAPI) when deployed on Heroku

I have a multi-Docker application with a Streamlit frontend and a FastAPI backend deployed on Heroku:

The code is on GitHub: (add an OPENAPI_API_KEY to a .env file if you want to reproduce).

It has:

locally, I can do:

docker compose build
docker compose up

and go to, ask a math question and get an answer:

app working locally

But after deploying the app on Heroku with

heroku create
heroku stack:set container
git push heroku main
heroku open

and trying the same math question there, I get the error:

ConnectionError: HTTPConnectionPool(host='api', port=8080): Max retries exceeded with url: /ask (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f9f5e00d040>: Failed to establish a new connection: [Errno -2] Name or service not known'))
File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/", line 565, in _run_script
    exec(code, module.__dict__)
File "/app/", line 34, in <module>
File "/app/", line 24, in main
    response = requests.request("POST", host, headers=header, data=payload)
File "/usr/local/lib/python3.9/site-packages/requests/", line 59, in request
    return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/", line 587, in request
    resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/", line 701, in send
    r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/", line 520, in send
    raise ConnectionError(e, request=request)

How do I fix this?

were you able to solve this problem? I’m running into a similar one, trying to connect streamlit and fastapi. works locally but problems within docker build.

I think it’s related to something due to hardcoding of “localhost”, but not sure.

I don’t know your setup at all, but it should be noted that localhost in a container always points to the container itself and not to the host where the docker daemon is running. This is a common misunderstanding.

were you able to solve this problem?

I was able to solve my problem following the second suggestion posted here:

Another option could be to run both the api and web on the same HTTP server instance in the same container. For example, serving the api code under /api route and the web under /app route. You would only run one image that contains both the web and api code combined.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.