Skip to content

Commit ab1188f

Browse files
authored
Merge pull request #14 from mfhepp/notebook_new
Notebook support; lock file export
2 parents 8aadb38 + 9f2129c commit ab1188f

13 files changed

+986
-7
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ COPY --chown=$MAMBA_USER:$MAMBA_USER ${ENVIRONMENT_FILE} /tmp/env.yaml
2828
# This is due to the way micromamba-docker works
2929
RUN micromamba install -y -n base -f /tmp/env.yaml && \
3030
micromamba clean --all --yes
31-
# Install Kernels for Jupyter Notebook etc.
32-
# TODO: Add
33-
RUN echo Notebook mode is "$NOTEBOOK_MODE"
34-
RUN if [[ -n "$NOTEBOOK_MODE" ]] ; then echo DEBUG Notebook mode ; fi
3531
WORKDIR /usr/app/src
32+
# TODO: This is not needed for notebook images
3633
COPY --chown=$MAMBA_USER:$MAMBA_USER src/ ./
3734
ARG MAMBA_DOCKERFILE_ACTIVATE=1
3835
ENTRYPOINT ["/usr/local/bin/_entrypoint.sh"]
36+
# Add the base environment as the default Jupyter Python kernel
37+
RUN if [[ -n "$NOTEBOOK_MODE" ]] ; then python -m ipykernel install --user ; fi
3938
# For debugging, use this one
4039
# ENTRYPOINT ["/usr/local/bin/_entrypoint.sh", "/bin/sh"]
4140
# In a final application, you may want to hard-wire the entrypoint to the script:

README.md

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Based on [`micromamba-docker`](https://github.com/mamba-org/micromamba-docker) a
1919
- Removed [Linux Kernel capabilities](https://man7.org/linux/man-pages/man7/capabilities.7.html)
2020
- Adding new kernel capabilities is blocked
2121
- **Development mode,** in which the local version of the Python code can be run inside the container
22+
- **Jupyter Notebook / JupyterLab**: You can also run Jupyter Notebook and JupyterLab inside the isolated container.
2223

2324
## Installation
2425

@@ -305,11 +306,95 @@ It is **strongly recommended to use an absolute path in the alias** (otherwise,
305306
306307
**Warning:** An alias will allow you to run the script from any folder on your system, and that folder will be available for read-access to the script as `/usr/app/data`.
307308
309+
## Jupyter Notebook and JupyterLab
310+
311+
You can build isolated containers with Juypter Notebook and JupyterLab.
312+
313+
### Building a Notebook Image
314+
315+
#### Using the default environment file `notebook.yaml`
316+
317+
```bash
318+
# This will build <username>/notebook:latest
319+
./build.sh -n
320+
```
321+
322+
#### Using one of the pre-defined environment files
323+
324+
```bash
325+
# This will build <username>/notebook:dataviz from dataviz.yaml
326+
./build.sh -n dataviz
327+
# This will build <username>/notebook:openai from openai.yaml
328+
./build.sh -n openai
329+
```
330+
331+
#### Using your own environment file
332+
333+
1. Copy `notebook.yaml` to a new YAML file (e.g. `foo.yaml`) and add modules as needed.
334+
2. Build the image with
335+
```bash
336+
# This will build <username>/notebook:foo from foo.yaml
337+
./build.sh -n foo
338+
```
339+
340+
### Creating an Alias `nbh` (for 'notebook here')
341+
342+
Add the following lines to your `.bash_profile` file, like so:
343+
344+
```bash
345+
# ~/foo/bar/py4docker/ is the absolute path to the project in this example
346+
alias nbh="bash ~/foo/bar/py4docker/run_notebook.sh"
347+
```
348+
**Warning:**
349+
1. An alias will allow you to run the notebook container from any folder on your system, and that folder will be available for read- and write-access to all code and libraries inside the container.
350+
2. Symbolic links may allow access to resources outside the current working directory!
351+
352+
#### Starting a Notebook Container
353+
354+
The notebook containers need write-access and a network connection and are hence not as well isolated as in the Python script modus.
355+
356+
The current working directory will be mapped to `/usr/app/src` inside the container.
357+
358+
For a list of available notebook images (=environments), you can use the alias `nbh`
359+
360+
```bash
361+
nbh --list
362+
```
363+
364+
or
365+
366+
```bash
367+
./run_notebook.sh --list
368+
```
369+
370+
#### Using the default environment `notebook.yaml`
371+
372+
```bash
373+
# This will start <username>/notebook:latest
374+
nbh
375+
```
376+
377+
#### Using one of the pre-defined environments
378+
379+
```bash
380+
# This will start <username>/notebook:dataviz
381+
nbh dataviz
382+
# This will start <username>/notebook:openai
383+
nbh openai
384+
```
385+
386+
#### Using your own environment
387+
388+
```bash
389+
# This will start <username>/notebook:foo built from foo.yaml
390+
nbh foo
391+
```
392+
308393
## Advanced Topics
309394
310395
### Access to the Local File System
311396
312-
The current working directory will be available as `/usr/app/data` from within the container. By default, it is read-only. If you want to make this writeable, change the line
397+
The current working directory will be available as `/usr/app/data` from within the container. By default, it is read-only (except in the Jupyter Notebook mode). If you want to make this writeable, change the line
313398
314399
`--mount type=bind,source=$REAL_PWD,target=/usr/app/data,readonly \`
315400
@@ -323,7 +408,6 @@ You can also mount additional local paths using the same syntax.
323408
324409
If you want to grant your code **write-access** to the `src` folder in **development mode** permanently, you can use the option `-D`, like so:
325410
326-
327411
```bash
328412
./run_script.sh -D
329413
```
@@ -379,10 +463,30 @@ from `run_script.sh`.
379463
380464
More advanced settings are possible, e.g. adding a proxy or firewall inside the container that permits access only to a known set of IP addresses or domains and / or logs the outbound traffic.
381465
466+
## Updating
467+
468+
For updating the Python packages, you should re-built the respective image with `-f` (for 'force'):
469+
470+
```bash
471+
# Script
472+
./build.sh -f
473+
# Script development image
474+
./build.sh -f -d
475+
# Default notebook image
476+
./build.sh -fn
477+
# Notebook image from dataviz.yaml
478+
./build.sh -fn dataviz
479+
# Notebook image from openai.yaml
480+
./build.sh -fn openai
481+
```
482+
382483
## Limitations and Ideas for Improvement
383484
384485
- The code is currently maintained for Docker Desktop on Apple Silicon only. It may work on other platforms, but I have no time for testing at the moment. It seems to work on Debian.
385-
- Expand support for blocking and logging Internet access e.g. by domain or IP ranges is a priority at my side, but non-trivial.
486+
- Better support for blocking and logging Internet access e.g. by domain or IP ranges is a priority at my side, but non-trivial.
487+
- The Jupyter Notebook mode has currently no support for bind mounts in Linux file-systems and will hence only work with Docker Desktop.
488+
- Jupyter Notebook requires a writeable OS.
489+
- The image size can likely be reduced further.
386490
387491
## LICENSE
388492

build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ else
7575
ENVIRONMENT_FILE="$1.yaml"
7676
# user:test_app:env-dev
7777
# user:test_app:env
78+
# user:notebook:env
7879
DIGEST="$1${DIGEST:+-$DIGEST}"
7980
echo "INFO: Environment file = $ENVIRONMENT_FILE"
8081
fi
@@ -91,3 +92,15 @@ docker build $PARAMETERS \
9192
--build-arg="ENVIRONMENT_FILE=$ENVIRONMENT_FILE" \
9293
$BUILD_NOTEBOOK \
9394
--progress=plain --tag $IMAGE_NAME .
95+
echo INFO: Writing lock file of installed packages for $ENVIRONMENT_FILE
96+
docker run \
97+
--security-opt seccomp=seccomp-default.json \
98+
--security-opt=no-new-privileges \
99+
--read-only --tmpfs /tmp \
100+
--cap-drop all \
101+
--rm \
102+
$IMAGE_NAME \
103+
micromamba env export -n base > $ENVIRONMENT_FILE.lock
104+
echo INFO: Build completed.
105+
106+

dataviz.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Using an environment name other than "base" is not recommended!
2+
# Read https://github.com/mamba-org/micromamba-docker#multiple-environments
3+
# if you must use a different environment name.
4+
name: base
5+
channels:
6+
- conda-forge
7+
dependencies:
8+
- pip
9+
- python>=3.9
10+
- typer
11+
- requests
12+
- httpx
13+
- nest-asyncio
14+
- black[jupyter]
15+
- jupyter
16+
- ipykernel
17+
- jupytext
18+
- pandas
19+
- numpy
20+
- openpyxl
21+
- tabulate
22+
- matplotlib
23+
- beautifulsoup4
24+
- graphviz
25+
- python-graphviz
26+
- seaborn
27+
# - jupyter_ai
28+
# PyPi modules
29+
# - pip:
30+
# - black[jupyter]

0 commit comments

Comments
 (0)