A Streamlit app can work on your laptop but fail elsewhere because Python versions, packages, or operating systems differ. Docker packages the app and its dependencies into a reproducible image, so it can run in a consistent environment locally or in the cloud. section 12.2.1 section 12.1.3
A Dockerfile defines the environment and builds an image layer by layer. A container is a running instance of that image. For a Streamlit app, the container holds the application code, Python, Streamlit, dependencies, and the Linux files it needs. Docker Engine creates and runs containers; Docker Desktop supplies a Linux virtual machine when the host needs one. section 12.2.1 section 12.2.2
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 before copying the rest of the code to use layer caching, configures Streamlit, and sets the startup command. section 12.2.3
To prepare an existing Streamlit app for deployment:
- List its Python dependencies in requirements.txt, then write a Dockerfile that installs them and copies the app code. section 12.2.3
- Use docker build to create an image and docker run to start a container. Forward a host port to the port where Streamlit listens inside it; for example, host port 8502 can reach container port 8501. section 12.6 section 12.2.2
- Keep secrets and configuration out of the image when appropriate: a mounted volume can share files between host and container. section 12.2.2
For cloud deployment, AWS Elastic Beanstalk can run a container built from the Dockerfile on an EC2 instance. Its load balancer routes requests to the instance, which passes them to the container. The EB CLI can detect the Dockerfile and offer a Docker platform. A database that needs its own container requires a multi-container setup beyond this single-container approach. section 12.3.3 section 12.3.6 section 12.2.3

























