# See https://testdriven.io/blog/docker-best-practices/ for recommendations
# on writing dockerfiles for python projects.

# We use a build stage to build a wheel which we then copy and install
# in the second stage to minimize image size. This is mostly needed
# because setuptools_scm needs the full version info from git and git
# itself but including that in the final image would bloat its size.

# we are using the official python image with just a version tag here
# it comes with many tools needed to build and compile python packages
# which increases it's size but is helpful for building

FROM harbor.cta-observatory.org/proxy_cache/python:3.12 AS builder


# Copy full repo, needed for setuptools_scm to properly report version
COPY . /repo/

WORKDIR /repo

# build the application wheel and all dependency wheels, then verify
# that everything installs from those wheels only
# hadolint ignore=DL3008
RUN apt-get update && apt-get install -y --no-install-recommends git gfortran libprotobuf-dev protobuf-compiler \
    && git config --global submodule.aiv-toolkit.update none \
    && python -m pip wheel --no-cache-dir --wheel-dir ./dist /repo/ \
    && python -m pip install --no-cache-dir --no-index --no-deps ./dist/*.whl \
    && python -m pip check \
    && rm -rf /var/lib/apt/lists/*


# second stage, copy and install wheels
# We are using the official python 3.12 image
# as base image in the slim variant to reduce image size.
FROM harbor.cta-observatory.org/proxy_cache/python:3.12-slim
COPY --from=builder /repo/dist /tmp/dist

# The builder stage already resolved and built all dependencies,
# so install the wheels without dependency resolution.
RUN python -m pip install --no-cache-dir --no-deps /tmp/dist/*.whl \
    && rm -rf /tmp/dist \
    && addgroup --system ctao \
    && adduser --system --group ctao

# Set numba cache dir to a writeable location for the ctao user
ENV NUMBA_CACHE_DIR=/tmp/numba_cache
USER ctao
