1+ # app/Dockerfile
2+
3+ # # Stage 1 - Install build dependencies
4+
5+ # A Dockerfile must start with a FROM instruction which sets the base image for the container.
6+ # The Python images come in many flavors, each designed for a specific use case.
7+ # The python:3.11-slim image is a good base image for most applications.
8+ # It is a minimal image built on top of Debian Linux and includes only the necessary packages to run Python.
9+ # The slim image is a good choice because it is small and contains only the packages needed to run Python.
10+ # For more information, see:
11+ # * https://hub.docker.com/_/python
12+ # * https://docs.streamlit.io/knowledge-base/tutorials/deploy/docker
13+ FROM python:3.11-slim AS builder
14+
15+ # The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
16+ # If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
17+ # For more information, see: https://docs.docker.com/engine/reference/builder/#workdir
18+ WORKDIR /app
19+
20+ # Set environment variables.
21+ # The ENV instruction sets the environment variable <key> to the value <value>.
22+ # This value will be in the environment of all “descendant” Dockerfile commands and can be replaced inline in many as well.
23+ # For more information, see: https://docs.docker.com/engine/reference/builder/#env
24+ ENV PYTHONDONTWRITEBYTECODE 1
25+ ENV PYTHONUNBUFFERED 1
26+
27+ # Install git so that we can clone the app code from a remote repo using the RUN instruction.
28+ # The RUN comand has 2 forms:
29+ # * RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
30+ # * RUN ["executable", "param1", "param2"] (exec form)
31+ # The RUN instruction will execute any commands in a new layer on top of the current image and commit the results.
32+ # The resulting committed image will be used for the next step in the Dockerfile.
33+ # For more information, see: https://docs.docker.com/engine/reference/builder/#run
34+ RUN apt-get update && apt-get install -y \
35+ build-essential \
36+ curl \
37+ software-properties-common \
38+ git \
39+ && rm -rf /var/lib/apt/lists/*
40+
41+ # Create a virtualenv to keep dependencies together
42+ RUN python -m venv /opt/venv
43+ ENV PATH="/opt/venv/bin:$PATH"
44+
45+ # Clone the requirements.txt which contains dependencies to WORKDIR
46+ # COPY has two forms:
47+ # * COPY <src> <dest> (this copies the files from the local machine to the container's own filesystem)
48+ # * COPY ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
49+ # For more information, see: https://docs.docker.com/engine/reference/builder/#copy
50+ COPY requirements.txt .
51+
52+ # Install the Python dependencies
53+ RUN pip install --no-cache-dir --no-deps -r requirements.txt
54+
55+ # Stage 2 - Copy only necessary files to the runner stage
56+
57+ # The FROM instruction initializes a new build stage for the application
58+ FROM python:3.11-slim
59+
60+ # Sets the working directory to /app
61+ WORKDIR /app
62+
63+ # Copy the virtual environment from the builder stage
64+ COPY --from=builder /opt/venv /opt/venv
65+
66+ # Set environment variables
67+ ENV PATH="/opt/venv/bin:$PATH"
68+
69+ # Clone the app.py containing the application code
70+ COPY app.py .
71+
72+ # Copy the images folder to WORKDIR
73+ # The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.
74+ # For more information, see: https://docs.docker.com/engine/reference/builder/#add
75+ ADD images ./images
76+
77+ # The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
78+ # For more information, see: https://docs.docker.com/engine/reference/builder/#expose
79+ EXPOSE 8501
80+
81+ # The HEALTHCHECK instruction has two forms:
82+ # * HEALTHCHECK [OPTIONS] CMD command (check container health by running a command inside the container)
83+ # * HEALTHCHECK NONE (disable any healthcheck inherited from the base image)
84+ # The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working.
85+ # This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections,
86+ # even though the server process is still running. For more information, see: https://docs.docker.com/engine/reference/builder/#healthcheck
87+ HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
88+
89+ # The ENTRYPOINT instruction has two forms:
90+ # * ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
91+ # * ENTRYPOINT command param1 param2 (shell form)
92+ # The ENTRYPOINT instruction allows you to configure a container that will run as an executable.
93+ # For more information, see: https://docs.docker.com/engine/reference/builder/#entrypoint
94+ ENTRYPOINT ["streamlit" , "run" , "app.py" , "--server.port=8501" , "--server.address=0.0.0.0" ]
0 commit comments