# syntax=docker/dockerfile:1

FROM python:alpine

# Install cron daemon and supervisord
RUN apk add --no-cache dcron supervisor

# Create non-root user
ARG CUSTOM_UID
ARG CUSTOM_GID
ENV CUSTOM_USERNAME=django
ENV CUSTOM_GROUPNAME=django
RUN addgroup --gid ${CUSTOM_GID:-1000} ${CUSTOM_GROUPNAME} && \
    adduser --uid ${CUSTOM_UID:-1000} --shell /bin/ash ${CUSTOM_USERNAME} --ingroup ${CUSTOM_GROUPNAME} --disabled-password && \
    mkdir /app && chown ${CUSTOM_UID:-1000}:${CUSTOM_GID:-1000} /app && chmod 700 /app
ENV PATH "$PATH:/home/${CUSTOM_GROUPNAME}/.local/bin"

# Add supervisord conf
COPY development.supervisord.conf /etc/supervisord.conf

# Add cron job
COPY --chmod=600 django.crontab /etc/crontabs/django

# Copy source files
WORKDIR /app
COPY --chown=${CUSTOM_USERNAME}:${CUSTOM_GROUPNAME} app/ /app/

# Install dependencies
RUN pip install -r requirements.txt

# Run supervisord
EXPOSE 8000/tcp
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
