Skip to content

Commit

Permalink
chore: add poetry config instead of requirements.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Nov 15, 2023
1 parent b3ddad9 commit 8791d8a
Show file tree
Hide file tree
Showing 5 changed files with 1,155 additions and 29 deletions.
94 changes: 78 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
# syntax = docker/dockerfile:1.2
# Base user uid / gid keep 1000 on prod, align with your user on dev
ARG USER_UID=1000
ARG USER_GID=1000


FROM python:3.11
# Instructions from https://fastapi.tiangolo.com/deployment/docker/
ARG USER_UID
ARG USER_GID
RUN groupadd -g $USER_GID off && \
useradd -u $USER_UID -g off -m off && \
ARG PYTHON_VERSION=3.11

# base python setup
# -----------------
FROM python:$PYTHON_VERSION-slim as python-base
RUN apt-get update && \
apt-get install --no-install-suggests --no-install-recommends -y curl && \
apt-get autoremove --purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv" \
POETRY_HOME="/opt/poetry" \
POETRY_VERSION=1.6.1 \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

# building packages
# -----------------
FROM python-base as builder-base
RUN curl -sSL https://install.python-poetry.org | python3 -
WORKDIR $PYSETUP_PATH
COPY poetry.lock pyproject.toml ./
RUN poetry install --without dev

# This is our final image
# ------------------------
FROM python-base as runtime
COPY --from=builder-base $VENV_PATH $VENV_PATH
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
RUN poetry config virtualenvs.create false
ENV POETRY_VIRTUALENVS_IN_PROJECT=false

# create off user
ARG OFF_UID=1000
ARG OFF_GID=$OFF_UID
RUN groupadd -g $OFF_GID off && \
useradd -u $OFF_UID -g off -m off && \
mkdir -p /home/off && \
mkdir -p /opt/open-prices && \
chown off:off -R /opt/open-prices /home/off
WORKDIR /opt/open-prices
COPY --chown=off:off requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY --chown=off:off app /opt/open-prices/app

COPY docker/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

COPY --chown=off:off poetry.lock pyproject.toml /opt/open-prices/

USER off:off
COPY --chown=off:off app app
WORKDIR /opt/open-prices
ENTRYPOINT /docker-entrypoint.sh $0 $@

CMD ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]


# building dev packages
# ----------------------
FROM builder-base as builder-dev
WORKDIR $PYSETUP_PATH
COPY poetry.lock pyproject.toml ./
# full install, with dev packages
RUN poetry install

# image with dev tooling
# ----------------------
# This image will be used by default, unless a target is specified in docker-compose.yml
FROM runtime as runtime-dev
COPY --from=builder-dev $VENV_PATH $VENV_PATH
COPY --from=builder-dev $POETRY_HOME $POETRY_HOME
# Handle possible issue with Docker being too eager after copying files
RUN true
COPY .flake8 pyproject.toml ./
# create folders that we mount in dev to avoid permission problems
USER root
RUN \
mkdir -p /opt/open-prices/.cov && \
chown -R off:off /opt/open-prices/.cov
USER off
13 changes: 13 additions & 0 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -e

# activate our virtual environment here
. /opt/pysetup/.venv/bin/activate

PRE_ARGS=()

# You can put other setup logic here

# Evaluating passed command:
exec "${PRE_ARGS[@]}" "$@"
Loading

0 comments on commit 8791d8a

Please sign in to comment.