python-dockerfile-app-sample/Dockerfile
2024-04-23 17:34:07 +00:00

49 lines
2.1 KiB
Docker

### SYSTEM GENERATED FILE, MAKE CHANGES AS NEEDED ###
# Build a virtualenv using the appropriate Debian release
# * Install python3-venv for the built-in Python3 venv module (not installed by default)
# * Install gcc libpython3-dev to compile C Python modules
# * In the virtualenv: Update pip setuputils and wheel to support building new packages
FROM debian:11-slim AS build
RUN apt-get update && \
apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev && \
python3 -m venv /venv && \
/venv/bin/pip install --upgrade pip setuptools wheel
# Build the virtualenv as a separate step: Only re-execute this step when packages changes
FROM build AS build-venv
# Install flask for starting the web server
RUN /venv/bin/pip install -U Flask waitress gunicorn
### COMMENT AS NEEDED ###
# Check if requirements.txt exists before copying
# If the path exists, copy requirements.txt into the builder stage
# Using glob pattern so that build does not fail if requirements.txt does not exist
COPY requirements.txt /requirements.txt
### DEFAULT BOOTSTRAP BUILD CODE, RUN LOCALLY TO GENERATE requirements.txt ###
# If requirements.txt exists and not empty, install the listed packages
# If it doesn't exist or is empty, print a message and skip the installation
RUN if [ -s /requirements.txt ]; then \
/venv/bin/pip install --disable-pip-version-check -r /requirements.txt; \
else \
echo "No requirements.txt found, skipping installation"; \
fi
# Start a new stage with a distroless image for smaller image size and improved security
FROM gcr.io/distroless/python3-debian11
# Copy the /venv directory from the builder stage into the current stage
# This includes the application code and the dependencies
COPY --from=build-venv /venv /venv
# Copy the rest of the application code
COPY . /python-buildpack-func-sample
# Set /python-buildpack-func-sample as the working directory in the current stage
WORKDIR /python-buildpack-func-sample
# Set the command to run when the container starts
# This will start the application by running entrypoint file
ENTRYPOINT [ "/venv/bin/python3", "autogen_index.py" ]