FROM docker.io/library/python:3.9-alpine

COPY requirements.txt /app/requirements.txt
COPY dev_requirements.txt /app/dev_requirements.txt

ENV PGDATA /var/lib/postgresql/12
RUN echo https://dl-cdn.alpinelinux.org/alpine/edge/testing >>/etc/apk/repositories \
  && apk add --update --virtual .build-deps \
    postgresql-dev \
    gcc \
    rust \
    cargo \
    libffi-dev \
    musl-dev \
    openldap-dev \
  && pip3 install --disable-pip-version-check --no-cache-dir -r /app/requirements.txt \
  && pip3 install --disable-pip-version-check --no-cache-dir -r /app/dev_requirements.txt \
  && runDeps="$( \
    scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
    | tr ',' '\n' \
    | sort -u \
    | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )" \
  && apk add --update --virtual .controller-rundeps \
    $runDeps \
    git \
    mercurial \
    ca-certificates \
    su-exec \
    bash \
    shadow \
    postgresql \
    redis \
    rabbitmq-server \
  && mkdir -p /run/postgresql $PGDATA \
  && chown -R postgres:postgres /run/postgresql $PGDATA \
  && apk del .build-deps \
  && su-exec postgres initdb -D $PGDATA

COPY . /app

WORKDIR /app
CMD ["/app/bin/boot"]
EXPOSE 8000
