# docker-jupyterhub ## Getting started [main reference](https://github.com/jupyterhub/jupyterhub-deploy-docker) 构建自己的docker镜像 修改自己想要的python环境在文件`Dockerfile.jupyterhub` 基础镜像是:`jupyter/scipy-notebook` 里面含有conda ```shell docker buildx build -t hotwa/jupyterhub:latest . -f Dockerfile.jupyterhub --load ``` 修改docker-compose.yml文件环境变量:DOCKER_NOTEBOOK_IMAGE ```shell DOCKER_NOTEBOOK_IMAGE: hotwa/jupyterhub:latest ``` 也可以通过 docker-compose 构建镜像 ```shell docker compose build ``` 启动docker-compose.yml ```shell docker compose up -d ``` 停止 ```shell docker compose down ``` [基本镜像](https://github.com/jupyter/docker-stacks/tree/main/images/base-notebook) https://github.com/jupyter/docker-stacks/tree/main/images [jupyter-gpu](https://github.com/iot-salzburg/gpu-jupyter) 构建自己的基础镜像: ```shell git clone https://github.com/jupyter/docker-stacks cd docker-stacks/images/base-notebook # 修改这个目录下面的 Dockerfile文件, 在后面加就行,里面已经有了mamba、conda docker buildx build -t hotwa/notebook:latest . -f Dockerfile ``` ## 修改jupyterhub启动notebook的路径映射 修改jupyterhub_config.py文件,修改卷的路径: ```python c.DockerSpawner.volumes = { "jupyterhub-user-{username}": notebook_dir, "/mnt/mydrive": "/home/jovyan/work/mydrive", "/mnt/mydrive/project/docker-jupyterhub/id_rsa": "/home/jovyan/.ssh/id_rsa", } ``` ## 启动notebook修改 启动notebook修改docker-compose.yml文件,修改notebook的基础镜像: ```yaml DOCKER_NOTEBOOK_IMAGE: hotwa/notebook:latest ``` ## Alternatives - [rocker/binder](https://rocker-project.org/images/versioned/binder.html) - From the R focused [rocker-project](https://rocker-project.org), lets you run both RStudio and Jupyter either standalone or in a JupyterHub - [jupyter/repo2docker](https://github.com/jupyterhub/repo2docker) - Turn git repositories into Jupyter-enabled Docker Images - [openshift/source-to-image](https://github.com/openshift/source-to-image) - A tool for building artifacts from source code and injecting them into docker images - [jupyter-on-openshift/jupyter-notebooks](https://github.com/jupyter-on-openshift/jupyter-notebooks) - OpenShift compatible S2I builder for basic notebook images ## nvidia-container-toolkit add 参考了 llama [容器部署](https://hub.docker.com/r/ollama/ollama) ```shell curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \ | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \ | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \ | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list sudo apt-get update sudo apt-get install -y nvidia-container-toolkit sudo apt-get install nvidia-container-runtime -y ``` Configure Docker to use Nvidia driver ```shell sudo nvidia-ctk runtime configure --runtime=docker sudo systemctl restart docker ``` 实际上这个操作修改了/etc/docker/daemon.json文件: ```shell { "registry-mirrors": [ "https://0b27f0a81a00f3560fbdc00ddd2f99e0.mirror.swr.myhuaweicloud.com", "https://ypzju6vq.mirror.aliyuncs.com", "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ], "runtimes": { "nvidia": { "args": [], "path": "nvidia-container-runtime" } } } ``` 配置完成之后: ```shell docker-compose up -d ``` 在jupyterhub_config.py中添加:这样jupyterhub的容器启动镜像就可以使用宿主机的显卡了 ```python # GPU 和网络配置 c.DockerSpawner.extra_host_config = { #'network_mode': 'host', 'runtime': 'nvidia' } c.DockerSpawner.environment = { 'NVIDIA_DRIVER_CAPABILITIES': 'compute,utility', 'NVIDIA_VISIBLE_DEVICES': 'all' } ``` ## 清除卷 ```shell docker volume ls docker-compose down docker rm docker volume rm basic-example_jupyterhub-data docker volume rm docker-jupyterhub_jupyterhub-data docker volume rm jupyterhub-user-admin ``` ## 修改绑定到notebook的卷 修改jupyterhub_config.py文件,修改卷的路径: ```python c.DockerSpawner.volumes = { "jupyterhub-user-{username}": notebook_dir, "/mnt/mydrive": "/home/jovyan/work/mydrive", "/mnt/mydrive/project/docker-jupyterhub/id_rsa": "/home/jovyan/.ssh/id_rsa", } ``` docker-compose.yml文件,修改notebook的基础镜像: ```yaml DOCKER_NOTEBOOK_IMAGE: hotwa/notebook:latest ``` ## 构建 构建基础notebook镜像 ```shell docker buildx build -t hotwa/notebook:latest -f Dockerfile.base-notebook . --load ``` 构建jupyterhub镜像 ```shell docker compose build ``` ## 推送镜像至阿里云私有仓库 ```shell docker login --username=ze.ga@qq.com registry.cn-hangzhou.aliyuncs.com # 重命名 docker tag 2ad3860183ce registry.cn-hangzhou.aliyuncs.com/hotwa/jupyterhub:latest docker tag ddf815cbaa9b registry.cn-hangzhou.aliyuncs.com/hotwa/notebook:latest # 推送 docker login --username=ze.ga@qq.com registry.cn-hangzhou.aliyuncs.com docker push registry.cn-hangzhou.aliyuncs.com/hotwa/jupyterhub:latest docker push registry.cn-hangzhou.aliyuncs.com/hotwa/notebook:latest # 拉取 docker pull registry.cn-hangzhou.aliyuncs.com/hotwa/jupyterhub:latest docker pull registry.cn-hangzhou.aliyuncs.com/hotwa/notebook:latest ``` ## add mojo ```shell docker build --no-cache \ --build-arg AUTH_KEY= mut_efe460b898e3482bb6208bfcd4a51d7e \ -t mojosdk:latest \ -f mojo/examples/docker/Dockerfile.mojosdk . docker buildx build --build-arg AUTH_KEY=mut_efe460b898e3482bb6208bfcd4a51d7e -t hotwa/notebook:latest . -f Dockerfile.base-notebook --load ``` ## 自建证书 openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx-selfsigned.key -out nginx-selfsigned.crt openssl dhparam -out dhparam.pem 2048 这将会在当前目录生成私钥nginx-selfsigned.key,自签名证书nginx-selfsigned.crt,以及Diffie-Hellman参数dhparam.pem。 然后,在您的nginx.conf中,您需要使用这些文件: ```shell ssl_certificate /path/to/nginx-selfsigned.crt; ssl_certificate_key /path/to/nginx-selfsigned.key; ssl_dhparam /path/to/dhparam.pem; ```