liveBook · Manning

Container

As explained in Build Python Web Apps with Streamlit

A Docker container is an isolated runtime environment created from a container image.

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

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through Dockerfile creation, image building, container startup, port mapping, and mounted files; leaves multi-container PostgreSQL setup aside.
Examples
Uses a Streamlit trivia app, a Python-based Dockerfile, build and run commands, and a host-to-container port mapping.
What it assumes
Assumes an existing Streamlit app while introducing Docker installation, images, containers, and the Dockerfile workflow.
What you can do after
Operate/configure it with Docker by building an image, starting a container, and opening the mapped local port.
How it connects
Connects Dockerfiles and images to port forwarding, volume mounting, Streamlit, and Elastic Beanstalk deployment.

Also explained in

Compare how 27 books explain container →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.