An image layer is an immutable filesystem change that contributes to a Docker image. Docker builds images layer by layer, so each Dockerfile instruction adds a snapshot of the environment prepared for the application. section 12.2.3 section 12.2.1
Layers can contain a base image, a working directory, installed dependencies, or application files. Putting requirements.txt and dependency installation before the rest of the application allows Docker to reuse that dependency layer when source files change. section 12.2.3
A Streamlit image
A Dockerfile starts from python:3.12-slim, copies requirements.txt, installs its packages, and then copies the application. These instructions create successive layers that can be reused in a container running the Streamlit app. 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"]
Inspect the complete Dockerfile to see how base-image selection, dependency installation, application files, configuration, and startup are arranged as build instructions. section 12.2.3




