FROM python:3.14-alpine AS base-libs

LABEL maintainer="mapproxy.org"

RUN apk add --no-cache \
      g++ \
      gdal \
      gdal-dev \
      libxslt-dev \
      libxml2 \
      proj \
      proj-dev \
      proj-util \
      geos \
      geos-dev \
      libffi-dev

FROM base-libs AS builder

RUN mkdir /mapproxy
WORKDIR /mapproxy

COPY setup.py MANIFEST.in README.md CHANGES.txt AUTHORS.txt COPYING.txt LICENSE.txt ./
COPY mapproxy mapproxy

RUN rm -rf dist/* \
     && pip wheel . -w dist

###### base/plain image ######
FROM base-libs AS base

ARG MAPPROXY_VERSION=8.0.0
ARG USER_UID=1000
ARG USER_GID=1000

ENV MAPPROXY_VERSION=${MAPPROXY_VERSION}
ENV PATH="${PATH}:/mapproxy/.local/bin"

RUN mkdir /mapproxy && addgroup -g $USER_GID -S mapproxy && \
    adduser -D -s /bin/sh -S -h /mapproxy/ -G mapproxy -u $USER_UID mapproxy && \
    chown -R mapproxy:mapproxy /mapproxy

WORKDIR /mapproxy
USER mapproxy:mapproxy

RUN mkdir mapproxy-dist
COPY --from=builder /mapproxy/dist/* mapproxy-dist/

# Installing optional packages and MapProxy afterwards
RUN pip install --no-cache-dir requests redis boto3 azure-storage-blob && \
  pip install --no-cache-dir --find-links=./mapproxy-dist --no-index MapProxy && \
  pip cache purge

COPY docker/app.py .
COPY docker/entrypoint.sh .
COPY docker/logging.ini ./config/logging.ini

ENTRYPOINT ["./entrypoint.sh"]

CMD ["echo", "no CMD given"]

LABEL org.opencontainers.image.authors="mapproxy.org"
LABEL org.opencontainers.image.created="$(date -u +%Y-%m-%dT%H:%M:%S%z)"
LABEL org.opencontainers.image.source="https://github.com/mapproxy/mapproxy"
LABEL org.opencontainers.image.title="MapProxy Alpine base image"
LABEL org.opencontainers.image.description="Docker image for MapProxy based on Alpine Linux and Python 3.14, including necessary dependencies for running MapProxy applications."
LABEL org.opencontainers.image.url=ghcr.io/mapproxy/mapproxy/mapproxy
LABEL org.opencontainers.image.version=${MAPPROXY_VERSION}

###### development image ######
FROM base AS development

ARG MAPPROXY_VERSION=8.0.0
ENV MAPPROXY_VERSION=${MAPPROXY_VERSION}

EXPOSE 8080

CMD ["mapproxy-util", "serve-develop", "-b", "0.0.0.0", "/mapproxy/config/mapproxy.yaml", "--log-config", "/mapproxy/config/logging.ini"]

LABEL org.opencontainers.image.authors="mapproxy.org"
LABEL org.opencontainers.image.created="$(date -u +%Y-%m-%dT%H:%M:%S%z)"
LABEL org.opencontainers.image.source="https://github.com/mapproxy/mapproxy"
LABEL org.opencontainers.image.title="MapProxy Development Server"
LABEL org.opencontainers.image.description="Docker image for MapProxy development server, based on Alpine Linux and Python 3.14."
LABEL org.opencontainers.image.url=ghcr.io/mapproxy/mapproxy/mapproxy
LABEL org.opencontainers.image.version=${MAPPROXY_VERSION}

##### nginx image ######
FROM base AS nginx

ARG MAPPROXY_VERSION=8.0.0
ARG NGINX_VERSION=1.30.4
ENV MAPPROXY_ALPINE=true
ENV NGINX_VERSION=${NGINX_VERSION}
ENV MAPPROXY_VERSION=${MAPPROXY_VERSION}

USER root:root

RUN apk --no-cache add ca-certificates supervisor pcre-dev build-base linux-headers openssl-dev wget zlib-dev && \
  su mapproxy -c "pip install uwsgi" && \
  cd /tmp && \
  wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
  tar xzf nginx-${NGINX_VERSION}.tar.gz && \
  cd /tmp/nginx-${NGINX_VERSION} && \
  ./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --user=mapproxy \
    --group=mapproxy && \
  make && \
  make install && \
  rm -rf /tmp/* && \
  apk del build-base linux-headers openssl-dev wget zlib-dev && \
  rm -rf /var/cache/apk/*

USER mapproxy:mapproxy

COPY docker/uwsgi.conf .
COPY docker/nginx-default.conf /etc/nginx/conf.d/nginx.conf
COPY docker/run-nginx.sh .

USER root:root

# Since we have a custom build of NGINX, we need to make sure that it includes the configuration files from /etc/nginx/conf.d/*.conf.
# The default nginx.conf does not include this, so we need to modify it to include our custom configuration.
COPY docker/nginx-minimal.conf /etc/nginx/nginx.conf

RUN mkdir /var/cache/nginx \
    && chown -R mapproxy:mapproxy /var/cache/nginx \
    && chown -R mapproxy:mapproxy /var/log/nginx \
    && touch /var/run/nginx.pid \
    && chown -R mapproxy:mapproxy /var/run/nginx.pid \
    && chown -R mapproxy:mapproxy /var/run \
    && chown -R mapproxy:mapproxy /etc/nginx

USER mapproxy:mapproxy
EXPOSE 9090
CMD ["./run-nginx.sh"]

LABEL org.opencontainers.image.authors="mapproxy.org"
LABEL org.opencontainers.image.created="$(date -u +%Y-%m-%dT%H:%M:%S%z)"
LABEL org.opencontainers.image.source="https://github.com/mapproxy/mapproxy"
LABEL org.opencontainers.image.title="MapProxy Alpine Image with NGINX and uWSGI"
LABEL org.opencontainers.image.description="Docker image for MapProxy based on Alpine Linux, including NGINX and uWSGI for serving MapProxy applications in a production environment."
LABEL org.opencontainers.image.url=ghcr.io/mapproxy/mapproxy/mapproxy
LABEL org.opencontainers.image.version=${MAPPROXY_VERSION}
