Skip to content

Commit 4c26777

Browse files
committed
Add a starting Dockerfile
1 parent 5a3fa10 commit 4c26777

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

Dockerfile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Use the jupyter/minimal-notebook as the base image
2+
FROM quay.io/jupyter/minimal-notebook:latest
3+
4+
# Switch to root user to install additional dependencies (if needed)
5+
USER root
6+
7+
# Install additional dependencies
8+
RUN apt-get update && \
9+
apt-get install -y --no-install-recommends \
10+
build-essential \
11+
gcc \
12+
g++ \
13+
libffi-dev \
14+
&& \
15+
apt-get clean && \
16+
rm -rf /var/lib/apt/lists/*
17+
18+
# Copy the script to setup the environment
19+
COPY docker/setup_custom_env.py /opt/setup-scripts/
20+
# Make the script executable
21+
RUN chmod +x /opt/setup-scripts/setup_custom_env.py
22+
23+
# Copy the script to activate the Conda environment
24+
COPY docker/activate-custom-env.sh /usr/local/bin/before-notebook.d/
25+
26+
# Switch back to the default notebook user
27+
USER ${NB_UID}
28+
29+
# Set a shortcut to the tutorial name
30+
ENV BASENAME="python-tutorial"
31+
32+
# Set the working directory to the home directory of the notebook user
33+
WORKDIR ${HOME}
34+
35+
# Clone the tutorial repository
36+
RUN git clone \
37+
--branch main \
38+
--depth 1 \
39+
https://github.com/empa-scientific-it/${BASENAME}
40+
41+
WORKDIR ${HOME}/${BASENAME}
42+
43+
# Create the Conda environment defined in environment.yml
44+
RUN mamba env create -f binder/environment.yml && \
45+
mamba clean --all -f -y && \
46+
fix-permissions "${CONDA_DIR}" && \
47+
fix-permissions "/home/${NB_USER}"
48+
49+
# Register the Jupyter kernel from the custom environment
50+
RUN /opt/setup-scripts/setup_custom_env.py ${BASENAME}
51+
52+
# Copy the IPython configuration file
53+
RUN mkdir -p ${HOME}/.ipython/profile_default
54+
COPY binder/ipython_config.py ${HOME}/.ipython/profile_default/
55+
56+
# Set the environment variable IPYTHONDIR
57+
ENV IPYTHONDIR="${HOME}/.ipython"
58+
59+
# Use the default ENTRYPOINT from the base image to start Jupyter Lab
60+
ENTRYPOINT ["tini", "-g", "--", "start.sh"]

docker/activate-custom-env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
conda activate python-tutorial

docker/post-build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -e
3+
4+
mkdir -p ${HOME}/.ipython/profile_default
5+
cp binder/ipython_config.py ${HOME}/.ipython/profile_default/

docker/setup_custom_env.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
import json
3+
import os
4+
import sys
5+
from pathlib import Path
6+
7+
# Retrieve the environment name from the command-line arguments
8+
env_name = sys.argv[1]
9+
10+
# Get the Conda directory from the environment variables
11+
CONDA_DIR = os.environ["CONDA_DIR"]
12+
13+
# Define the path to the kernel.json file
14+
kernel_dir = Path.home() / f".local/share/jupyter/kernels/{env_name}"
15+
kernel_file = kernel_dir / "kernel.json"
16+
17+
# Ensure the kernel directory exists
18+
kernel_dir.mkdir(parents=True, exist_ok=True)
19+
20+
# Define default kernel.json content
21+
default_content = {
22+
"argv": [
23+
f"{CONDA_DIR}/envs/{env_name}/bin/python",
24+
"-m",
25+
"ipykernel_launcher",
26+
"-f",
27+
"{connection_file}",
28+
],
29+
"display_name": f"Python ({env_name})",
30+
"language": "python",
31+
}
32+
33+
# If the kernel.json file doesn't exist, create it with default content
34+
if not kernel_file.exists():
35+
kernel_file.write_text(json.dumps(default_content, indent=1))
36+
37+
# Read the existing kernel.json content
38+
content = json.loads(kernel_file.read_text())
39+
40+
# Add the environment variables to the kernel configuration
41+
content["env"] = {
42+
"XML_CATALOG_FILES": "",
43+
"PATH": f"{CONDA_DIR}/envs/{env_name}/bin:$PATH",
44+
"CONDA_PREFIX": f"{CONDA_DIR}/envs/{env_name}",
45+
"CONDA_PROMPT_MODIFIER": f"({env_name}) ",
46+
"CONDA_SHLVL": "2",
47+
"CONDA_DEFAULT_ENV": env_name,
48+
"CONDA_PREFIX_1": CONDA_DIR,
49+
}
50+
51+
# Write the updated content back to the kernel.json file
52+
kernel_file.write_text(json.dumps(content, indent=1))

0 commit comments

Comments
 (0)