Skip to content

Commit

Permalink
build: add docker setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ttpss930141011 committed Sep 26, 2023
1 parent 0154d3a commit cb57cc8
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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} \
Expand All @@ -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 - \
Expand All @@ -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"]

0 comments on commit cb57cc8

Please sign in to comment.