An app that works on your laptop may fail elsewhere because Python, packages, or operating systems differ. Docker helps by packaging the app and its environment into an image. A container is a running instance of that image: a small, self-contained environment for the app. section 12.2.1
For a Streamlit app, the container includes the application code, Python, Streamlit, its dependencies, and minimal Linux files and utilities. A Dockerfile defines how to build the image; its CMD instruction names the command that starts when the container runs, not when the image is built. section 12.2.2 section 12.2.5
Listing 12.2 Dockerfile
# Start with Python 3.12 as base image FROM python:3.12-slim # Set working directory in the container WORKDIR /app # Copy requirements file and install dependencies # (We do this first to use Docker's layer caching) COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the application COPY . . # Expose the port Streamlit runs on EXPOSE 8501 # Set Streamlit to run in headless mode ENV STREAMLIT_SERVER_PORT=8501 ENV STREAMLIT_SERVER_HEADLESS=true ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0 # Command to run when the container starts CMD ["streamlit", "run", "main.py"]
This Dockerfile starts from Python 3.12, installs requirements, copies the app, configures Streamlit, and sets its startup command. section 12.2.3
Run the app locally
From the app directory, run `docker build -t fact-frenzy .` to create the image, then `docker run -p 8502:8501 fact-frenzy` to start a container. Open `http://localhost:8502`. The port mapping forwards requests from your computer’s port 8502 to Streamlit’s port 8501 inside the container; the container’s own URL is not directly accessible from the host browser. section 12.2.5
A container can share selected host files through a mounted volume. This lets the app read secrets or configuration at runtime without including them in the image. In a cloud deployment, an EC2 instance runs a container from the image, and a load balancer routes browser requests to it. section 12.2.2 section 12.3.3


























