pythondockerdeploymentstreamlitlanggraph

Runtime Error: No Running Event Loop on Streamlit with Docker


Phase 1: The app I made: https://github.com/tanvircr7/CRAG/

I first built without docker in mind. I used python virtual environment and everything was working.

Then I started containerising and the problems started

This error kept popping up: https://pastebin.com/FsGanN8N

Phase 2: I deciding to start fresh with docker and then work my way up to the final app

github link of the new project: https://github.com/tanvircr7/corrective-rag

Dockerfile:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.12
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Create data directory with full permissions
RUN mkdir -p /app/data && \
    chmod 777 /app/data && \
    chown -R appuser:appuser /app/data  # Optional: specific user ownersh

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8501

# Run the application.
CMD ["streamlit", "run", "./app/main.py", "--server.port=8501", "--server.address=0.0.0.0"]

compose.yaml file

services:
  server:
    build:
      context: .
    ports:
      - 8501:8501
    volumes:
      - ./app:/app/app
      - ./src:/app/src
      - ./data:/app/data:rw  # :rw explicitly states read-write access

Now I just have a simple file upload application.

When I run it, it starts up on port 8501.

But The same error keeps popping up: https://pastebin.com/FsGanN8N

The error code:

Exception in callback AppSession._on_scriptrunner_event.<locals>.<lambda>() at /usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py:518

handle: <Handle AppSession._on_scriptrunner_event.<locals>.<lambda>() at /usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py:518>

Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/streamlit/web/bootstrap.py", line 347, in run

if asyncio.get_running_loop().is_running():
^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop

During handling of the above exception, another exception occurred: 


Traceback (most recent call last):
File "/usr/local/lib/python3.11/asyncio/events.py", line 84, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py", line 518, in <lambda>
lambda: self._handle_scriptrunner_event_on_event_loop(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py", line 612, in _handle_scriptrunner_event_on_event_loop
msg = self._create_new_session_message(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py", line 742, in _create_new_session_message
_populate_user_info_msg(imsg.user_info)
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/app_session.py", line 990, in _populate_user_info_msg
inst = Installation.instance()
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 228, in instance
cls._instance = Installation()
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 236, in __init__
self.installation_id_v4 = _get_machine_id_v4()
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 209, in _get_machine_id_v4
with file_util.streamlit_write(filepath) as output:
File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/streamlit/file_util.py", line 109, in streamlit_write
os.makedirs(os.path.dirname(path), exist_ok=True)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/nonexistent'

Solution

  • Try creating the home dir as something is trying to write to it

    RUN adduser \
        --disabled-password \
        --gecos "" \
        --home "/nonexistent" \
        --shell "/sbin/nologin" \
        --create-home \
        --uid "${UID}" \
        appuser