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: https://morning-everglades-39854.herokuapp.com/

The code is on GitHub: https://github.com/BioGeek/streamlit-fastapi-langchain (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 http://172.19.0.3:8501, 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'))
Traceback:
File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "/app/streamlit_app.py", line 34, in <module>
    main()
File "/app/streamlit_app.py", line 24, in main
    response = requests.request("POST", host, headers=header, data=payload)
File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", 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.