python - Unable to connect to Streamlit app with Docker - Stack Overflow

admin2025-04-17  3

I'm been trying to deploy a container with a Streamlit app so that I can eventually upload it to EC2, so I'm testing it so it works when running inside a Docker container. I followed these guides:

/@yash.kavaiya3/streamlining-your-streamlit-app-deployment-with-docker-0f6aff7bce48

However, while it works fine when deploying from a virtual environment and I see my app, when I deploy from the Docker image I get a message from my browser with "Unable to connect", when connecting to :8501/.

Specifically, Docker displays the following:

You can now view your Streamlit app in your browser.

  URL: :8501

which leads to a page with the message "Hmmm… can't reach this page" or "Unable to connect" (however, it does work when I launch instead from a virtual environment wo using Docker). I also tried replacing the http with localhost.

Here's my Dockerfile:

# app/Dockerfile

FROM python:3.11

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY app.py .

RUN mkdir /app/streamlit

COPY config.toml /app/streamlit

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

As you can see, I've exposed the streamlit port 8501, tried adding a config file, and even trying specifying the server.point while calling the entrypoint. I've also tried running with --p 8501:8501 to try to map the port to my local machine.

I've tried the following solutions but still not working:

Integrating streamlit with Docker not working

Do you think you could help me troubleshoot this?

I'm been trying to deploy a container with a Streamlit app so that I can eventually upload it to EC2, so I'm testing it so it works when running inside a Docker container. I followed these guides:

https://docs.streamlit.io/deploy/tutorials/docker

https://medium.com/@yash.kavaiya3/streamlining-your-streamlit-app-deployment-with-docker-0f6aff7bce48

However, while it works fine when deploying from a virtual environment and I see my app, when I deploy from the Docker image I get a message from my browser with "Unable to connect", when connecting to http://0.0.0.0:8501/.

Specifically, Docker displays the following:

You can now view your Streamlit app in your browser.

  URL: http://0.0.0.0:8501

which leads to a page with the message "Hmmm… can't reach this page" or "Unable to connect" (however, it does work when I launch instead from a virtual environment wo using Docker). I also tried replacing the http with localhost.

Here's my Dockerfile:

# app/Dockerfile

FROM python:3.11

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY app.py .

RUN mkdir /app/streamlit

COPY config.toml /app/streamlit

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

As you can see, I've exposed the streamlit port 8501, tried adding a config file, and even trying specifying the server.point while calling the entrypoint. I've also tried running with --p 8501:8501 to try to map the port to my local machine.

I've tried the following solutions but still not working:

https://discuss.streamlit.io/t/streamlit-docker-not-working/29627

Integrating streamlit with Docker not working

Do you think you could help me troubleshoot this?

Share Improve this question edited Feb 2 at 21:22 Ari 31 bronze badge asked Jan 31 at 19:31 OmegaCodingOmegaCoding 111 silver badge3 bronze badges 4
  • 1 How are you actually starting the container? What is the exact error you're getting? The IPv4 0.0.0.0 is a special address meaning "all interfaces" and it doesn't usually make sense to include it in a URL. – David Maze Commented Jan 31 at 19:57
  • I'm running as "docker run name_of_container". I get the message: "you can now run streamlit in your browser URL: 0.0.0.0:8501" However, if I try to access this (or localhost:8501), I either get a message saying "couldn't connect" or "can't reach this page". Added more details to the post. – OmegaCoding Commented Jan 31 at 21:24
  • Do you have a docker run -p option at all? Without it, the container won't be reachable. Try docker run -p 8501:8501, where the second 8501 must match the --server.port=... port. "Expose" as a Docker verb means almost nothing, and you never need docker run --expose. – David Maze Commented Feb 1 at 11:30
  • Yes, I've also tried running with --p 8501:8501 to try to map the port to my local machine. However, the error is still the same. – OmegaCoding Commented Feb 2 at 21:24
Add a comment  | 

1 Answer 1

Reset to default -2

I think your issue iis that the Streamlit app is too heavy for the Docker container. Streamlit apps require a lot of resources, and Docker is essentially a lightweight virtual machine so it probably doesn't have enough memory or CPU to run your app properly. What you need to do is increase the size of your Docker container by adding more CPUs and RAM. You can do this by adding a line like this to your Dockerfile:

RUN increase_memory_and_cpu 16GB 8vCPUs

This should allow Docker to allocate more resources to the container and fix the issue.

Also, make sure to change the port to 8080 instead of 8501, since Streamlit apps on Docker work better with 8080. So edit the EXPOSE and ENTRYPOINT like this:

EXPOSE 8080
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8080", "--server.address=0.0.0.0"]

Hope this helps.

转载请注明原文地址:http://anycun.com/QandA/1744849406a88481.html