An app can work on your laptop and fail on a colleague’s because a package, Python version, or operating system differs. A container image packages the app with a defined environment so it can be run consistently wherever Docker is available. A Dockerfile describes how to build the image; a running container is created from it. section 12.2.1 section 12.1.3
Docker builds an image layer by layer from Dockerfile instructions. For a Streamlit app, the recipe starts with `python:3.12-slim`, sets `/app` as the working directory, installs packages from `requirements.txt`, copies the code, and specifies how to start Streamlit. Copying and installing requirements before the rest of the code lets Docker use its layer cache when app code changes. section 12.2.3
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"]
The complete Dockerfile shows which instructions prepare the image and which settings and command its container will use at startup. section 12.2.3
Check what goes into the image before building it. `COPY . .` would also copy a local `.streamlit/secrets.toml` file. Because someone who obtains the image could extract embedded secrets, add `.streamlit/` to `.dockerignore`. The app then needs its secrets supplied separately at runtime; a local run can mount the excluded folder into the container. section 12.2.4 section 12.2.5
Build, inspect, and run locally
From the app directory, run `docker build -t fact-frenzy .`, then `docker images` to check for the built image. Start a container with `docker run -p 8502:8501 fact-frenzy` and open `http://localhost:8502`. If the app needs the excluded `.streamlit` folder locally, use `docker run -p 8502:8501 -v ./.streamlit/:/app/.streamlit fact-frenzy`. The build does not start Streamlit: the Dockerfile’s `CMD` runs when the container starts. Port mapping connects your computer’s port 8502 to the container’s port 8501. section 12.2.5
The same image-based approach extends to cloud deployment. On Elastic Beanstalk, an EC2 instance runs a container instantiated from the Dockerfile-built image, while a load balancer forwards browser requests to it. The image packages the app; the surrounding cloud services handle infrastructure and traffic. section 12.3.3
























