FROM alpine:latest AS base

ARG VERSION

RUN --mount=type=cache,target=/var/cache/apk apk update
RUN --mount=type=cache,target=/var/cache/apk apk add python3 nodejs libcurl

RUN adduser -D -u 1000 jupyterhub
RUN mkdir -p /var/lib/jupyterhub 
RUN chown jupyterhub:jupyterhub /var/lib/jupyterhub

FROM base AS build
USER root
RUN --mount=type=cache,target=/var/cache/apk apk add npm curl-dev gcc musl-dev python3-dev
RUN --mount=type=cache,target=/home/jupyterhub/.cache/pip npm install -g configurable-http-proxy
USER jupyterhub
WORKDIR /var/lib/jupyterhub
RUN python3 -m venv .
RUN --mount=type=cache,target=/home/jupyterhub/.cache/pip ./bin/pip install "jupyterhub==${VERSION}" jupyterhub_idle_culler dockerspawner psycopg2-binary oauthenticator pycurl


FROM base AS release
USER root
COPY --from=build /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/configurable-http-proxy/bin/configurable-http-proxy /usr/local/bin/configurable-http-proxy
COPY --from=build /var/lib/jupyterhub /var/lib/jupyterhub
USER jupyterhub
WORKDIR /home/jupyterhub
ENV PATH="/var/lib/jupyterhub/bin/:$PATH"
EXPOSE 43080
ENTRYPOINT ["jupyterhub"]
