【ローカル仮想環境構築】Dockerを自宅のWindowsPCに入れてJupyterLab(コンテナ)を立ち上げる流れについて

メモ書きとしてWIndowsにDockerを入れて、docker-composeを導入する手順、jupyterを起動するまでの手順を残す

基本的にDockerはコンテナ仮想化を提供するプラットフォームアプリで、下記のような手順で実施可能

***１　
Dockerfileを作成し、docker buildでコンテナイメージを作成。***
***２　
docker runでそのイメージからコンテナを起動し、必要に応じてdocker execでコンテナ内でコマンドを実行してアプリを操作。***

上記の他にはdocker-composeという仕組みがあり
***docker-compose.yml ファイルを使用して、複数のコンテナの設定を一つのファイルにまとめて定義します。このファイルには、各サービス（コンテナ）のイメージ、ビルド手順、環境変数、ボリュームマウント、ネットワーキング設定などを記述できます。***

## Docker for DesktopをWindowsに入れる
適当に公式ページからインストール

### https://www.docker.com/products/docker-desktop/

https://www.docker.com/products/docker-desktop/

## docker-composeをPower Shellから導入
なぜかデスクトップ用のDockerの入れてもdocker-composeが入っていなかったのでPowerSHellから導入。

```powershell:
Invoke-WebRequest "https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-windows-x86_64.exe"
-OutFile C:\Program Files\Docker\Docker
```

https://github.com/docker/compose/releases?expanded=true&page=1&q=2.4.3


## docker-composeでymlファイルを実行
``` docker-compose.yml
version: "3"
services:
  jupyter:
    build: .
    volumes:
      - "C:\\Users:/tmp/working"
    working_dir: /tmp/working
    ports:
      - "8888:8888"
    command: jupyter notebook --ip=0.0.0.0 --allow-root --no-browser


```

インストールログはこちら。

```
PS C:xxxxxxxxx python-kaggle-start-book-master\python-kaggle-start-book-master> docker-compose.exe up --build
time="2024-08-27T09:50:50+09:00" level=warning msg="C:xxxxxxxxxxxxxx python-kaggle-start-book-master\\python-kaggle-start-book-master\\docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion"
[+] Building 755.0s (6/6) FINISHED                                                                 docker:desktop-linux
 => [jupyter internal] load build definition from Dockerfile                                                       0.0s
 => => transferring dockerfile: 74B                                                                                0.0s
 => [jupyter internal] load metadata for gcr.io/kaggle-images/python:v68                                           1.9s
 => [jupyter internal] load .dockerignore                                                                          0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [jupyter 1/1] FROM gcr.io/kaggle-images/python:v68@sha256:7a54af952b186b6135e2e6876ee89fffc5e018383ce91f6fd  752.6s
 => => resolve gcr.io/kaggle-images/python:v68@sha256:7a54af952b186b6135e2e6876ee89fffc5e018383ce91f6fdb91a255f7b  0.0s
 => => sha256:c5e155d5a1d130a7f8a3e24cee0d9e1349bff13f90ec6a941478e558fde53c14 45.34MB / 45.34MB                  16.4s
 => => sha256:7a54af952b186b6135e2e6876ee89fffc5e018383ce91f6fdb91a255f7b658d2 7.07kB / 7.07kB                     0.0s
 => => sha256:38107095ca92aa4322ef8b6851dea976921a1f0b599276e617844659165ec726 33.02kB / 33.02kB                   0.0s
 => => sha256:5764e90b1fae3f6050c1b56958da5e94c0d0c2a5211955f579958fcbe6a679fd 1.57GB / 1.57GB                   481.9s
 => => sha256:86534c0d13b7196a49d52a65548f524b744d48ccaf89454659637bee4811d312 95.10MB / 95.10MB                  33.0s
 => => extracting sha256:c5e155d5a1d130a7f8a3e24cee0d9e1349bff13f90ec6a941478e558fde53c14                          2.0s
 => => sha256:ba67f7304613606a1d577e2fc5b1e6bb14b764bcc8d07021779173bcc6a8d4b6 1.08MB / 1.08MB                    18.4s
 => => sha256:10d355ad7c8c7ec86943c9bc6647bc77ef87a59c9901ddbf4338d328eacc21ad 213B / 213B                        18.8s
 => => sha256:16ad5b44117a2fca535f611053d375afbcd5ce91212ff21825ba9d5a383537ab 525B / 525B                        19.2s
 => => sha256:4def8145903fa388b44bdfe99e061e25c756e8a85071988230fbcd2d8406a5ce 457B / 457B                        19.8s
 => => sha256:b318a6db4c687ab828a06300402bcfda4df9df6e351c11e98ea145e5adeefab5 13.09MB / 13.09MB                  26.2s
 => => sha256:444bc9f8f83c3c6457e2257d84aeac9f47cfb211f407756cf1be27c650eeb265 571.84MB / 571.84MB               210.7s
 => => sha256:22b6483a5759b34af4ff11a0db4f5cb5fd227ce4a4023d34d5720c42f4c24858 953.23MB / 953.23MB               303.0s
 => => extracting sha256:86534c0d13b7196a49d52a65548f524b744d48ccaf89454659637bee4811d312                          3.2s
 => => sha256:3a247f79636f01846b77de9820a40c24cab3276724276c0204bf4f4eb6c964d2 79.10MB / 79.10MB                 236.3s
 => => sha256:a323e7c22c178c86d28e1a1f95a99f24409d612c2dfa363dcac945d69bfc3039 93.99MB / 93.99MB                 259.4s
 => => sha256:52ca83457f627d2bdb01a56ef4efb22644ec1527f6b19d5928df2ec2fec45c67 1.15GB / 1.15GB                   595.1s
 => => sha256:0caf9e9942fecefcb6629fbc5344fee90b08cd16c8ae3c86d68004975b3d3981 371.52MB / 371.52MB               406.6s
 => => sha256:061ec0743e8bc182ee50b953b0c3b3260fea72279e9786f85aae256b94cad51b 145.89MB / 145.89MB               458.2s
 => => sha256:4eb09dffe364526524f185f0ef0b6948a6f67e13a4e2c8a5c945f4d1aa4163af 63.25MB / 63.25MB                 489.2s
 => => extracting sha256:5764e90b1fae3f6050c1b56958da5e94c0d0c2a5211955f579958fcbe6a679fd                         50.3s
 => => sha256:c5282e066b17f86c7e037fdcab5300c8d9e19d94cc607956538def2b808b8ab1 372.31MB / 372.31MB               571.4s
 => => sha256:4d6b11a35c61823ec4e76573a77b67fb1cc55f24c0bedc3de02b36e7bbdaccc0 91.38MB / 91.38MB                 521.6s
 => => sha256:c4e7c70c716c9ab1c39604076d5193d4f3219b69918d3c02f29a096cbc51d931 115.24MB / 115.24MB               566.2s
 => => extracting sha256:ba67f7304613606a1d577e2fc5b1e6bb14b764bcc8d07021779173bcc6a8d4b6                          0.0s
 => => extracting sha256:10d355ad7c8c7ec86943c9bc6647bc77ef87a59c9901ddbf4338d328eacc21ad                          0.0s
 => => extracting sha256:16ad5b44117a2fca535f611053d375afbcd5ce91212ff21825ba9d5a383537ab                          0.0s
 => => extracting sha256:4def8145903fa388b44bdfe99e061e25c756e8a85071988230fbcd2d8406a5ce                          0.0s
 => => extracting sha256:b318a6db4c687ab828a06300402bcfda4df9df6e351c11e98ea145e5adeefab5                          0.1s
 => => extracting sha256:444bc9f8f83c3c6457e2257d84aeac9f47cfb211f407756cf1be27c650eeb265                         34.8s
 => => sha256:3610cd6de3daa0a7b04d0c696319600ab39517c639f991f6283ed81d0e9e8330 66.71MB / 66.71MB                 584.8s
 => => extracting sha256:22b6483a5759b34af4ff11a0db4f5cb5fd227ce4a4023d34d5720c42f4c24858                         10.0s
 => => sha256:81e49fa7885b9774361729aeba6926d10bf3241136a4deb2a00580bd5fd127cd 12.75MB / 12.75MB                 577.8s
 => => sha256:0cd90ca0bb5315533ddd29fc827df87f512db435ef77355f0a1d1fb82e36e359 611.52MB / 611.52MB               722.5s
 => => extracting sha256:3a247f79636f01846b77de9820a40c24cab3276724276c0204bf4f4eb6c964d2                          0.5s
 => => extracting sha256:a323e7c22c178c86d28e1a1f95a99f24409d612c2dfa363dcac945d69bfc3039                          4.1s
 => => sha256:8cdcc412f2bfc13af935863fd277409d191ff90cdb352b15ba0bd9f793773903 21.74MB / 21.74MB                 596.0s
 => => extracting sha256:52ca83457f627d2bdb01a56ef4efb22644ec1527f6b19d5928df2ec2fec45c67                         19.3s
 => => sha256:78a72602749e101fcfa99322c86a49f3d878ffb210d1fc16afa993a8ed8008f7 163.49MB / 163.49MB               628.1s
 => => sha256:6fc4ff181a1de96f828fdcaec501f8b27eb6c0875ce5320e63fc4353ec277df0 19.09kB / 19.09kB                 597.0s
 => => sha256:9268a2f3969f9a4701de9aabf05dde69168db6d6e25c817713f78a90b73a3de7 55.06MB / 55.06MB                 621.4s
 => => extracting sha256:0caf9e9942fecefcb6629fbc5344fee90b08cd16c8ae3c86d68004975b3d3981                          8.9s
 => => sha256:149e7ce48019565994388a04e5b3a28acf0e605e6588b44529e66568ea7514d4 3.08kB / 3.08kB                   622.9s
 => => sha256:b223eca929d16d722e56e8cce14583273a7a2c85ee9eae6a73704c9d0f68168c 2.08kB / 2.08kB                   624.4s
 => => extracting sha256:061ec0743e8bc182ee50b953b0c3b3260fea72279e9786f85aae256b94cad51b                          4.7s
 => => sha256:ff5df7baf16080ed5274ff3cec017443eecd891f5580d96562e4540eb4c18b97 2.05kB / 2.05kB                   625.9s
 => => sha256:2a5ec7dba5069c470f7392ac6afed0f3da405b09c7d323ae2cfdaca27b9468c1 873B / 873B                       627.4s
 => => sha256:5188298d15aaf83916628e388441cb845ef9c2f7e7a950d4468781e868d20990 73.99kB / 73.99kB                 629.0s
 => => sha256:ebbdeaaec044b6f4c48249297b5c2ae67e428bde25fc7502d7a36fb0ce1bd7a6 1.11kB / 1.11kB                   629.6s
 => => extracting sha256:4eb09dffe364526524f185f0ef0b6948a6f67e13a4e2c8a5c945f4d1aa4163af                          1.3s
 => => extracting sha256:c5282e066b17f86c7e037fdcab5300c8d9e19d94cc607956538def2b808b8ab1                         20.2s
 => => extracting sha256:4d6b11a35c61823ec4e76573a77b67fb1cc55f24c0bedc3de02b36e7bbdaccc0                          6.4s
 => => extracting sha256:c4e7c70c716c9ab1c39604076d5193d4f3219b69918d3c02f29a096cbc51d931                          3.5s
 => => extracting sha256:3610cd6de3daa0a7b04d0c696319600ab39517c639f991f6283ed81d0e9e8330                          2.2s
 => => extracting sha256:81e49fa7885b9774361729aeba6926d10bf3241136a4deb2a00580bd5fd127cd                          0.8s
 => => extracting sha256:0cd90ca0bb5315533ddd29fc827df87f512db435ef77355f0a1d1fb82e36e359                         11.8s
 => => extracting sha256:8cdcc412f2bfc13af935863fd277409d191ff90cdb352b15ba0bd9f793773903                          0.4s
 => => extracting sha256:78a72602749e101fcfa99322c86a49f3d878ffb210d1fc16afa993a8ed8008f7                          8.9s
 => => extracting sha256:6fc4ff181a1de96f828fdcaec501f8b27eb6c0875ce5320e63fc4353ec277df0                          0.0s
 => => extracting sha256:9268a2f3969f9a4701de9aabf05dde69168db6d6e25c817713f78a90b73a3de7                          6.9s
 => => extracting sha256:149e7ce48019565994388a04e5b3a28acf0e605e6588b44529e66568ea7514d4                          0.0s
 => => extracting sha256:b223eca929d16d722e56e8cce14583273a7a2c85ee9eae6a73704c9d0f68168c                          0.0s
 => => extracting sha256:ff5df7baf16080ed5274ff3cec017443eecd891f5580d96562e4540eb4c18b97                          0.0s
 => => extracting sha256:2a5ec7dba5069c470f7392ac6afed0f3da405b09c7d323ae2cfdaca27b9468c1                          0.0s
 => => extracting sha256:5188298d15aaf83916628e388441cb845ef9c2f7e7a950d4468781e868d20990                          0.0s
 => => extracting sha256:ebbdeaaec044b6f4c48249297b5c2ae67e428bde25fc7502d7a36fb0ce1bd7a6                          0.0s
 => [jupyter] exporting to image                                                                                   0.4s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:2c99a373a41fa4b2b24f07ebf89df08ff15e8b1852cf533cce86798b15a65a0e                       0.0s
 => => naming to docker.io/library/python-kaggle-start-book-master-jupyter                                         0.0s
 => [jupyter] resolving provenance for metadata file                                                               0.0s
[+] Running 2/2
 ✔ Network python-kaggle-start-book-master_default      Creat...                                                   0.0s
 ✔ Container python-kaggle-start-book-master-jupyter-1  C...                                                       0.8s
Attaching to jupyter-1
Gracefully stopping... (press Ctrl+C again to force)
[+] Stopping 1/0
 ✔ Container python-kaggle-start-book-master-jupyter-1  S...                                                       0.0s
Error response from daemon: driver failed programming external connectivity on endpoint python-kaggle-start-book-master-jupyter-1 (4fc62d234720dcb7f38318944b67440a7b9baff7748140a24e7c8b9e67dc9f72): Bind for 0.0.0.0:8888 failed: port is already allocated

```

docker-composeによるイメージのビルドが完了して、プロセスが立ち上がったようなので管理コンソールからコンテナを見てみましょうか。
↓　ありますね。新たに仮想化コンテナプロセスが作られています。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/381629/7a742851-e663-4cec-666b-f05a693ed148.png)

下記のDockerのコンソールから、ログを確認してjupyternotebookにアクセスしてみます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/381629/492863ea-6dc4-cb97-06a9-7b88cb050a6c.png)

ローカルホストの8888のTCPポートでアプリケーションプロセスとしてjupyternotebookが起動していますね！

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/381629/8cf1e21a-02aa-9476-f8d7-1758ebc6e38a.png)

改めてコンテナ仮想化は、従来のハイパーバイザー仮想型(ホストOS型)に比べて非常に軽量と実感しました。。