JupyterHub Jupyterlab DockerSpawner Docker单主机部署

使用自定义验证,DockerSpawner,mysql

后面添加新主机的时候考虑使用SwarmSpawner,生产环境使用Kubernetes(k8s) + KubeSpawner

安装docker和docker-compose

https://docs.docker.com/engine/install/ubuntu/

https://docs.docker.com/compose/install/

加入docker用户组,避免每次使用sudo

sudo usermod -aG docker USERNAME

从dockerhub获取需要的镜像jupyterhub和jupyter/base-notebook

docker pull jupyterhub/jupyterhub
docker pull jupyter/base-notebook

使用docker-compose管理服务

配置

docker-compose.yaml内容:

version: "3.8"
services:
  hub:
    env_file: .env
    build:
      context: .
      dockerfile: Dockerfile
      args:
        JUPYTERHUB_VERSION: ${JUPYTERHUB_VERSION}
    restart: always
    image: jupyterhub
    container_name: jupyterhub
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:rw"
    ports:
      - "8000:8000"
    environment:
      DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME}
      DOCKER_NOTEBOOK_IMAGE: ${DOCKER_NOTEBOOK_IMAGE}
      DOCKER_NOTEBOOK_DIR: ${DOCKER_NOTEBOOK_DIR}
    command: >
      jupyterhub -f /srv/jupyterhub/jupyterhub_config.py

networks:
  default:
    external:
      name: ${DOCKER_NETWORK_NAME}

环境变量.env:

JUPYTERHUB_VERSION=latest
DOCKER_NETWORK_NAME=jupyterhub-network
DOCKER_NOTEBOOK_IMAGE=base-notebook
DOCKER_NOTEBOOK_DIR=/home/testuser/work

jupyterhub的Dockerfile:

FROM jupyterhub/jupyterhub:latest
COPY requirements.txt /tmp/requirements.txt
#RUN apt update
RUN python3 -m pip install --no-cache -r /tmp/requirements.txt
COPY jupyterhub_config.py /srv/jupyterhub/jupyterhub_config.py
COPY style.min.css /usr/local/share/jupyterhub/static/css/style.min.css
COPY my_logo.png /srv/jupyterhub/my_logo.png

requirement.txt

dockerspawner
mysql-connector

base-notebook的Dockerfile:

FROM jupyter/base-notebook:latest
USER root
RUN groupadd -g 8000 testuser
RUN useradd -u 8000 -g testuser-G users -m testuser
ENV HOME=/home/testuser
WORKDIR $HOME
#RUN userdel -r jovyan
COPY custom.css $CONDA_DIR/lib/python3.8/site-packages/notebook/static/custom/custom.css
#COPY custom.js $CONDA_DIR/lib/python3.8/site-packages/notebook/static/custom/custom.js
RUN chown -R testuser$testuser/lib/python3.8/site-packages/notebook/static/custom
USER testuser

jupyterhub_config.py

import os
from tornado import gen
from jupyterhub.auth import Authenticator
from requests import post, codes
from json import loads, dumps

class MCAuthenticator(Authenticator):
  @gen.coroutine
  def authenticate(self, handler, data):
    #refresh_token = handler.get_cookie('refresh_token')
    #添加你的验证code
    return username

#此处的python方便使用nginx反向代理
c.JupyterHub.bind_url = 'http://:8000/python'
c.JupyterHub.hub_ip = 'jupyterhub'
c.JupyterHub.authenticator_class = MCAuthenticator
# Specify path to a logo image to override the Jupyter logo in the banner.
c.JupyterHub.logo_file = '/srv/jupyterhub/my_logo.png'
# Shuts down all user servers on logout
c.JupyterHub.shutdown_on_logout = True
# Spawn single-user servers as Docker containers
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'

# Number of days for a login cookie to be valid. Default is two weeks.
c.JupyterHub.cookie_max_age_days = 1
# c.JupyterHub.db_url='mysql+mysqlconnector://Mysql用户名:Mysql密码@数据库地址/数据库名称'

# c.JupyterHub.ssl_key = os.environ['SSL_KEY']
# c.JupyterHub.ssl_cert = os.environ['SSL_CERT']

# Defaults to an empty set, in which case no user has admin access.
c.Authenticator.admin_users = {'testuser'}
#自动登陆,跳过登录页面,使用了Nginx代理,已经在自己网站登陆过
c.Authenticator.auto_login = True

c.Spawner.default_url = 'lab'

# Spawn containers from this image
single_user_image = os.environ['DOCKER_NOTEBOOK_IMAGE'] or 'jupyter/base-notebook'
c.DockerSpawner.image = single_user_image

network_name = os.environ['DOCKER_NETWORK_NAME']
c.DockerSpawner.network_name = network_name
c.DockerSpawner.extra_host_config = {'network_mode': network_name}

notebook_dir = os.environ.get('DOCKER_NOTEBOOK_DIR') or '/home/testuser/work'
c.DockerSpawner.notebook_dir = notebook_dir
c.DockerSpawner.volumes = {'/home/testuser/jupyterhub-user-{username}': notebook_dir}
# 未生效,重建镜像
# c.DockerSpawner.extra_container_spec = {'user': '0'}
# c.DockerSpawner.environment = {'NB_USER':  'testuser', 'NB_UID': 8000, 'NB_GID': 8000, 'CHOWN_HOME': 'yes'}

# For debugging arguments passed to spawned containers
c.DockerSpawner.debug = True
c.DockerSpawner.remove = True

制作镜像

docker-compose build
docker build -f base-notebook/Dockerfile -t base-notebook .
docker images

启动服务

新建jupyterhub网络,与jupyterhub相关的服务会加入网络

docker network create jupyterhub-network
docker-compose up -d

退出服务

docker-compose down

 

你可能感兴趣的:(web,AI)