M1 Mac Apple siliconにてDockerfileをdocker build
コマンドでDocker Imageを作成する方法を紹介します。
DockerでPython環境構築する方法ではDocker HubからJupyter Labが扱える imageをpullすることで簡単にDockerを使ったPythonの環境構築することができました。
今回は機械学習をするために、Pytorchを扱えるDocker Imageの作成方法を紹介します。
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 imageを作成していきます。
当サイトではプログラミングやブログに関する記事の情報投稿をしております。
他の記事もあわせて読んでいただけると嬉しいのでよろしくお願いします!
Dockerfileを作成
下記のようにDockerfileを作成します。
FROM jupyter/scipy-notebook:ubuntu-20.04
RUN pip3 install torch torchvision torchaudio
今回はjupyterlab環境のdocker imageをベースにpytorchをインストールするだけのものを作成します。
Dockerfileのbuild
docker build
コマンドを使ってDockerfileをビルドしimageを作成します。
% ls
Dockerfile
% docker build -t jupyter/pytorch:1.0 .
[+] Building 10.8s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 129B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/jupyter/scipy-notebook:ubuntu- 0.0s
=> CACHED [1/2] FROM docker.io/jupyter/scipy-notebook:ubuntu-20.04 0.0s
=> [2/2] RUN pip3 install torch torchvision torchaudio 9.7s
=> exporting to image 1.0s
=> => exporting layers 1.0s
=> => writing image sha256:d3fa2177665c4c10919046b3bb57d1dd32629a98a8183 0.0s
=> => naming to docker.io/jupyter/pytorch:1.0 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
作成したimageを確認してみます。
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jupyter/pytorch 1.0 d3fa2177665c 2 minutes ago 2.72GB
jupyter/scipy-notebook ubuntu-20.04 28201454b6cf 3 weeks ago 2.44GB
設定したimage nameとtagが付いていることが確認できます。
Docker imageの動作確認
docker run
コマンドを使ってコンテナを立ち上げます
% docker run -it --rm jupyter/pytorch:1.0 bash
(base) jovyan@74aa97d5dff8:~$
コンテナを立ち上げを確認できましたので、次にPytorchの動作確認を行います。
(base) jovyan@74aa97d5dff8:~$ python
Python 3.9.7 | packaged by conda-forge | (default, Oct 10 2021, 15:08:54)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.9286, 0.4562, 0.0022],
[0.5176, 0.6703, 0.9645],
[0.1054, 0.1737, 0.6381],
[0.6793, 0.6808, 0.5097],
[0.0594, 0.9219, 0.4356]])
Pytorchも問題なく動作確認ができました。
Docker Hubにpush
docker push
コマンドを使ってDocker Hubにpushすることができます
% docker push hitorisekai/jupyter-torch
Using default tag: latest
The push refers to repository [docker.io/hitorisekai/jupyter-torch]
05ee1c48bea0: Pushed
4b5723c38154: Pushed
159f13eb9565: Pushed
b3cc9bcf8706: Pushed
9755afcb9c89: Pushed
7a6c6c820c37: Pushed
6cfa1b477681: Pushed
76ea099f162d: Pushed
c432eba8d7aa: Pushed
1240dcdc9885: Pushed
664fa01a32ce: Pushed
9106e3e13fb1: Pushed
fc76ab657dff: Pushed
8fb2426f83b4: Pushed
1cf3293e2cd3: Pushed
845489e6b880: Pushed
8aff8bba1f0d: Pushed
9d508eb6b00c: Pushed
350f36b271de: Pushed
latest: digest: sha256:7eb6de197bb2060440a3fd69a34b3597aadc498593d3cd81fc967eaadb4ad05e size: 4309
Tips
今回作成したPytorchとJupyter Lab環境が扱えるDocker imageはDocker Hubにて公開しております。
下記コマンドを実行することでDocker Imageを取得できます。
docker pull hitorisekai/jupyter-torch:latest
docker run
コマンドでコンテナを起動しますので、Jupyter Lab環境でPytorchを使うことができます。
% docker run -it --rm -p 8888:8888 -v "${PWD}":/home/jovyan/work --name jupyter-torch hitorisekai/jupyter-torch:latest
WARNING: Jupyter Notebook deprecation notice https://github.com/jupyter/docker-stacks#jupyter-notebook-deprecation-notice.
Entered start.sh with args: jupyter notebook
Executing the command: jupyter notebook
[I 09:02:16.844 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
[W 2022-01-20 09:02:17.410 LabApp] '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 2022-01-20 09:02:17.410 LabApp] '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 2022-01-20 09:02:17.410 LabApp] '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 2022-01-20 09:02:17.410 LabApp] '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 2022-01-20 09:02:17.418 LabApp] JupyterLab extension loaded from /opt/conda/lib/python3.9/site-packages/jupyterlab
[I 2022-01-20 09:02:17.418 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 09:02:17.422 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 09:02:17.422 NotebookApp] Jupyter Notebook 6.4.6 is running at:
[I 09:02:17.422 NotebookApp] http://9c51defdaef4:8888/?token=c8ffa2d2a428ce8b0264bbc63c1326303706da8d4eee5e37
[I 09:02:17.422 NotebookApp] or http://127.0.0.1:8888/?token=c8ffa2d2a428ce8b0264bbc63c1326303706da8d4eee5e37
[I 09:02:17.422 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 09:02:17.425 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/jovyan/.local/share/jupyter/runtime/nbserver-8-open.html
Or copy and paste one of these URLs:
http://9c51defdaef4:8888/?token=c8ffa2d2a428ce8b0264bbc63c1326303706da8d4eee5e37
or http://127.0.0.1:8888/?token=c8ffa2d2a428ce8b0264bbc63c1326303706da8d4eee5e37
一番下のhttp://以下をコピーしてブラウザに貼り付けることでJupyter Lab環境が起動します。
コメント