Skip to content

Commit 596785d

Browse files
authored
Add Dockerfile
2 parents 04ab64c + a1ef367 commit 596785d

5 files changed

Lines changed: 133 additions & 0 deletions

File tree

Dockerfile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Use the jupyter/minimal-notebook as the base image
2+
FROM quay.io/jupyter/minimal-notebook:latest
3+
4+
# Set a shortcut to the tutorial name
5+
ENV BASENAME="python-tutorial"
6+
ENV REPO="https://github.com/edoardob90/${BASENAME}"
7+
8+
# Set the working directory to the home directory of the notebook user
9+
WORKDIR ${HOME}
10+
11+
# Clone the tutorial repository
12+
RUN git clone \
13+
--branch main \
14+
--depth 1 \
15+
${REPO}
16+
17+
# Set the working directory to the repository directory
18+
WORKDIR ${BASENAME}
19+
20+
# Switch to root user to install additional dependencies (if needed)
21+
USER root
22+
23+
# Install additional dependencies
24+
RUN apt-get update && \
25+
apt-get install -y --no-install-recommends \
26+
build-essential \
27+
gcc \
28+
g++ \
29+
libffi-dev \
30+
&& \
31+
apt-get clean && \
32+
rm -rf /var/lib/apt/lists/*
33+
34+
# Switch back to the default notebook user
35+
USER ${NB_UID}
36+
37+
# Create the Conda environment defined in environment.yml
38+
# COPY --chown=${NB_USER}:${NB_GID} docker/environment.yml docker/environment.yml
39+
RUN mamba env update -n base -f docker/environment.yml && \
40+
mamba clean --all -f -y && \
41+
fix-permissions "${CONDA_DIR}" && \
42+
fix-permissions "/home/${NB_USER}"
43+
44+
# Copy the IPython configuration file (binder/postBuild script)
45+
RUN mkdir -p ${HOME}/.ipython/profile_default && \
46+
cp -a binder/ipython_config.py ${HOME}/.ipython/profile_default/
47+
48+
# Set the environment variable IPYTHONDIR
49+
ENV IPYTHONDIR="${HOME}/.ipython"
50+
51+
# Use the default ENTRYPOINT from the base image to start Jupyter Lab
52+
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/environment.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: base
3+
channels:
4+
- conda-forge
5+
dependencies:
6+
- pip
7+
- pip:
8+
- numpy
9+
- matplotlib
10+
- pandas
11+
- ipywidgets
12+
- ipynbname
13+
- jupyterlab
14+
- pytest
15+
- pytest-timeout
16+
- markdown
17+
- pre-commit
18+
- geostatspy
19+
- gstools
20+
- scikit-learn
21+
- attrs
22+
- multiprocess

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)