FROM docker.io/drycc/base:bullseye

RUN adduser --system \
	--shell /bin/bash \
	--disabled-password \
	--home /workspace \
	--group \
	drycc

COPY requirements.txt /workspace/requirements.txt
COPY dev_requirements.txt /workspace/dev_requirements.txt

ENV PGDATA="/var/lib/postgresql/data" \
  PYTHON_VERSION="3.10.2" \
  POSTGRES_VERSION="14.2" \
  GOSU_VERSION="1.14"

RUN buildDeps='gcc rustc cargo libffi-dev musl-dev libldap2-dev libsasl2-dev'; \
  install-packages ldap-utils mercurial ca-certificates openssl git $buildDeps \
  && install-stack python $PYTHON_VERSION \
  && install-stack postgresql $POSTGRES_VERSION \
  && install-stack gosu $GOSU_VERSION && . init-stack \
  && python3 -m venv /workspace/.venv \
  && source /workspace/.venv/bin/activate \
  && pip3 install --disable-pip-version-check --no-cache-dir -r /workspace/requirements.txt \
  && pip3 install --disable-pip-version-check --no-cache-dir -r /workspace/dev_requirements.txt \
  && chown -R drycc:drycc /workspace \
  # set env
  && echo "source /workspace/.venv/bin/activate" >> /opt/drycc/python/profile.d/python.sh \
  # cleanup
  && scanelp /workspace/.venv/lib > runtime.txt \
  && apt-get purge -y --auto-remove $buildDeps \
  && install-packages $(< runtime.txt) \
  && apt-get autoremove -y \
  && apt-get clean -y \
  && rm -rf \
        /usr/share/doc \
        /usr/share/man \
        /usr/share/info \
        /usr/share/locale \
        /var/lib/apt/lists/* \
        /var/log/* \
        /var/cache/debconf/* \
        /etc/systemd \
        /lib/lsb \
        /lib/udev \
        /usr/lib/`echo $(uname -m)`-linux-gnu/gconv/IBM* \
        /usr/lib/`echo $(uname -m)`-linux-gnu/gconv/EBC* \
  && mkdir -p /usr/share/man/man{1..8} \
  && mkdir -p /run/postgresql $PGDATA \
  && groupadd postgres && useradd -g postgres postgres \
  && chown -R postgres:postgres /run/postgresql $PGDATA \
  && gosu postgres initdb -D $PGDATA

COPY . /workspace
WORKDIR /workspace
CMD ["/workspace/bin/boot"]
EXPOSE 8000
