Skip to content

Instantly share code, notes, and snippets.

@SomeBottle
Last active October 29, 2026 02:17
Show Gist options
  • Select an option

  • Save SomeBottle/8a75d490d2616ecfe100e676564ac769 to your computer and use it in GitHub Desktop.

Select an option

Save SomeBottle/8a75d490d2616ecfe100e676564ac769 to your computer and use it in GitHub Desktop.
My JupyterLab Dockerfile for Research Experiments
# FROM python:3.11.14-bookworm
FROM docker.1ms.run/python:3.11.14-bookworm
# 【可配置环境变量】
# JUPYTER_PASSWORD: Jupyter 登录密码,不配置的话默认是随机字串
# JUPYTER_WORKDIR: 工作目录,默认是 /app
# JUPYTER_PORT: Jupyter 端口,默认是 9527
ENV JUPYTER_WORKDIR="/app" \
JUPYTER_CONF_DIR="/home/somebottle/.jupyter" \
JUPYTER_PORT=9527 \
PIP_INSTALL_BIN_DIR="/home/somebottle/.local/bin" \
PYTHON_BIN_DIR="/usr/local/bin"
USER root
WORKDIR $JUPYTER_WORKDIR
RUN useradd -m -s /bin/bash -U somebottle && \
echo 'export PATH=$PATH:'$PIP_INSTALL_BIN_DIR > /etc/profile.d/jupyter.sh && \
echo 'export PATH=$PATH:'$PYTHON_BIN_DIR >> /etc/profile.d/python.sh && \
echo 'export PATH=$PATH:'$NVM_DIR/versions/node/v24.*/bin >> /etc/profile.d/node.sh && \
chown -R somebottle:somebottle $JUPYTER_WORKDIR
USER somebottle
RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/simple && \
pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir torch torchvision torchaudio matplotlib scikit-learn pillow tqdm pandas opencv-python kornia crc32c soundfile tensorboard tensorboardX imagehash && \
pip install --no-cache-dir timm einops && \
pip install --no-cache-dir nvidia-ml-py grad-cam rich seaborn scipy && \
pip install --no-cache-dir transformers peft accelerate safetensors bitsandbytes trl
RUN bash -c "source /etc/profile && \
pip install --no-cache-dir jupyter notebook jupyterlab && \
pip install --no-cache-dir jupyterlab-language-pack-zh-CN && \
jupyter notebook --generate-config && \
echo 'c.ServerApp.password_required = True' >> $JUPYTER_CONF_DIR/jupyter_notebook_config.py && \
echo 'c.ServerApp.allow_root = True' >> $JUPYTER_CONF_DIR/jupyter_server_config.py"
USER root
RUN echo "deb http://deb.debian.org/debian bookworm main contrib non-free" | tee /etc/apt/sources.list && \
echo "deb http://deb.debian.org/debian-security bookworm-security main contrib non-free" | tee -a /etc/apt/sources.list && \
echo "deb http://deb.debian.org/debian bookworm-updates main contrib non-free" | tee -a /etc/apt/sources.list && \
sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y vim zip screen nano tree lsof htop ncurses-bin binutils libgl1 acl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
USER somebottle
ENV NVM_DIR=/home/somebottle/.nvm
RUN mkdir -p "$NVM_DIR" && \
curl -o- https://fastly.jsdelivr.net/gh/nvm-sh/nvm@v0.40.8/install.sh | bash && \
. "$NVM_DIR/nvm.sh" && \
nvm install 24 && \
nvm alias default 24
# entrypoint
COPY entrypoint.sh /home/somebottle/entrypoint.sh
ENTRYPOINT ["/bin/bash", "/home/somebottle/entrypoint.sh"]
#!/bin/bash
export SHELL=/bin/bash
if [ -z $JUPYTER_PASSWORD ]; then
export JUPYTER_PASSWORD=$(openssl rand -hex 32)
fi
HASHED_PASSWD=$(python -c "from jupyter_server.auth import passwd;print(passwd('$JUPYTER_PASSWORD', algorithm='sha256'))")
if [ ! -f $JUPYTER_CONF_DIR/init.flag ]; then
echo "c.ServerApp.password = '$HASHED_PASSWD'" >> $JUPYTER_CONF_DIR/jupyter_notebook_config.py
echo "c.ServerApp.terminado_settings = {'shell_command': ['/bin/bash']}" >> $JUPYTER_CONF_DIR/jupyter_notebook_config.py
touch $JUPYTER_CONF_DIR/init.flag
fi
source /etc/profile
jupyter lab --ip=0.0.0.0 --port $JUPYTER_PORT --no-browser --notebook-dir=$JUPYTER_WORKDIR
#!/usr/bin/env bash
set -euo pipefail
# Usage:
# On host: ./share_acl.sh host <directory> <container-mapped host UID>
# In container: ./share_acl.sh container <directory>
usage() {
echo "Usage:"
echo " $0 host <directory> <container host UID>"
echo " $0 container <directory>"
exit 1
}
[[ $# -ge 2 ]] || usage
MODE="$1"
DIR="$2"
MY_UID="$(id -u)"
[[ -d "$DIR" ]] || {
echo "Directory does not exist: $DIR" >&2
exit 1
}
command -v setfacl >/dev/null || {
echo "setfacl command not found" >&2
exit 1
}
DIR="$(cd -- "$DIR" && pwd -P)"
[[ "$DIR" != "/" ]] || {
echo "Refusing to modify the root directory" >&2
exit 1
}
case "$MODE" in
host)
[[ $# -eq 3 && "$3" =~ ^[0-9]+$ ]] || usage
CONTAINER_HOST_UID="$3"
if [[ "$(stat -c %u "$DIR")" != "$MY_UID" ]]; then
echo "Error: current user is not the owner of the shared directory" >&2
exit 1
fi
echo "[HOST] Initializing shared directory: $DIR"
# Make new files inherit the parent directory's group
find "$DIR" -xdev -type d -uid "$MY_UID" \
-exec chmod g+s -- {} +
# Allow container user to read/write existing host files
find "$DIR" -xdev -uid "$MY_UID" ! -type l \
-exec setfacl -m \
"u:${CONTAINER_HOST_UID}:rwX,g::rwX" -- {} +
# Future new files and subdirectories automatically inherit permissions for both sides
find "$DIR" -xdev -type d -uid "$MY_UID" \
-exec setfacl -m \
"d:u:${MY_UID}:rwx,d:u:${CONTAINER_HOST_UID}:rwx,d:g::rwx" \
-- {} +
;;
container)
[[ $# -eq 2 ]] || usage
echo "[CONTAINER] Fixing existing container files: $DIR"
# Regular files owned by the container user:
# grant the host user read/write access via the file's owning group
find "$DIR" -xdev -type f -uid "$MY_UID" \
-exec setfacl -m "g::rwX" -- {} +
# Set default ACLs for existing subdirectories owned by the container user
# Do not modify their access ACL to avoid accidentally clearing setgid
find "$DIR" -xdev -type d -uid "$MY_UID" \
-exec setfacl -m \
"d:u:${MY_UID}:rwx,d:g::rwx" -- {} +
;;
*)
usage
;;
esac
echo "Done: $MODE"
@SomeBottle

Copy link
Copy Markdown
Author

2025.6.11: Add kornia library.

@SomeBottle

Copy link
Copy Markdown
Author

2025.7.2: Add tensorboard, tensorboardX libraries.

@SomeBottle

SomeBottle commented Jul 29, 2025 •

Copy link
Copy Markdown
Author

According to the document: https://jupyter-server.readthedocs.io/en/latest/api/jupyter_server.auth.html#jupyter_server.auth.identity.IdentityProvider.token

We can configure a token as an alternative authentication method by using -e JUPYTER_TOKEN=xxx when running this docker image.
This is especially useful when you are using jupyter extension in VSCode.

@SomeBottle

SomeBottle commented Oct 12, 2025 •

Copy link
Copy Markdown
Author

@SomeBottle

Copy link
Copy Markdown
Author

Packages planned to install: timm, einops

@SomeBottle

Copy link
Copy Markdown
Author

2025.10.22:

  1. Change the base image from bitnami/python to official python.
  2. Use USTC pypi mirror due to rate-limiting of tsinghua mirror.
  3. Add packages timm, einops.

@SomeBottle

SomeBottle commented Oct 23, 2025 •

Copy link
Copy Markdown
Author

Build command example:

docker build --build-arg HTTP_PROXY="http://127.0.0.1:7890" --build-arg HTTPS_PROXY="http://127.0.0.1:7890" --build-arg NO_PROXY="localhost,127.0.0.1,.example.com" --network=host -t somebottle/jupyter-torch:0.1.0 .

Startup command example:

# If shm-size is not specified, it may easily run out of shared memory when loading data batches with multiple workers. Docker only allocates 64 MiB by default.  
docker run --gpus all -d --restart=unless-stopped --shm-size=16g --env JUPYTER_PASSWORD='<Password used on WebUI>' \
       --env JUPYTER_TOKEN='<Token for remote connections>' \
       -v <Your notebook workdir>:/app  \
       -p 9527:9527 -p 9528:6006 --name=somebottle-jupyter somebottle/jupyter-torch:0.0.7
  • Tensorboard default port inside container: 6006, here I map it to host port 9528.
  • Tensorboard won't start by default, please use screen -dmS tb tensorboard --host=0.0.0.0 --logdir=<Log dir> to run it in the background.

@SomeBottle

SomeBottle commented Nov 16, 2025 •

Copy link
Copy Markdown
Author

Planned new package:

@SomeBottle

SomeBottle commented Dec 2, 2025 •

Copy link
Copy Markdown
Author

2025.12.2:

  1. Add python packages:
    • nvidia-ml-py
    • grad-cam
    • rich
    • seaborn
    • scipy
  2. Add Debian package:
    • libgl1

@SomeBottle

SomeBottle commented Oct 26, 2026 •

Copy link
Copy Markdown
Author

2026.10.26:

  1. Add python packages:

    • transformers
    • peft
    • accelerate
    • safetensors
    • bitsandbytes
    • trl
  2. Add nvm and install node v24 by default.

Warning

For BackWeak implementation, please use somebottle/jupyter-torch:0.0.8 or somebottle/jupyter-torch:0.0.7.

@SomeBottle

SomeBottle commented Oct 28, 2026 •

Copy link
Copy Markdown
Author

2026.10.28:

  1. Add Debian package:

    • acl
  2. Add new script share_acl.sh to solve permission issues.

share_acl script usage

Place this script in the /media/notebook_workdir directory. Assume that when I start the container, I want to mount the host's /media/notebook_workdir to the /app path inside the container.

First, run the following command in the host directory:

cd /media/notebook_workdir

chmod +x share_acl.sh

./share_acl.sh host /media/notebook_workdir 100999

Here, 100999 is the UID on the host that corresponds to the user inside the container, the exact value depends on whether Docker is running in rootless or rootful mode.

Note

You can first create a file in that directory inside the container, then check the UID that owns the file on the host.

Then enter the container and run the following command inside the container:

/app/share_acl.sh container /app

In this way, file permission issues can be resolved to some extent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment