文章目录
- docker
- docker的安装
- 将当前用户添加到docker用户组
- 拉取一个镜像
- 创建配置目录
- 启动docker服务
- 登录jupyter容器
- 安装vim
- 更换源
- Jupyter
- 设置python
- 安装Jupyter
- 设置密码
- 生成jupyter的config文件
- 修改配置文件
- 启动jupyter notebook
- 安装插件
- R
- R 安装
- 在jupyter notebook中配置R的kernel
- 配置Nginx
docker
docker的安装
将当前用户添加到docker用户组
为了避免每次使用docker
命令都需要加上sudo
权限,可以将当前用户加入安装中自动创建的docker
用户组(可以参考官方文档):
sudo usermod -aG docker $USER
拉取一个镜像
docker pull ubuntu:20.04
创建配置目录
# xx为你的用户名
mkdir -p /home/xx/data/jupyter/
mkdir -p /home/xx/data/jupyter/.jupyter
chmod 777 -R /home/xx/data/jupyter/
启动docker服务
docker run --name jupyter -itd \
-p 8888:8888 \
-v /home/xx/data/jupyter:/home/jupyter \
ubuntu:20.04
- 命名容器为jupyter,后台运行
- 映射宿主机8888端口到容器的8888端口
- 挂载宿主机目录/home/xx/data/jupyter到容器目录/home/jupyter
登录jupyter容器
docker attach jupyter
安装vim
apt-get install vim
更换源
cd /etc/apt/
vim sources.list
清华镜像
apt-get update
Jupyter
以下均在root下运行的命令如果在普通用户需要加上sudo权限
设置python
- 更新本地apt包索引
apt-get update
- 安装 pip 和 Python 头文件
apt-get install python3-pip python3-dev
- 升级pip
pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
- 安装常用命令
apt-get install curl
apt-get install unzip
安装Jupyter
pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
设置密码
ipython
在ipython中输入以下命令
from notebook.auth import passwd
passwd()
生成密钥
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'argon2:XXXXXX' # 复制上引号内的内容
In [3]: exit()
生成jupyter的config文件
jupyter notebook --generate-config
修改配置文件
vim ~/.jupyter/jupyter_notebook_config.py
在末尾加入如下内容
c.NotebookApp.ip='*'
c.NotebookApp.password = u'argon2:$XXXXX' # 刚刚复制的内容
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888
启动jupyter notebook
nohup jupyter notebook --allow-root &
安装插件
Jupyter Notebook 扩展插件(nbextensions)是一些 JavaScript 模块,我们可以使用插件强化 Notebook 的功能。扩展插件本质上修改了 Jupyter UI,以实现更强大的功能。
界面添加 Nbextensions
pip install jupyter_nbextensions_configurator -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
jupyter nbextensions_configurator enable --user
jupyter nbextension list
退出jupyter容器Ctrl+p,Ctrl+q
docker restart jupyter
docker attach jupyter
nohup jupyter notebook --allow-root &
安装常用扩展集合
pip install jupyter_contrib_nbextensions -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
jupyter contrib nbextension install --user
jupyter nbextension list
R
R 安装
apt-get install r-base
在jupyter notebook中配置R的kernel
安装必要的lib
apt-get install libzmq3-dev libssl-dev openssl libssl-dev
进入R环境
R
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'uuid', 'digest'))
注: 会提示选择mirrors, 建议选择 0-cloud
install.packages( 'IRkernel')
IRkernel::installspec()
如果显示如下结果,说明配置成功。
[InstallKernelSpec] Installed kernelspec ir in /home/xxx/.local/share/jupyter/kernels/ir
注意:在jupyter上加载R包时一定要设定镜像,不然一般会失败:
# 通过如下命令设定镜像,比如我选择镜像ustc
options(repos = 'http://mirrors.ustc.edu.cn/CRAN/')
# 检查镜像是否更改
getOption('repos')
配置Nginx
jupyter 使用了 websocket 协议,所以需要配置支持 websocket。
如果不配置的话,通过域名访问时会报错无法连接内核,也就无法运行python脚本。
server {
listen 80;
server_name 你的域名;
charset utf-8;
location /{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://你的ip:8888;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
如有错误以及可以改进的地方欢迎在下方评论区留言!