FROM nikolaik/python-nodejs:python3.11-nodejs22 AS build-stage

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /workdir

COPY package*.json ./
RUN npm install

COPY . .

ENV DJANGO_ENV=production

RUN npx webpack --mode production
RUN python manage.py collectstatic --noinput -v2

FROM python:3.11

RUN apt-get update && apt-get install -y --no-install-recommends \
        gettext \
    && rm -rf /var/lib/apt/lists/*

RUN groupadd -r runner && useradd --no-log-init -r -g runner runner

# Must match the settings.KINGFISHER_COLLECT_FILES_STORE default value.
RUN mkdir -p /data/collect && chown -R runner:runner /data/collect
# Must match the settings.EXPORTER_DIR default value.
RUN mkdir -p /data/exporter && chown -R runner:runner /data/exporter

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /workdir
USER runner:runner
COPY --chown=runner:runner . .

# Django needs a copy of the staticfiles.json manifest file.
COPY --from=build-stage --chown=runner:runner /workdir/static/staticfiles.json /workdir/static/staticfiles.json

ENV DJANGO_ENV=production
ENV WEB_CONCURRENCY=2

RUN python manage.py compilemessages

EXPOSE 8000
CMD ["gunicorn", "core.wsgi", "--bind", "0.0.0.0:8000", "--worker-tmp-dir", "/dev/shm", "--threads", "2", "--name", "data-registry"]