# Multi-stage Dockerfile for localtileserver
#
# Targets:
#   slim    - minimal server image (default)
#   jupyter - JupyterLab with localtileserver pre-installed
#
# Usage:
#   docker build -t localtileserver .                        # slim (default)
#   docker build -t localtileserver-jupyter --target jupyter .
#
#   docker run --rm -it -p 8000:8000 localtileserver
#   docker run --rm -it -p 8888:8888 localtileserver-jupyter

# ---- shared base: install the package once ----
FROM python:3.12-slim AS base

LABEL maintainer="Bane Sullivan"
LABEL repo="https://github.com/banesullivan/localtileserver"

# libexpat1 is required at runtime by the GDAL bundled in the rasterio wheel
RUN apt-get update && apt-get install -y --no-install-recommends \
        libexpat1 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build-context

RUN python -m pip install --upgrade --no-cache-dir pip

# Version is computed by setuptools_scm from git, which isn't available inside
# the build context. Pass it in as a build arg (the CI workflow derives it from
# the checked-out repo); the default is a safe placeholder for ad-hoc builds.
ARG SETUPTOOLS_SCM_PRETEND_VERSION_FOR_LOCALTILESERVER=0.0.0+docker
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_LOCALTILESERVER=${SETUPTOOLS_SCM_PRETEND_VERSION_FOR_LOCALTILESERVER}

COPY pyproject.toml README.md /build-context/
COPY localtileserver/ /build-context/localtileserver/
# ``jupyter-config/`` is referenced by ``[tool.setuptools.data-files]``
# in pyproject.toml; the jupyter-server auto-enable JSON lands at
# /etc/jupyter/jupyter_server_config.d/ on install.
COPY jupyter-config/ /build-context/jupyter-config/

# ---- slim: server-only image (default) ----
FROM base AS slim

RUN pip install --no-cache-dir "/build-context[colormaps]"

EXPOSE 8000
ENTRYPOINT ["uvicorn", "localtileserver.web.wsgi:app", "--host=0.0.0.0", "--port=8000", "--workers=4"]

# ---- jupyter: full JupyterLab image ----
FROM base AS jupyter

RUN pip install --no-cache-dir "/build-context[all,test]" jupyterlab

ENV JUPYTER_ENABLE_LAB=yes

WORKDIR /home

COPY example.ipynb /home/

EXPOSE 8888
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--no-browser", "--allow-root"]
