FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/python:3.9-slim-builder as builder

FROM builder as build-venv

WORKDIR /app

COPY requirements/ /app/requirements/

RUN python -m venv /venv \
    && . /venv/bin/activate \
    && pip --no-cache-dir install --upgrade pip setuptools wheel \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/base.txt \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/dev.txt \
    && pip --no-cache-dir wheel --wheel-dir=/tmp/wheelhouse -r requirements/tests.txt

FROM build-venv as build-base

RUN . /venv/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/base.txt

FROM build-base as build-dev

RUN . /venv/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/dev.txt

FROM build-dev as build-test

RUN . /venv/bin/activate \
    && pip --no-cache-dir install --trusted-host pypi.python.org --no-index --find-links=/tmp/wheelhouse -r requirements/tests.txt

FROM 483193324480.dkr.ecr.us-east-1.amazonaws.com/python:3.9-slim-runner as main

ARG VERSION
ARG REVISION
ARG BUILDTIME

LABEL com.datadoghq.tags.service=vendor-api
LABEL com.datadoghq.tags.version=${VERSION}
LABEL com.datadoghq.tags.application_family=apollo
LABEL org.opencontainers.image.revision=${REVISION}
LABEL org.opencontainers.image.version=${VERSION}
LABEL BUILDTIME=${BUILDTIME}

ENV DD_SERVICE=vendor-api
ENV DD_VERSION=${VERSION}
ENV SENTRY_PROJECT=vendor-api

COPY --from=build-base /venv /venv

USER ${APP_USER}

COPY . /app

WORKDIR /app

EXPOSE 8000

CMD ["/venv/bin/gunicorn", "app:main", "-c", "gunicorn_config.py"]

FROM build-test as test

COPY . /app

WORKDIR /app
