FROM python:3.12-slim-bookworm

ARG APOLON_UID=10001
ARG APOLON_GID=10001

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

RUN groupadd --gid "${APOLON_GID}" apolon \
    && useradd --uid "${APOLON_UID}" --gid "${APOLON_GID}" --create-home --shell /usr/sbin/nologin apolon

WORKDIR /srv/apolon

COPY licensing_server/requirements.txt /tmp/licensing-requirements.txt
RUN python -m pip install --no-cache-dir -r /tmp/licensing-requirements.txt

COPY --chown=apolon:apolon licensing_shared ./licensing_shared
COPY --chown=apolon:apolon licensing_server ./licensing_server
RUN sed -i 's/\r$//' licensing_server/docker-entrypoint.sh \
    && chmod 0755 licensing_server/docker-entrypoint.sh

USER apolon:apolon

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/v1/health', timeout=3).read()"

ENTRYPOINT ["/srv/apolon/licensing_server/docker-entrypoint.sh"]
