M1 Mac Apple siliconにてDockerでJupyter Lab環境を構築する方法を紹介します。
私自身がPythonで機械学習をするためにDockerを使ってJupyter Labを起動させるまでの手順を紹介します。
Dockerをまだインストールされていない方はMacにDockerを入れる方法をチェックしてください。
私のDocker環境は下記になります。
% docker version
Client:
Cloud integration: v1.0.22
Version: 20.10.11
API version: 1.41
Go version: go1.16.10
Git commit: dea9396
Built: Thu Nov 18 00:36:09 2021
OS/Arch: darwin/arm64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:34:44 2021
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
それではDockerでのJupyterLab環境の構築をやっていきます。
当サイトではプログラミングやブログに関する記事の情報投稿をしております。
他の記事もあわせて読んでいただけると嬉しいのでよろしくお願いします!
Docker Image の取得

Docker Hubでjupyterと調べるとたくさん出てきますが、今回は簡単なデータ解析を行えるモジュールを含んだscipy-notebookのDocker Imageを取得します。ターミナルで下記コマンドを実行します。
% docker pull jupyter/scipy-notebook:ubuntu-20.04
取得したDocker Imageは% docker images
で確認することができます。
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jupyter/scipy-notebook ubuntu-20.04 28201454b6cf 3 days ago 2.44GB
Docker コンテナを立ち上げる
ターミナルで下記コマンドを実行しコンテナを立ち上げます。
--nameオプション
でコンテナに名前をつけることができます。 [container-name]
は任意に変更して実行してください。
% docker run -it --rm -p 8888:8888 -v "${PWD}":/home/jovyan/work --name [container-name] jupyter/scipy-notebook:ubuntu-20.04
今回使用したオプションは下記にまとめておきます。
–name | コンテナに名前をつける | docker run –name hitorisekai image:tag |
–rm | 終了時にコンテナを自動的に削除 | docker run –rm image:tag |
-it | コンソールに結果表示 | docker run -it image:tag |
-p | ポートフォワーディング | docker run -p 8888:8888 |
-v | ボリュームのマウント | docker run -v “${PWD}”:/home/jovyan/work image:tag |
Jupyter Labの起動

先ほどのコンテナを立ち上げると下記のようにターミナルに表示されますので、24行目のhttp://以降をコピーしてブラウザに貼り付けます。
Entered start.sh with args: jupyter lab
Executing the command: jupyter lab
[I 2021-12-31 08:19:50.855 ServerApp] jupyterlab | extension was successfully linked.
[W 2021-12-31 08:19:50.857 NotebookApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-31 08:19:50.857 NotebookApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-31 08:19:50.857 NotebookApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[I 2021-12-31 08:19:50.862 ServerApp] Writing Jupyter server cookie secret to /home/jovyan/.local/share/jupyter/runtime/jupyter_cookie_secret
[I 2021-12-31 08:19:50.963 ServerApp] nbclassic | extension was successfully linked.
[I 2021-12-31 08:19:50.977 ServerApp] nbclassic | extension was successfully loaded.
[I 2021-12-31 08:19:50.978 LabApp] JupyterLab extension loaded from /opt/conda/lib/python3.9/site-packages/jupyterlab
[I 2021-12-31 08:19:50.978 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 2021-12-31 08:19:50.980 ServerApp] jupyterlab | extension was successfully loaded.
[I 2021-12-31 08:19:50.981 ServerApp] Serving notebooks from local directory: /home/jovyan
[I 2021-12-31 08:19:50.981 ServerApp] Jupyter Server 1.13.1 is running at:
[I 2021-12-31 08:19:50.981 ServerApp] http://3c41d3b59f3a:8888/lab?token=ac8118f48972901e87c924bd73e8f43dec5b67115f0cefc1
[I 2021-12-31 08:19:50.981 ServerApp] or http://127.0.0.1:8888/lab?token=ac8118f48972901e87c924bd73e8f43dec5b67115f0cefc1
[I 2021-12-31 08:19:50.981 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2021-12-31 08:19:50.983 ServerApp]
To access the server, open this file in a browser:
file:///home/jovyan/.local/share/jupyter/runtime/jpserver-8-open.html
Or copy and paste one of these URLs:
http://3c41d3b59f3a:8888/lab?token=ac8118f48972901e87c924bd73e8f43dec5b67115f0cefc1
or http://127.0.0.1:8888/lab?token=ac8118f48972901e87c924bd73e8f43dec5b67115f0cefc1
下記のようにJupyter Labに接続ができます。

Jupyter Labの終了
ターミナルにてControl + C
を押すとJupyter serverをシャットダウンするかと聞かれますのでyを入力しEnterを押します
Shutdown this Jupyter server (y/[n])? y
また、Dockerコンテナを立ち上げるときに--rmオプション
をつけているためJupyterLabを終了するとコンテナも削除されます。
Tips

Dockerコンテナを立ち上げるコマンドの最後にbash
をつけると下記のようにコンテナに入ることができます。
% docker run -it --rm -e JUPYTER_ENABLE_LAB=yes -p 8888:8888 -v "${PWD}":/home/jovyan/work --name hitorisekai jupyter/scipy-notebook:ubuntu-20.04 bash
(base) jovyan@c3bd58e547a1:~$
コンテナから出るときは下記のようにexit
で出ることができます。
(base) jovyan@c3bd58e547a1:~$ exit
コメント