A Streamlit app can work on your laptop and fail elsewhere because Python or a required library is missing. A Dockerfile helps prevent that mismatch. It is a file named Dockerfile, with no extension, that tells Docker how to build an image containing the environment and code your app needs. Docker runs that image as a container, locally or on another machine. section 12.2.1 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"]
Use this complete Dockerfile as a starting point for a Streamlit app whose entry point is main.py and whose dependencies are in requirements.txt. section 12.2.3
Read the instructions in build order:
- FROM selects the python:3.12-slim base image; WORKDIR makes /app the working directory. section 12.2.3
- COPY brings in requirements.txt, and RUN installs its packages. Keeping these steps before COPY . . lets Docker reuse the dependency layer when only app code changes. section 12.2.3
- COPY . . adds the remaining files. EXPOSE 8501 identifies the app’s port, and ENV sets Streamlit’s port, headless mode, and listening address. section 12.2.3
- CMD names the command to start Streamlit. Unlike RUN, it does not start the app while the image is being built; it runs when a container starts. section 12.2.3 section 12.2.5
Before building, check what COPY . . could include. If .streamlit contains secrets.toml, copying it into a shareable image exposes those secrets. Put .streamlit/ in a .dockerignore file beside the Dockerfile, along with other files you do not want copied. Then provide the excluded configuration separately at runtime; otherwise an app that needs those secrets will not have them. section 12.2.4 section 12.2.5
Build and test locally
From the app directory containing the Dockerfile, run docker build -t fact-frenzy . and then docker run -p 8502:8501 fact-frenzy. Open http://localhost:8502: the port option forwards your computer’s port 8502 to the container’s port 8501. If the app needs the excluded .streamlit directory, the local example mounts it with docker run -p 8502:8501 -v ./.streamlit/:/app/.streamlit fact-frenzy. section 12.2.5
The same Dockerfile also supports cloud deployment. Elastic Beanstalk can detect it and offer a Docker platform; its running instances then host containers built from the image. The Dockerfile defines what goes into the image and what starts inside the container, while deployment determines how users reach it. section 12.3.6 section 12.3.3

















