# Rancher Desktop インストーラー URL https://rancherdesktop.io/ ## インストール 特に変更することもなく次へを連打して完了。 ## 初回セットアップ - Enable Kubernetes のチェックを外す - Container Engine は dockerd (moby) を選択する - ![](https://hackmd.io/_uploads/ryXekaMth.png) - 初期化が済んだらセットアップ完了 - ![](https://hackmd.io/_uploads/SyEncKbY2.png) ## もろもろの確認 Powershell で WSL の一覧を確認し、以下が追加されたことを確認。 - `rancher-desktop-data` - `rancher-desktop` ``` PS C:\Users\tmymd> wsl -l -v NAME STATE VERSION * Ubuntu Running 2 rancher-desktop-data Stopped 2 rancher-desktop Running 2 docker-desktop-data Stopped 2 ``` ### ネットワーク接続周りの確認 Rancher Desktop でコンテナのポート公開が利用できるかどうか、nginx コンテナを用いて確認する。 はじめに、PowerShell 上から docker run コマンドで nginx コンテナを実行し、 http://localhost:80 にアクセスできることを確認する。 > `docker run -it --name test --rm -p 80:80/tcp nginx` ``` PS C:\Users\tmymd> docker run -it --name test --rm -p 80:80/tcp nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx (省略) Digest: sha256:08bc36ad52474e528cc1ea3426b5e3f4bad8a130318e3140d6cfe29c8892c7ef Status: Downloaded newer image for nginx:latest (省略) 2023/07/05 10:02:35 [notice] 1#1: nginx/1.25.1 2023/07/05 10:02:35 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 2023/07/05 10:02:35 [notice] 1#1: OS: Linux 5.15.90.1-microsoft-standard-WSL2 2023/07/05 10:02:35 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2023/07/05 10:02:35 [notice] 1#1: start worker processes (省略) ``` 上記を実行状態でブラウザを開き、localhost に接続し、nginx にアクセスできていることを確認。 ![](https://hackmd.io/_uploads/SJ4GlpMt2.png) 続いて、別の Powershell ウィンドウから Linux の Shell に移動し、内部 IP を確認。 > `inet 172.20.169.34/20` ``` PS C:\Users\tmymd> wsl -d rancher-desktop /mnt/c/Users/tmymd # ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:15:5d:47:34:a6 brd ff:ff:ff:ff:ff:ff inet 172.20.169.34/20 brd 172.20.175.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::215:5dff:fe47:34a6/64 scope link valid_lft forever preferred_lft forever ``` localhost ではなく、内部 IP を直接指定してアクセスできることを確認。 ![](https://hackmd.io/_uploads/rkGmM6fK2.png) 以下のように、2通りの通信方法でアクセスできるが、localhost:80 を指す通信については、WSL 側の設定により透過的に WSL 側に接続が転送されている。 なお、WSL 側に割り当てられた `172.20.169.34` という IP については動的割り当てであるため、再起動などで変更になる可能性がある。 ```mermaid graph LR subgraph windows[Windows PC] subgraph wsl[WSL] subgraph rancher_desktop[rancher-desktop] ip_rancher_desktop(fas:fa-network-wired 172.20.169.34) nginx([nginx コンテナ]) end end ps[PowerShell >] ===>|docker run -it --name test --rm -p 80:80/tcp nginx|nginx chrome[ブラウザ] -.->|http://localhost:80|nginx chrome --->|http://172.20.169.34:80|nginx end style nginx fill:#43A047,color:#FFF,stroke:#fff,stroke-width:1px style chrome fill:#fff,color:#222,stroke:#222,stroke-width:1px style ps fill:#222,color:#ddd,stroke:#222,stroke-width:1px style ip_rancher_desktop fill:transparent,stroke:transparent ``` PowerShell で netsh コマンドを実行し、現在のポート転送設定を確認すると、:80 ポートへの接続が 172.20.169.34:80 に転送されていることを確認できる。 ``` PS C:\Users\tmymd> netsh interface portproxy show all ipv4 をリッスンする: ipv4 に接続する: Address Port Address Port --------------- ---------- --------------- ---------- 0.0.0.0 80 172.20.169.34 80 ipv6 をリッスンする: ipv6 に接続する: Address Port Address Port --------------- ---------- --------------- ---------- :: 80 fe80::215:5dff:fe47:34a6 80 ``` 最後に、起動している nginx コンテナを削除する。 ``` PS C:\Users\tmymd> docker rm -f $(docker ps -aq) 0128f7944dc5 ``` # postgres コンテナの実行 続いて、postgres コンテナを実行して Windows 側の DBeaver からアクセスできることを確認する。 ```mermaid graph LR subgraph windows[Windows PC] subgraph wsl[WSL] subgraph rancher_desktop[rancher-desktop] ip_rancher_desktop(fas:fa-network-wired 172.20.169.34) postgres([postgres コンテナ]) end end ps[PowerShell >] ===>|docker run ... -p 5432:5432 ... -d postgres:14.5|postgres dbeaver[DBeaver] -.->|localhost:5432|postgres dbeaver --->|172.20.169.34:5432|postgres end style postgres fill:#0064a5,color:#FFF,stroke:#008bb9,stroke-width:1px style dbeaver fill:#8E8580,color:#E5DFDC,stroke:#E38B73,stroke-width:1px style ps fill:#222,color:#ddd,stroke:#222,stroke-width:1px style ip_rancher_desktop fill:transparent,stroke:transparent ``` Powershell 上から postgres コンテナを実行してみる。 - postgres ユーザのパスワード: `testdb_password` - 初回起動時に作成する DB: `testdb` ``` docker run -itd --name testdb -e POSTGRES_PASSWORD=testdb_password -e POSTGRES_DB=testdb -p 5432:5432 postgres:14.5 ``` 実行結果: ``` Unable to find image 'postgres:14.5' locally 14.5: Pulling from library/postgres e9995326b091: Pull complete a0cb03f17886: Pull complete bb26f7e78134: Pull complete c8e073b7ae91: Pull complete 99b5b1679915: Pull complete 55c520fc03c5: Pull complete d0ac84d6672c: Pull complete 4effb95d5849: Pull complete f1605d32a2b6: Pull complete 45587aaa5f27: Pull complete 27c45a07ef3f: Pull complete 2b6113fb2a7b: Pull complete 144293d36fee: Pull complete Digest: sha256:135c62a8134dcef829a1e4f5568bfae44bcfa2c75659ff948f43c71964366aa4 Status: Downloaded newer image for postgres:14.5 592e3447947f0fe3254ae8c5d6b15aef75f6a46d9e4eb70dde6bfdf42be2c474 ``` ログを見たところ、起動している。 > `2023-07-05 11:06:58.683 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432` ``` PS C:\Users\tmymd> docker logs testdb The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /var/lib/postgresql/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Etc/UTC creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /var/lib/postgresql/data -l logfile start waiting for server to start....2023-07-05 11:06:58.356 UTC [48] LOG: starting PostgreSQL 14.5 (Debian 14.5-2.pgdg110+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit 2023-07-05 11:06:58.358 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2023-07-05 11:06:58.362 UTC [49] LOG: database system was shut down at 2023-07-05 11:06:58 UTC 2023-07-05 11:06:58.365 UTC [48] LOG: database system is ready to accept connections done server started CREATE DATABASE /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* 2023-07-05 11:06:58.570 UTC [48] LOG: received fast shutdown request waiting for server to shut down....2023-07-05 11:06:58.572 UTC [48] LOG: aborting any active transactions 2023-07-05 11:06:58.572 UTC [48] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1 2023-07-05 11:06:58.572 UTC [50] LOG: shutting down 2023-07-05 11:06:58.582 UTC [48] LOG: database system is shut down done server stopped PostgreSQL init process complete; ready for start up. 2023-07-05 11:06:58.683 UTC [1] LOG: starting PostgreSQL 14.5 (Debian 14.5-2.pgdg110+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit 2023-07-05 11:06:58.683 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 2023-07-05 11:06:58.683 UTC [1] LOG: listening on IPv6 address "::", port 5432 2023-07-05 11:06:58.686 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2023-07-05 11:06:58.690 UTC [62] LOG: database system was shut down at 2023-07-05 11:06:58 UTC 2023-07-05 11:06:58.692 UTC [1] LOG: database system is ready to accept connections ``` ## 接続の確認 ![](https://hackmd.io/_uploads/r1sG-0MK3.png =700x) - Host: `localhost` - Database: `testdb` - パスワード: `testdb_password` ![](https://hackmd.io/_uploads/Sk6OWRzF3.png =700x) 接続テスト: 成功 ![](https://hackmd.io/_uploads/S1ToW0zth.png =500x) localhost で testdb への接続を確認できた。 ![](https://hackmd.io/_uploads/BycCWAft2.png =350x) 続いて、接続設定を変更してみる。 ![](https://hackmd.io/_uploads/HJtVzAzt2.png =700x) 問題なく接続できた。 ![](https://hackmd.io/_uploads/ryz8MRfFn.png =350x) PowerShell 上からポート転送の設定も確認したところ、以下の設定に自動的に変更されていた。 ``` PS C:\Users\tmymd> netsh interface portproxy show all ipv4 をリッスンする: ipv4 に接続する: Address Port Address Port --------------- ---------- --------------- ---------- 0.0.0.0 5432 172.20.169.34 5432 ipv6 をリッスンする: ipv6 に接続する: Address Port Address Port --------------- ---------- --------------- ---------- :: 5432 fe80::215:5dff:fe47:34a6 5432 ```