1- # Use the jupyter/minimal-notebook as the base image
2- FROM quay.io/jupyter/minimal-notebook:latest
1+ # Stage 1: Build environment
2+ FROM quay.io/jupyter/minimal-notebook:latest as builder
33
4- # Metadata labels
5- LABEL org.opencontainers.image.title="Python Tutorial"
6- LABEL org.opencontainers.image.description="A containerized Python tutorial environment with Jupyter Lab."
7- LABEL org.opencontainers.image.authors="Empa Scientific IT <scientificit@empa.ch>"
8- LABEL org.opencontainers.image.url="https://github.com/empa-scientific-it/python-tutorial"
9- LABEL org.opencontainers.image.source="https://github.com/empa-scientific-it/python-tutorial"
10- LABEL org.opencontainers.image.version="1.0.0"
11- LABEL org.opencontainers.image.licenses="MIT"
12-
13- # Set environment variables for the tutorial and repository
14- ENV BASENAME="python-tutorial"
15- ENV REPO=${HOME}/${BASENAME}
16- ENV IPYTHONDIR="${HOME}/.ipython"
4+ # Define build argument for PyTorch variant (cpu or cuda)
5+ ARG PYTORCH_VARIANT=cpu
176
187# Switch to root user to install additional dependencies
198USER root
@@ -33,16 +22,59 @@ USER ${NB_UID}
3322# Set up the Conda environment
3423COPY docker/environment.yml /tmp/environment.yml
3524RUN mamba env update -n base -f /tmp/environment.yml && \
25+ # Install PyTorch packages without cache - conditionally based on variant
26+ if [ "$PYTORCH_VARIANT" = "cpu" ]; then \
27+ echo "Installing CPU-only PyTorch" && \
28+ pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu; \
29+ else \
30+ echo "Installing CUDA-enabled PyTorch" && \
31+ pip install --no-cache-dir torch torchvision; \
32+ fi && \
33+ # Clean up all package caches to reduce image size
3634 mamba clean --all -f -y && \
35+ # Remove pip cache
36+ rm -rf ~/.cache/pip && \
3737 fix-permissions "${CONDA_DIR}" && \
3838 fix-permissions "/home/${NB_USER}"
3939
40- # Prepare IPython configuration (move earlier in the build)
41- RUN mkdir -p ${HOME}/.ipython/profile_default
40+ # Stage 2: Runtime environment - creates a lighter final image
41+ FROM quay.io/jupyter/minimal-notebook:latest
42+
43+ # Inherit build argument for image labeling
44+ ARG PYTORCH_VARIANT=cpu
45+
46+ # Metadata labels
47+ LABEL org.opencontainers.image.title="Python Tutorial"
48+ LABEL org.opencontainers.image.description="A containerized Python tutorial environment with Jupyter Lab."
49+ LABEL org.opencontainers.image.authors="Empa Scientific IT <scientificit@empa.ch>"
50+ LABEL org.opencontainers.image.url="https://github.com/empa-scientific-it/python-tutorial"
51+ LABEL org.opencontainers.image.source="https://github.com/empa-scientific-it/python-tutorial"
52+ LABEL org.opencontainers.image.version="1.0.0"
53+ LABEL org.opencontainers.image.licenses="MIT"
54+ LABEL org.opencontainers.image.variant="pytorch-${PYTORCH_VARIANT}"
55+
56+ # Switch to root user to install minimal dependencies
57+ USER root
58+ RUN apt-get update && \
59+ apt-get install -y --no-install-recommends \
60+ libgl1 && \
61+ apt-get clean && \
62+ rm -rf /var/lib/apt/lists/*
63+
64+ # Switch back to the default notebook user
65+ USER ${NB_UID}
66+
67+ # Copy the conda environment from the builder stage
68+ COPY --from=builder ${CONDA_DIR} ${CONDA_DIR}
69+
70+ # Copy home directory with configurations
71+ COPY --from=builder --chown=${NB_UID}:${NB_GID} /home/${NB_USER} /home/${NB_USER}
72+
73+ # Prepare IPython configuration
4274COPY --chown=${NB_UID}:${NB_GID} binder/ipython_config.py ${HOME}/.ipython/profile_default/
4375
44- # Set the working directory to the repository
45- WORKDIR ${REPO }
76+ # Set the working directory to user's home ( repository will be cloned here by Renku)
77+ WORKDIR /home/${NB_USER }
4678
4779# Use the default ENTRYPOINT from the base image to start Jupyter Lab
4880ENTRYPOINT ["tini" , "-g" , "--" , "start.sh" ]
0 commit comments