Skip to content

Instantly share code, notes, and snippets.

@SomeBottle
Last active November 16, 2025 14:11
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 Docker Container 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 && \
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
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"
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 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
USER somebottle
# 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
@SomeBottle
Copy link
Author

2025.6.11: Add kornia library.

@SomeBottle
Copy link
Author

2025.7.2: Add tensorboard, tensorboardX libraries.

@SomeBottle
Copy link
Author

SomeBottle commented Jul 29, 2025

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
Copy link
Author

SomeBottle commented Oct 12, 2025

@SomeBottle
Copy link
Author

Packages planned to install: timm, einops

@SomeBottle
Copy link
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
Copy link
Author

SomeBottle commented Oct 23, 2025

Startup command example:

# If shm-size is not specified, it may easily get out of shared memory when loading data batch for training, 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
Copy link
Author

Planned new package:

  • pyNVML - used to retrieve detailed GPU status info.

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