diff --git a/Dockerfile b/Dockerfile index 9a4471e..4e8433d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ -FROM python:3.10.13-slim-bookworm +FROM python:3.10.13-slim-bookworm as python-base # `FLASK_ENV` arg is used to make prod / dev builds: -ARG FLASK_ENV=production +ARG FLASK_ENV="production" # Needed for fixing permissions of files created by Docker: ENV FLASK_ENV=${FLASK_ENV} \ @@ -15,29 +15,28 @@ ENV FLASK_ENV=${FLASK_ENV} \ PIP_DISABLE_PIP_VERSION_CHECK=1 \ PIP_DEFAULT_TIMEOUT=100 \ PIP_ROOT_USER_ACTION=ignore \ - # tini: - TINI_VERSION=v0.19.0 \ # poetry: POETRY_VERSION=1.6.1 \ POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_CREATE=false \ POETRY_CACHE_DIR='/var/cache/pypoetry' \ - POETRY_HOME='/usr/local' + POETRY_HOME='/usr/local' \ + # paths + # this is where our requirements + virtual environment will live + PYSETUP_PATH="/opt/pysetup" \ + VENV_PATH="/opt/pysetup/.venv" -WORKDIR /app - +# prepend poetry and venv to path +ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" # System deps (we don't use exact versions because it is hard to update them, # pin when needed): # hadolint ignore=DL3008 +FROM python-base as builder-base RUN apt-get update && apt-get upgrade -y \ && apt-get install --no-install-recommends -y \ - bash \ - brotli \ build-essential \ curl \ - git \ - libpq-dev \ # Installing `poetry` package manager: # https://github.com/python-poetry/poetry && curl -sSL 'https://install.python-poetry.org' | python - \ @@ -49,24 +48,28 @@ RUN apt-get update && apt-get upgrade -y \ # Copy only requirements, to cache them in docker layer +WORKDIR $PYSETUP_PATH COPY ./poetry.lock ./pyproject.toml /app/ # Project initialization: # hadolint ignore=SC2046 -RUN --mount=type=cache,target="$POETRY_CACHE_DIR" \ - echo "$FLASK_ENV" \ - && poetry version \ - # Install deps: - && poetry run pip install -U pip \ - && poetry install \ - $(if [ "$FLASK_ENV" = 'production' ]; then echo '--only main'; fi) \ - --no-interaction --no-ansi --sync --no-dev +RUN poetry install --no-dev -# Copy the app code: -COPY . . +# `production` image is used during production / testing +FROM python-base as production +ENV FLASK_ENV="production" +WORKDIR $PYSETUP_PATH -EXPOSE 5000 +# copy in our built poetry + venv +COPY --from=builder-base $POETRY_HOME $POETRY_HOME +COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH + +# quicker install as runtime deps are already installed +RUN poetry install --no-dev -# Run the app: +# will become mountpoint of our code +WORKDIR /app + +EXPOSE 5000 CMD ["poetry", "run", "python", "app.py"]