1+ # ###############################################################################
2+ # BASE #
3+ # ###############################################################################
14FROM python:3.11-slim-bookworm as base
25
36ARG POETRY_VERSION=1.8.3
47ARG UID=1000
58ARG GID=1000
69
10+ # Where python should look for packages and modules when using import
711ENV PYTHONPATH="/app"
812
13+ # Ensure the stdout and stderr streams are sent straight to terminal
14+ ENV PYTHONUNBUFFERED=1
15+
16+ # Extend the socket timeout. Default would be 15s
17+ ENV PIP_DEFAULT_TIMEOUT=100
18+
919RUN groupadd -g ${GID} -o app
1020RUN useradd -m -d /app -u ${UID} -g ${GID} -o -s /bin/bash app
1121
@@ -16,55 +26,61 @@ WORKDIR /app
1626
1727CMD ["tail" , "-f" , "/dev/null" ]
1828
19- # Both build and development need poetry, so it is its own step.
29+ # ###############################################################################
30+ # POETRY
31+ # ###############################################################################
32+ #
33+ # Both BUILD and DEVELOPMENT need poetry
34+ #
2035FROM base AS poetry
2136
2237RUN pip install poetry==${POETRY_VERSION}
2338
24- # Use this page as a reference for python and poetry environment variables:
25- # https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED Ensure
26- # the stdout and stderr streams are sent straight to terminal, then you can see
27- # the output of your application
28-
29- ENV PYTHONUNBUFFERED=1\
30- # Avoid the generation of .pyc files during package install
31- # Disable pip's cache, then reduce the size of the image
32- PIP_NO_CACHE_DIR=off \
33- # Save runtime because it is not look for updating pip version
34- PIP_DISABLE_PIP_VERSION_CHECK=on \
35- PIP_DEFAULT_TIMEOUT=100 \
36- # Disable poetry interaction
37- POETRY_NO_INTERACTION=1 \
38- POETRY_VIRTUALENVS_CREATE=1 \
39- POETRY_VIRTUALENVS_IN_PROJECT=1 \
40- POETRY_CACHE_DIR=/tmp/poetry_cache
39+ # Ensure that the virtual environment directory is in the project. This path
40+ # will be be `/app/.venv/`
41+ ENV POETRY_VIRTUALENVS_IN_PROJECT=1
4142
43+ # Create the virtual environment if it does not already exist
44+ ENV POETRY_VIRTUALENVS_CREATE=1
4245
46+ # ###############################################################################
47+ # BUILD #
48+ # ###############################################################################
49+ #
50+ # This step uses poetry to generate a requirements.txt file for PRODUCTION
51+ #
4352FROM poetry AS build
4453
45- # Just copy the files needed to install the dependencies
54+ # README.md is needed so that poetry command will work.
4655COPY pyproject.toml poetry.lock README.md ./
4756
48- # Use poetry to create a requirements.txt file. Dont include development dependencies
4957RUN poetry export --without dev -f requirements.txt --output requirements.txt
5058
51-
52- # We want poetry on in development
59+ # ###############################################################################
60+ # DEVELOPMENT #
61+ # ###############################################################################
62+ #
63+ # In development we want poetry in the container, so it inherits from the POETRY
64+ # step. This step is the place to install development-only sytem dependencies
65+ #
5366FROM poetry AS development
5467
55- # Install development system dependencies
5668# RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
5769# wget\
5870
59- # Switch to the non-root user "user"
6071USER app
6172
62-
63- # We don't want poetry on in production, so we copy the needed files form the build stage
73+ # ###############################################################################
74+ # PRODUCTION #
75+ # ###############################################################################
6476FROM base AS production
65- # Switch to the non-root user "user"
66- # RUN mkdir -p /venv && chown ${UID}:${GID} /venv
6777
78+ # Setting this to 'off' actually turns off the cache. This is set to decrease
79+ # the size of the image.
80+ ENV PIP_NO_CACHE_DIR=off
81+
82+ # Speed up pip usage by not checking for the version
83+ ENV PIP_DISABLE_PIP_VERSION_CHECK=on
6884
6985COPY --chown=${UID}:${GID} . /app
7086COPY --chown=${UID}:${GID} --from=build "/app/requirements.txt" /app/requirements.txt
0 commit comments