From b1403ba2d0e18e09a402bf8c2accbce7e05e4db1 Mon Sep 17 00:00:00 2001 From: Steffen Clemens <67413881+td-sclemens@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:25:06 +0100 Subject: [PATCH] Add image for Jupyter projects (#14) --- .github/workflows/build.yml | 3 ++ jupyter/Dockerfile | 40 ++++++++++++++++++++++ jupyter/JUPYTER.md | 68 +++++++++++++++++++++++++++++++++++++ jupyter/README.md | 46 +++++++++++++++++++++++++ jupyter/entrypoint.sh | 40 ++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 jupyter/Dockerfile create mode 100644 jupyter/JUPYTER.md create mode 100644 jupyter/README.md create mode 100755 jupyter/entrypoint.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4d30294..a35bf41 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -124,6 +124,9 @@ jobs: - path: keycloak/21.0.1 tags: - riptidepy/keycloak:21.0.1 + - path: jupyter + tags: + - riptidepy/jupyter:latest name: Build Image steps: - name: Checkout diff --git a/jupyter/Dockerfile b/jupyter/Dockerfile new file mode 100644 index 0000000..71b1728 --- /dev/null +++ b/jupyter/Dockerfile @@ -0,0 +1,40 @@ +ARG PYTHON_VERSION=3.11.7 +ARG DOCKER_USER=docker +ARG USER_ID=1000 +ARG GROUP_ID=$USER_ID + +FROM python:${PYTHON_VERSION}-slim +ARG DOCKER_USER +ARG USER_ID +ARG GROUP_ID + +RUN apt update && apt install -y --no-install-recommends \ + curl \ + procps \ + neovim \ + && apt clean && rm -rf /var/lib/apt/lists/* + +RUN addgroup --gid $GROUP_ID $DOCKER_USER \ + && adduser --uid $USER_ID --gid $GROUP_ID --shell /bin/sh --home "/home/${DOCKER_USER}" --disabled-password $DOCKER_USER +USER $USER_ID:$GROUP_ID + +WORKDIR /src +VOLUME /src +VOLUME /notebooks +VOLUME /home/docker/.jupyter +EXPOSE 8888 +COPY JUPYTER.md /assets/ +COPY entrypoint.sh /usr/local/bin/ +ENTRYPOINT ["entrypoint.sh"] +CMD [ \ + "python", \ + "-m", \ + "jupyter", \ + "notebook", \ + "--ip", \ + "0.0.0.0", \ + "--no-browser", \ + "--ServerApp.root_dir=/notebooks", \ + "--IdentityProvider.token=''", \ + "--PasswordIdentityProvider.password_required=false" \ +] diff --git a/jupyter/JUPYTER.md b/jupyter/JUPYTER.md new file mode 100644 index 0000000..810abfa --- /dev/null +++ b/jupyter/JUPYTER.md @@ -0,0 +1,68 @@ +# Jupyter Notebook + +## Setup + +### Setup Riptide project + +```shell +riptide setup +``` + +### Update images + +```shell +riptide update +``` + +Note: Make sure riptide is in your PATH (not just an alias to your bin), so it could be found with `which`. + + +## Start + +To start your notebook server run: + +```shell +riptide start +``` + +This will +- create a new python virtual environment at `.venv` (if not existing) +- create a `pyproject.toml` containing the project dependencies (if not existing) +- install the project dependencies from the `pyproject.toml` (if the venv was not existing before) +- ensures that jupyter notebook is installed (adds it as dependency if missing) +- starts the jupyter notebook server + +You manually need to adjust the `pyproject.toml` to your needs. + +When the start command is finished, it will give you the link to your notebook server. + +If no virtual environment exists, the start command can take a little longer. +It will wait until the server is up and running, so that you know, when you can start accessing it. + +## Install dependencies + +After `pyproject.toml` was externally changed (e.g. by `git pull`), you need to run following command, +to install the up-to-date dependencies. + +```shell +poetry install +``` + +### Add new dependencies + +To install a new dependency (like in this example `pandas`), run the following: + +```shell +poetry add pandas +``` + +## Troubleshooting + +If something is not working as expected, you can attach to the logs: + +```shell +tail -f _riptide/logs/jupyter/stderr.log +``` + +At any time you can remove the `.venv` directory in you project and restart +Riptide (`riptide restart`) to recreate it. This could solve most of the issues. diff --git a/jupyter/README.md b/jupyter/README.md new file mode 100644 index 0000000..c38c6bb --- /dev/null +++ b/jupyter/README.md @@ -0,0 +1,46 @@ +# Supported tags and respective `Dockerfile` links + +- [`latest` (*Dockerfile*)](https://github.com/theCapypara/riptide-docker-images/jupyter/Dockerfile) + +# Quick reference + +- **Where to get help**: + [the Riptide Docker Images Github Repository](https://github.com/theCapypara/riptide-docker-images) + +- **Where to file issues**: + [https://github.com/theCapypara/riptide-docker-images/issues](https://github.com/theCapypara/riptide-docker-images/issues) + +- **Maintained by**: + [the Riptide Community](https://github.com/theCapypara/riptide-docker-images) + +- **Source of this description**: + [README in riptide-docker-images repo](https://github.com/theCapypara/riptide-docker-images/tree/master/jupyter) ([history](https://github.com/theCapypara/riptide-docker-images/tree/master/jupyter)) + +# What is Jupyter? + +[Jupyter Notebook](https://jupyter.org) is a web-based interactive computing platform. This image contains all tools needed to run a Jupyter server and work with notebooks. + +# How to use this image. + +This image is meant to be used with [Riptide](https://github.com/theCapypara/riptide-cli). +Using it without Riptide is probably possible, but not supported. + +## Basic Usage with Riptide + +For a service using this image, see: [Jupyter Riptide Service](https://github.com/theCapypara/riptide-repo/tree/master/service/jupyter) + +This image supports ``run_as_current_user: true`` for services. + +## Basic Usage with Docker Run + +``` +docker run \ + -v .:/src \ + -v ./notebooks:/notebooks \ + -v ./.jupyter:~/.jupyter \ + riptidepy/jupyter +``` + +# Volumes + +Have a look here for the volumes used by this image: [Jupyter Riptide Service](https://github.com/theCapypara/riptide-repo/tree/master/service/jupyter) diff --git a/jupyter/entrypoint.sh b/jupyter/entrypoint.sh new file mode 100755 index 0000000..eba4847 --- /dev/null +++ b/jupyter/entrypoint.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +set -x + +NEW_VENV=false +cp /assets/JUPYTER.md /src + +# create new venv if not existing +if [ ! -d .venv ]; then + python -m venv .venv + .venv/bin/pip install --upgrade pip + .venv/bin/pip install poetry + NEW_VENV=true +fi + +# activate the virtual env +source .venv/bin/activate + +# add activation script to .bashrc, so we are using the venv, if we are in a bash shell +echo "if [ -f .venv/bin/activate ]; then source .venv/bin/activate; fi" >> ~/.bashrc + +# init poetry project +if [ ! -f pyproject.toml ]; then + PLACEHOLDER_NAME="Your Project" + poetry new "$PLACEHOLDER_NAME" + cp "$PLACEHOLDER_NAME/pyproject.toml" . + rm -r "$PLACEHOLDER_NAME" +fi + +# install dependencies if the venv was newly created +if [ "$NEW_VENV" = true ]; then + poetry install --no-root +fi + +# ensure jupyter is installed +if ! jupyter-notebook --version; then + poetry add jupyter +fi + +exec "$@"