サーバーでDockerでJupyter notebook

リモートサーバーでJupyter notebookを使う場合、SSHでログイン後、--ipオプションをつけて起動すれば良い。

$ jupyter notebook --ip=*
[W 12:06:27.853 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 12:06:27.854 NotebookApp] The port 8888 is already in use, trying another port.
[I 12:06:27.862 NotebookApp] Serving notebooks from local directory: /home/XXX
[I 12:06:27.862 NotebookApp] Jupyter Notebook 6.1.6 is running at:
[I 12:06:27.862 NotebookApp] http://XXX-desktop:8889/?token=58287be265ecc0a16b21c0eeecb9e07d89ebb80078cf6224
[I 12:06:27.862 NotebookApp]  or http://127.0.0.1:8889/?token=58287be265ecc0a16b21c0eeecb9e07d89ebb80078cf6224
[I 12:06:27.863 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 12:06:27.879 NotebookApp] No web browser found: could not locate runnable browser.
[C 12:06:27.879 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/XXX/.local/share/jupyter/runtime/nbserver-9183-open.html
    Or copy and paste one of these URLs:
        http://XXX-desktop:8889/?token=58287be265ecc0a16b21c0eeecb9e07d89ebb80078cf6224
     or http://127.0.0.1:8889/?token=58287be265ecc0a16b21c0eeecb9e07d89ebb80078cf6224

起動確認後、クライアントでブラウザからhttp://<jupyter実行ホストのIP Address>:<port番号(上記だと8889)> にアクセスすればいい。port番号は使用されていないものでデフォルト(8888)から一番近いものが使用される。そのため、毎回同じでない可能性があるため、他人のNotebookにアクセスしないよう注意。(まあ、Passwordがあるので大丈夫だと思う。)

ただ、この状態だと環境が共通となってしまい不便である。そのためDockerを使用してJupyter notebookを実行する方法を示す。

準備(Dockerのインストール)

下記を参考にDockerをインストールする。GPUを使用したい場合は、Nvidia-Dockerも導入する。

docs.docker.com

medium.com

コンテナ起動

コンテナを起動して、Jupyter notebookをインストールする。このとき、--network hostオプションを使用することで、外部からのアクセスが可能となる。

下記だとPython3.8のイメージでコンテナを起動している。

$ docker run -it --network host python:3.8-slim-buster bash
# apt update && apt install  python3-pip python-pip -y && pip3 install jupyter

通常時とほぼ同様に、Notebookを起動する。--allow-rootをつけておかないと怒られて起動しないので注意。

# jupyter notebook --ip=* --allow-root

起動後、http://<jupyter実行ホストのIP Address>:<port番号> にアクセスすると、Notebookが使える。設定したパスワードは覚えておく。

終了する

終了する場合はjupyter notebook を実行したターミナルでCtrl + CでNotebookを終了し、exitまたはCtrl + Dでコンテナから抜ける。

コンテナの動作状況は、以下のように確認できる。

$ docker ps -a
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                    PORTS               NAMES
ce6c11aa6e9a        python:3.8-slim-buster              "bash"                   27 minutes ago      Exited (0) 2 seconds ago                       great_meninsky

再起動する

コンテナをRestartして、コンテナのなかに入る。この場合、コンテナIDまたはコンテナ名が必要なので、上のdocker psコマンドで調べておく。

コンテナIDの場合

$ docker restart ce6c11aa6e9a
$ docker exec -it ce6c11aa6e9a bash
# jupyter notebook --ip=* --allow-root

コンテナ名の場合

$ docker restart great_meninsky
$ docker exec -it great_meninsky bash
# jupyter notebook --ip=* --allow-root

bashが必要ない場合は、直接起動してもOK。

$ docker restart ce6c11aa6e9a
$ docker exec -it ce6c11aa6e9a jupyter notebook --ip=* --allow-root