ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

Docker を使って Jupyter notebook の環境を構築

Mac のシステムの環境を汚したくなので docker を使って Jupyter notebook の環境を構築しようと思います。

Docker について

Docker とは

コンテナ型の仮想環境を作成、配布、実行するためのプラットフォームのようです。

メリット

  • コード化されたファイルを共有することで、どこでも誰でも同じ環境が作れる
  • スクラップ&ビルドが容易にできる
  • 作成した環境を配布しやすい

作業内容

環境

  • macOS 10.15.7 (19H2)
  • docker セットアップ済み



DockerHubからjupyterのimageの取得

[jupyter/scipy-notebook] は pandas/matlabplot が一通り入っているようです。

$ docker pull jupyter/scipy-notebook
コマンド実行後の表示内容
Using default tag: latest
latest: Pulling from jupyter/scipy-notebook
e6ca3592b144: Pull complete 
534a5505201d: Pull complete 
990916bd23bb: Pull complete 
59e662992786: Pull complete 
e7ef11e12600: Pull complete 
e8d96f392447: Pull complete 
00a2d3699371: Pull complete 
ded25038ed87: Pull complete 
170e861b54f7: Pull complete 
083d83c4d7be: Pull complete 
2b52f2977511: Pull complete 
71e060d2de84: Pull complete 
d312e2e8823b: Pull complete 
fa0781258890: Pull complete 
3bd2290b4fc8: Pull complete 
b8109e1fb5f4: Pull complete 
542f15f65ba4: Pull complete 
8cf56ccad899: Pull complete 
8ce891840d4d: Pull complete 
8223e16aa32c: Pull complete 
d3155ba8272e: Pull complete 
Digest: sha256:180f73d0b169d59e417232fdaf9b943bab47850882696644408b4f48154d3e44
Status: Downloaded newer image for jupyter/scipy-notebook:latest
docker.io/jupyter/scipy-notebook:latest

Jupyter Notebook を起動する(container 停止すると作成中の notebook のデータが消える)

コマンドを実行後に token 情報を控えておきます。

$ docker run -it -p 8888:8888 --rm --name jupyter jupyter/scipy-notebook
コマンド実行後の表示内容
Executing the command: jupyter notebook
[I 12:42:28.464 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
[I 12:42:29.612 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.8/site-packages/jupyterlab
[I 12:42:29.612 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 12:42:29.615 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 12:42:29.615 NotebookApp] Jupyter Notebook 6.1.4 is running at:
[I 12:42:29.615 NotebookApp] http://f1ee1fd510ae:8888/?token=746a9570f9f34f43fac397fea9397722b21f07da758ea004
[I 12:42:29.615 NotebookApp]  or http://127.0.0.1:8888/?token=746a9570f9f34f43fac397fea9397722b21f07da758ea004
[I 12:42:29.615 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:42:29.620 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/jovyan/.local/share/jupyter/runtime/nbserver-6-open.html
    Or copy and paste one of these URLs:
        http://f1ee1fd510ae:8888/?token=746a9570f9f34f43fac397fea9397722b21f07da758ea004
     or http://127.0.0.1:8888/?token=746a9570f9f34f43fac397fea9397722b21f07da758ea004

ブラウザから動作確認

コマンド実行時に出てきた token 情報[746a9570f9f34f43fac397fea9397722b21f07da758ea004]を入力し[Log in]ボタンをクリックします。

ログイン後は普通に使用できま

Jupyter Notebookを起動する(docker container 起動時にfolderをマウント)

$ docker run -v `pwd`:/home/jovyan/work -p 8888:8888 --name jupyter jupyter/scipy-notebook
コマンド実行後の表示内容
Executing the command: jupyter notebook
[I 13:13:43.380 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
[I 13:13:44.077 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.8/site-packages/jupyterlab
[I 13:13:44.077 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 13:13:44.079 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 13:13:44.080 NotebookApp] Jupyter Notebook 6.1.4 is running at:
[I 13:13:44.080 NotebookApp] http://76014351390a:8888/?token=a79af52d3c07f6648802be88553a3048ddf02a16bd056a9b
[I 13:13:44.080 NotebookApp]  or http://127.0.0.1:8888/?token=a79af52d3c07f6648802be88553a3048ddf02a16bd056a9b
[I 13:13:44.080 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 13:13:44.083 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/jovyan/.local/share/jupyter/runtime/nbserver-6-open.html
    Or copy and paste one of these URLs:
        http://76014351390a:8888/?token=a79af52d3c07f6648802be88553a3048ddf02a16bd056a9b
     or http://127.0.0.1:8888/?token=a79af52d3c07f6648802be88553a3048ddf02a16bd056a9b

いったん止めて再度動かした場合うまくいかなかった時

[Control-C] でサービスを終えた後、再度起動させるとうまく起動しなかったので、その場合の対応についてメモします。

$ docker run -v `pwd`:/home/jovyan/work -p 8888:8888 --name jupyter jupyter/scipy-notebook
docker: Error response from daemon: Conflict. The container name "/jupyter" is already in use by container "fce281f6003a7464314cd2b9af07826a431de7262b809d19ec2b45ceff6ce65d". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
jupyter の [CONTAINER ID]

[CONTAINER ID] が [fce281f6003a] であることを控えておきます。

$ docker ps -a
[CONTAINER ID]と[STATUS]を確認んします。                                                                            
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                            PORTS               NAMES
fce281f6003a        jupyter/scipy-notebook   "tini -g -- start-no…"   12 minutes ago      Exited (130) About a minute ago                       jupyter
docker コンテナ削除

控えた[CONTAINER ID]で削除します。

$ docker rm fce281f6003a                                                             
fce281f6003a

再度コマンドを実行してサービスが利用できることを確認します。

$ docker run -v `pwd`:/home/jovyan/work -p 8888:8888 --name jupyter jupyter/scipy-notebook
Executing the command: jupyter notebook
[I 13:44:26.472 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
[I 13:44:27.171 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.8/site-packages/jupyterlab
[I 13:44:27.171 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 13:44:27.174 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 13:44:27.174 NotebookApp] Jupyter Notebook 6.1.4 is running at:
[I 13:44:27.174 NotebookApp] http://62541416e00f:8888/?token=9b15e3dc975b782a0f171121341a8efb35f0bf1eabc9f519
[I 13:44:27.174 NotebookApp]  or http://127.0.0.1:8888/?token=9b15e3dc975b782a0f171121341a8efb35f0bf1eabc9f519
[I 13:44:27.174 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 13:44:27.177 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/jovyan/.local/share/jupyter/runtime/nbserver-6-open.html
    Or copy and paste one of these URLs:
        http://62541416e00f:8888/?token=9b15e3dc975b782a0f171121341a8efb35f0bf1eabc9f519
     or http://127.0.0.1:8888/?token=9b15e3dc975b782a0f171121341a8efb35f0bf1eabc9f519