主要是把jupyterhub数据库要改为portal_system的数据库,后面通过挂载来实现两者共用一个数据库
同时jupyterhub的配置文件也作了修改,使之可以数据持久化,限制计算资源(CPU、内存)
jupyterhub配置文件如下:
c = get_config()
from jupyterhub.auth import Authenticator
from tornado import gen
import os
import sqlite3
# 重写验证类,使用sqlite数据库验证
class SQLiteAuthenticator(Authenticator):
"""JupyterHub Authenticator Based on SQLite"""
def __init__(self, **kwargs):
super(SQLiteAuthenticator, self).__init__(**kwargs)
@staticmethod
def _verify_password(username, password):
try:
# to define sqlite db location in hub images
os.environ["JUPYTERHUB_SQLITEDB_PATH"]="/srv/database/db.sqlite3"
sql_cnn = sqlite3.connect(os.getenv('JUPYTERHUB_SQLITEDB_PATH'))
cursor = sql_cnn.cursor()
sql = ("SELECT `password` FROM users WHERE `username` = '{}'").format(username)
cursor.execute(sql)
user_passwd = cursor.fetchone()[0]
input_passwd = password
if user_passwd == input_passwd:
cursor.close()
sql_cnn.close()
return True
else:
cursor.close()
sql_cnn.close()
return False
except:
cursor.close()
sql_cnn.close()
return False
@gen.coroutine
def authenticate(self, handler, data):
username = data['username']
passwd = data['password']
if self._verify_password(username, passwd):
return data['username']
else:
return None
c.JupyterHub.ip = '0.0.0.0'
c.JupyterHub.port = 80
#设置管理员
c.Authenticator.admin_users = {'jiangyu'}
c.JupyterHub.authenticator_class = SQLiteAuthenticator
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'
c.DockerSpawner.image = 'dhso/jupyter_lab_singleuser:v1'
#数据持久化
notebook_dir = '/home/jovyan/work'
c.DockerSpawner.volumes = { 'jupyterhub-user-{username}': notebook_dir,
'/data/shared_data': {"bind": '/home/jovyan/shared_data',"mode":"ro"}}
#限制资源
c.Spawner.cpu_limit = 1
c.Spawner.mem_limit = '1G'
# 使用jupyterlab
c.DockerSpawner.extra_create_kwargs.update({ 'command': "start-singleuser.sh --SingleUserNotebookApp.default_url=/lab" })
network_name = 'jupyterhub_network'
c.DockerSpawner.use_internal_ip = True
c.DockerSpawner.network_name = network_name
c.DockerSpawner.extra_host_config = { 'network_mode': network_name }
#关掉服务时移除所有用户容器
c.DockerSpawner.remove_containers = True
c.DockerSpawner.debug = True
#c.JupyterHub.hub_ip需要配置,以下二选一都可
#from jupyter_client.localinterfaces import public_ips
#c.JupyterHub.hub_ip = public_ips()[0]
c.JupyterHub.hub_ip = '0.0.0.0'
启动容器命令如下,不同点在于增加了一个挂载卷
docker run -d --name jupyterhub -p 0.0.0.0:5678:80 \
--network jupyterhub_network \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /data/jupyterhub:/srv/jupyterhub \
-v /data/PortalSystem/db.sqlite3:/srv/database/db.sqlite3 test/jupyterhub:v1
#最后一个-v将PortalSystem的数据库文件挂载到容器里,作为jupyterhub登录验证的数据库
另外,portal_system页面的跳转链接要设置成服务器的ip和对应服务的端口