LoginSignup
0
0

More than 1 year has passed since last update.

WindowsにUbuntu環境を入れてDockerをインストールしてみる

Last updated at Posted at 2022-08-24

Docker Desktop for Windowsが有料となったので、WindowsのLinuxでDockerをインストールして使用出来るようにします。

WSLの有効化

WindowsでLinuxを使用出来るように設定を行います。
管理者権限でPowerShellを開き、下記コマンドを実行します。

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

スクリーンショット 2022-08-17 201047.png

仮想マシン プラットフォームを有効化します。

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

スクリーンショット 2022-08-17 201145.png

上記のコマンドで有効化出来ない場合、コントロールパネル→関連設定 プログラムと機能→Windowsの機能の有効化または有効化で表示される画面で「Linux用 Windows サブシステム」と「仮想マシンプラットフォーム」をチェックします。
image.png

Windowsを再起動してLinux有効化は完了です。

Ubuntuのインストール

デスクトップメニューの「MicrosoftStore」若しくは下記URLをクリックします。
https://aka.ms/wslstore

スクリーンショット 2022-08-15 143628.png

検索欄にUbuntuを入力し、左の「インストール」をクリックしてインストールを進めます。
完了するとUbuntuの入力画面が表示されます。
image.png

image.png

image.png

インストール完了後、Ubuntuのユーザー名を入力します。
スクリーンショット 2022-08-15 144525.png

パスワードを入力します。
スクリーンショット 2022-08-15 144557.png

スクリーンショット 2022-08-15 144716.png

これで設定完了です。

Dockerインストール

先程のUbuntu画面でコマンドを実行していきます。

sudo su

image.png

パスワードを再度入力します。
image.png

コマンド解説

・sudo su Windowsでいう管理者(rootユーザー)に切り替え

apt update
パッケージを更新します。
image.png

apt install apt-transport-https ca-certificates curl software-properties-common
Do you want to continue? [Y/n]が出たら「Y」を入力してEnterを押します。
image.png

コマンド解説

・apt パッケージ管理コマンドでDebian系ディストリビューションで使用

・apt-transport-https  aptコマンドはhttpsに対応させるのに必要なパッケージ

・ca-certificates  CA証明書を扱うパッケージ。通常curl コマンドで HTTPS 通信しようとするとエラーが発生

・curl URLを用いてファイルを送信または受信するコマンドラインツール

・software-properties-common aptリポジトリを抽象化して扱いやすくするパッケージ(実行コマンドを減らす目的)

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
image.png

コマンド解説

・curl -fsSL エラー時の挙動時を分かりやすくし、通信失敗するとリトライ

・apt-key add - aptからUbuntu公式以外からパッケージを取得する際に認証する鍵情報を取り込んでおく

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Press [ENTER] to continue or Ctrl-c to cancel.が出たらEnterを入力
image.png

コマンド解説

・add-apt-repository パッケージを新たに追加

apt update
image.png

apt-cache policy docker-ce

image.png

コマンド解説

・apt-cache policy 特定のバージョンをインストールした際のバージョン確認

apt install docker-ce
Do you want to continue? [Y/n]が出たら「Y」を入力してEnterを押します。
image.png

これでDockerインストール完了です。

Ubuntuのバージョン更新

今の状態でDockerを起動させると以下エラーが発生してしまいます。
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
image.png

解消するにはUbuntuのバージョンを更新しなければなりません。
Ubuntuを起動した状態で管理者権限のPowerShellを起動します。

wsl --list --verbose

image.png

UbuntuのVERSIONが1のため2に変更します。

wsl --set-version Ubuntu 2
image.png

wslを終了します。

wsl --shutdown

終了後Ubuntu画面では下記の状態になるので、Enterを押して終了します。
image.png

これでUbuntuのバージョン更新完了です。

Docker起動・確認

再度スタートメニューのUbuntuを起動させて以下コマンドを入力します。

sudo su
パスワードを入力します。
image.png

service docker start
service docker status

Docker起動コマンドを実行しますが、失敗します。
image.png

dockerd
iptableでエラーが発生していますが、これはwslのバグです。

image.png

以下コマンド実行後、1を入力して実行します。
update-alternatives --config iptables

image.png

再度Ubuntuを開き直して、下記コマンドを実行すると正常稼働が確認できます。
sudo su
service docker start
service docker status

image.png

docker version
バージョンも確認出来ます。
image.png

これでDocker実行は以上となります。

WindowsからUbuntuへのファイルアクセス

エクスプローラーのパスに以下を入力すると、WindowsからUbuntuのストレージにアクセス出来ます。

\\wsl$

image.png

image.png

image.png

余談 dockerdの実行

dockerdを実行すると起動に失敗します。

image.png

解決策として以下を実行します。
rm /var/run/docker.pid
dockerd
image.png

再度エラーが発生しているので下記を実行します。
ps -aux | grep dockerd
kill -9 該当ID
image.png

dockerd
実行を確認。
image.png

参考サイト

https://www.aise.ics.saitama-u.ac.jp/~gotoh/HowToEnableWSL.html#toc2
https://vividcode.hatenablog.com/entry/linux/ubuntu-ca-certificates
https://mirahouse.jp/n10/blog/2015/apt-transport-https/
https://hy4yh.hatenablog.com/entry/2018/12/06/183841
https://note.com/npaka/n/n58ce1fac14ef
https://thr3a.hatenablog.com/entry/20191110/1573353587
https://patrickwu.space/2021/03/09/wsl-solution-to-native-docker-daemon-not-starting/
https://github.com/docker/for-linux/issues/517

これで以上となります。
見て頂きありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0