WindowsでLinuxを使いたい、さらに、Dockerも使いたい
背景
目標
環境
- エディション:Windows 10 Home
- バージョン:1803
- 実行日:2019/07/15
手順
以下リンク先を参考に実施 qiita.com
Windows Subsystem for Linux(WSL)でUbuntuを使う
Windows Subsystem for Linux (Ubuntu)のセットアップ
WSLの機能を有効化
- スタートメニューの「設定」から「アプリと機能」を開く
- 下の方へスクロールして「関連設定」の「プログラムと機能」を開く
- 「Windowsの機能の有効化または無効化」を開く
- 「Windows Subsystem for Linux」にチェックを入れる
- WSLのインストールが始まるので、終了したら「今すぐ再起動」をクリックして再起動する
特に問題なく完了
Ubuntu 16.04のインストール
- 再起動後、スタートメニューから「Microsoft Store」を選択し、検索ボックスに「Ubuntu」を入力して「Ubuntu 16.04」を選択する
- 「入手」ボタンをクリックして(ダウンロード&)インストールする
- インストールが完了すると、初期設定としてユーザー名とパスワードを聞いてくるので、Linux(Ubuntu)上で利用するユーザー名とパスワードを入力する。
- 初期設定が完了すると、Ubuntu on WSLのコマンドプロンプト(ターミナル)が表示される。
特に問題なく完了
UbuntuでDockerを使う
Docker のセットアップ
- 「Ubuntu on WSL」を管理者として起動する
- 下記コマンドを順に実行する
sudo apt update sudo apt upgrade sudo apt install docker.io sudo cgroupfs-mount sudo usermod -aG docker $USER sudo service docker start
- 「exit」で終了
- 「Ubuntu on WSL」を管理者として起動する
- 下記コマンドを実行する
- 2回目以降は、下記操作でDockerデーモンを起動する
sudo cgroupfs-mount && sudo service docker start
- 下記コマンドを実行するが、起動しない
docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. See 'docker run --help'.
- 下記ブログをもとに下記コマンドを実行する qiita.com
sudo cgroupfs-mount && sudo service docker start
* Starting Docker: docker [ OK ]
sudo service docker status
* Docker is running
Dockerが起動していなかったっぽい。何度も起動とステータス確認を行うとそのうち起動した。原因は不明だが、先に進めることにする。
- 下記コマンドを実行するが、起動しない
docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Extracting [==================================================>] 977B/977B docker: failed to register layer: Error processing tar file(exit status 1): remount /, flags: 0x84000: invalid argument. See 'docker run --help'.
- 下記ブログをもとに下記コマンドを実行する qiita.com
docker version
Client: Version: 18.09.7 API version: 1.39 Go version: go1.10.4 Git commit: 2d0083d Built: Wed Jul 3 13:38:22 2019 OS/Arch: linux/amd64 Experimental: false Server: Engine: Version: 18.09.7 API version: 1.39 (minimum version 1.12) Go version: go1.10.4 Git commit: 2d0083d Built: Mon Jul 1 19:31:53 2019 OS/Arch: linux/amd64 Experimental: false
Dockerのバージョンが18.09.7となっており、WSLでは17.09.0までが対象となるらしい。やり直し・・・
- 下記ブログをもとに下記コマンドを実行する
sudo apt-get purge docker.io
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: bridge-utils cgroupfs-mount containerd libfreetype6 pigz runc ubuntu-fan Use 'sudo apt autoremove' to remove them. The following packages will be REMOVED: docker.io* 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 155 MB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 26095 files and directories currently installed.) Removing docker.io (18.09.7-0ubuntu1~16.04.4) ... '/usr/share/docker.io/contrib/nuke-graph-directory.sh' -> '/var/lib/docker/nuke-graph-directory.sh' invoke-rc.d: could not determine current runlevel * Stopping Docker: docker [ OK ] Purging configuration files for docker.io (18.09.7-0ubuntu1~16.04.4) ... Nuking /var/lib/docker ... (if this is wrong, press Ctrl+C NOW!) + sleep 10 + rm -rf /var/lib/docker/builder /var/lib/docker/buildkit /var/lib/docker/containerd /var/lib/docker/containers /var/lib/docker/image /var/lib/docker/network /var/lib/docker/nuke-graph-directory.sh /var/lib/docker/overlay2 /var/lib/docker/plugins /var/lib/docker/runtimes /var/lib/docker/swarm /var/lib/docker/tmp /var/lib/docker/trust /var/lib/docker/volumes dpkg: warning: while removing docker.io, directory '/etc/docker' not empty so not removed Processing triggers for man-db (2.7.5-1) ...
- 下記ブログをもとに再度Dockerインストールを実行する
sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB] Get:3 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB] Get:4 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [989 kB] Get:5 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB] Get:6 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [448 kB] Get:7 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [182 kB] Get:8 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [756 kB] Get:9 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [315 kB] Fetched 3,015 kB in 20s (147 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done All packages are up to date.
※libltdl7をインストールしないとDockerインストールでエラーになる
sudo apt-get install libltdl7
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: bridge-utils containerd libfreetype6 pigz runc ubuntu-fan Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: libltdl7 0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded. 1 not fully installed or removed. Need to get 38.3 kB of archives. After this operation, 85.0 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libltdl7 amd64 2.4.6-0.1 [38.3 kB] Fetched 38.3 kB in 1s (31.1 kB/s) Selecting previously unselected package libltdl7:amd64. (Reading database ... 26096 files and directories currently installed.) Preparing to unpack .../libltdl7_2.4.6-0.1_amd64.deb ... Unpacking libltdl7:amd64 (2.4.6-0.1) ... Processing triggers for libc-bin (2.23-0ubuntu11) ... Setting up libltdl7:amd64 (2.4.6-0.1) ... Setting up docker-ce (17.09.0~ce-0~ubuntu) ... invoke-rc.d: could not determine current runlevel Processing triggers for libc-bin (2.23-0ubuntu11) ... Processing triggers for systemd (229-4ubuntu21.22) ... Processing triggers for ureadahead (0.100.0-19.1) ...
sudo dpkg -i docker-ce_17.09.0~ce-0~ubuntu_amd64.deb
(Reading database ... 26103 files and directories currently installed.) Preparing to unpack docker-ce_17.09.0~ce-0~ubuntu_amd64.deb ... invoke-rc.d: could not determine current runlevel * Docker already stopped - file /var/run/docker-ssd.pid not found. Unpacking docker-ce (17.09.0~ce-0~ubuntu) over (17.09.0~ce-0~ubuntu) ... Setting up docker-ce (17.09.0~ce-0~ubuntu) ... invoke-rc.d: could not determine current runlevel Processing triggers for systemd (229-4ubuntu21.22) ... Processing triggers for ureadahead (0.100.0-19.1) ... Processing triggers for man-db (2.7.5-1) ...
docker version
Client: Version: 17.09.0-ce API version: 1.32 Go version: go1.8.3 Git commit: afdb6d4 Built: Tue Sep 26 22:42:18 2017 OS/Arch: linux/amd64 Server: Version: 17.09.0-ce API version: 1.32 (minimum version 1.12) Go version: go1.8.3 Git commit: afdb6d4 Built: Tue Sep 26 22:40:56 2017 OS/Arch: linux/amd64 Experimental: false
Dockerのバージョンが17.09.0になっていることを確認
- 下記コマンドでDockerを起動する
sudo cgroupfs-mount && sudo service docker start sudo service docker status docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
無事に「Hello from Docker!」が表示された。
今後
UbuntuやDockerのバージョンが古いため、WSLでの実施は微妙かもしれないと手順を実施しながら思えてきた。が、ここまで実施したので、やれるところまではこの環境でいろいろ勉強していこうと思う。