且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JupyterLab多用户配置

更新时间:2022-08-13 09:32:47

上一篇初步安装了JupyterLab,但是这个不能多用户真的让人难过。

拉取镜像运行

在阿里云镜像市场上找到一个配置好Ananoconda和JupyterLab的镜像,使用如下代码直接拉取

docker pull registry.cn-shanghai.aliyuncs.com/stormstone/juplab

带端口号运行,其中8000是本机主要用户端口,7777是第二个用户的端口,要多少用户就用多少端口。Docker的/home下新建用户并设置密码,密码多少无所谓,Docker的work目录也挂过来,你可以用OSS在本机挂载。

docker run -p 8000:8888 -p 7777:7777 -d --name jupyterlabofstormstone -v `pwd`/新建一个宿主机Jupyter目录/home:/home -v `pwd`/新建一个宿主机Jupyter目录/work:/work  --restart=always registry.cn-shanghai.aliyuncs.com/stormstone/juplab:v2

进入镜像,新建用户

docker exec -it jupyterlabofstormstone bash
adduser share # share 是我新建的用户
passwd share

切换用户,生成配置文件,其实这个文件在哪里大概不重要,后面带绝对路径运行即可,不过咱还是规范点。

su share
jupyter notebook --generate-config

修改/home/share/.jupyter/jupyter_notebook_config.py加入如下字段,密码是空密码

c.NotebookApp.ip='*'               
c.NotebookApp.password = 'sha1:840438352003:10c0d77ee275c741e22e6929cb7822e61ab5c256'    
c.NotebookApp.open_browser = False      
c.NotebookApp.port =7777                         
c.NotebookApp.notebook_dir = '/work/share'  
c.PAMAuthenticator.encoding = 'utf8'   
c.NotebookApp.default_url = '/lab'
c.NotebookApp.allow_remote_access = True

修改root配置

切换回root,重复上述动作。

su root
jupyter notebook --generate-config

生成密码

ipython

输入

from notebook.auth import passwd
passwd()

在跳出的框内输入密码,两次,得到一个Token值。复制一下一会要用。

修改/root/.jupyter/jupyter_notebook_config.py加入如下字段

c.NotebookApp.ip='*'               
c.NotebookApp.password = 你自己生成Token
c.NotebookApp.open_browser = False      
c.NotebookApp.port =8888                        
c.NotebookApp.notebook_dir = '/work'  
c.PAMAuthenticator.encoding = 'utf8'

保存,退出镜像

重启镜像

docker stop jupyterlabofstormstone
docker restart jupyterlabofstormstone
docker exec jupyterlabofstormstone nohup jupyter notebook --config /home/share/.jupyter/jupyter_notebook_config.py --allow-root # 给新用户的端口也跑起来,会输出一些信息,不用管,ctrl-c也没有影响

现在可以在http://IP:8000http://IP:7777两个端口打开了,为两个端口绑定域名,设置操作权限等。

参考

JupyterHub 配置和管理

实验室工作站jupyterhub安装笔记

JupyterLab on JupyterHub

jupyter notebook 权限控制,实现多个用户工作空间不同,同时使用

如何用一台服务器给多个 Jupyter 用户提供服务