使用docker-compose尝试《使用Jupyter Notebook进行文学基础设施运作的建议》

为了什么

为了尝试国立情报学研究所(NII)利用Jupyter Notebook运营文学基础设施的建议,可以轻松启动NII公开提供的Docker容器。同时,还可以修复个人觉得有些不方便的部分。

参考 : 推荐使用Jupyter笔记本进行文艺式基础设施运营

终端设备

以下是对中国的本地版本:
从这里开始的工作假设以下环境。
在工作终端上运行以下工具。
* Docker服务器
* Docker Compose
* 用于连接Jupyter Notebook的Web浏览器
* OpenSSL

创建所需文件

请参考:基础设施的Jupyter使用

这个任务要在作业终端的docker用户下进行。

创建工作目录

LC_DIR=/opt/docker/jupyter-LC
sudo mkdir -p ${LC_DIR}/{conf,notebooks,notebooks/.ansible}
cd ${LC_DIR}

Ansible 配置设置 de

根据此处提供的基本设置创建Ansible配置文件。根据需要进行相应更改。

cat <<EOF |sudo tee ${LC_DIR}/conf/ansible.cfg
[defaults]
forks = 3
remote_user = ansible
private_key_file = /notebooks/.ansible/ansible_id_ed25519
ansible_ssh_user = ansible
inventory = /notebooks/.ansible/inventory
EOF

创建库存文件

提前创建一个用于指定 Ansible 目标服务器的 inventory 文件模板。

echo "[TARGETS]" | sudo tee ${LC_DIR}/notebooks/.ansible/inventory

产生公钥

创建用于Ansible SSH连接的公钥。

sudo ssh-keygen -f ${LC_DIR}/notebooks/.ansible/ansible_id_ed25519 -t ed25519 -N ""

Jupyter Notebook 设置。

为了能够以哈希方式传递密码,需要修改Jupyter-LC的配置,因为当前配置需要以明文形式进行密码记录。

cat <<EOF |sudo tee ${LC_DIR}/conf/jupyter_notebook_config.py
import os
c.NotebookApp.ip = '*'
c.MultiKernelManager.kernel_manager_class = 'lc_wrapper.LCWrapperKernelManager'
c.KernelManager.shutdown_wait_time = 10.0

if 'PASSWORD' in os.environ:
    c.NotebookApp.password = os.environ['PASSWORD']
EOF

Docker Compose的配置

创建用于Jupyter-LC的docker-compose.yml文件。

cat <<EOF |sudo tee ${LC_DIR}/docker-compose.yml
version: '2'
services:
  notebook:
    image: niicloudoperation/notebook
    environment:
      TZ: Asia/Tokyo
    ports:
      - 127.0.0.1:8888:8888
    volumes:
      - ./notebooks:/notebooks
      - ./conf/jupyter_notebook_config.py:/home/bit_kun/.jupyter/jupyter_notebook_config.py
      - ./conf/ansible.cfg:/etc/ansible/ansible.cfg
    environment:
      - PASSWORD=sha1:3703d242f3a6:04ef6e0984039af6fc1d9ec054e583898cedd500
    restart: always
EOF

权限设置

在Jupyter-LC_docker的Dockerfile中,由于uid=1000 gid=100,所以需要设置目录的所有者和组。

sudo chown 1000:100 -R $LC_DIR

启动 Jupyter Notebook 容器

在之前的工作中,已经准备好启动容器,可以使用docker-compose进行启动。

cd $LC_DIR
docker-compose up -d

在启动Jupyter Notebook容器之后,可以在Web浏览器中打开http://localhost:8888/。
如果要求输入密码,请输入”PASSWORD”作为示例中的设置选项。

給你一個禮物

在Jupyter Notebook的单元格中写下密码的哈希值,并执行该代码,即可得到该值。
将该值设置为docker-compose文件中的密码,并重新启动容器。

from notebook.auth import passwd
print(passwd())