DEV Community

birdsea
birdsea

Posted on • Edited on

2 1

Windows Subsystem for Linux 2でDocker for WSL2を使う

Subsystem for Linux 2(WSL2)はWindowsからLinuxが使える機能です。

2020年4月にリリースされるWindows10 2004から提供される予定です。
もともと2017年にリリースされたWindows 10 Fall Creators UpdateでWSLが提供されてましたが、そちらは本物のLinuxカーネルを使っておらず、実行できるシステムコールが限られてました。

2は本物のLinuxカーネルが動作します。
Dockerも使えるようになります。
これにより今後はWindowsでもLinuxを使用して開発が行いやすくなると思います。


WSL2のインストール

WSL 2 のインストール手順をもとに実施

WSL2に必要な環境条件は以下

  • Windows 10 builds 18917 or higher

2020/03/20時点でWindows10 builds 18917はWindows Insider Program(slow or fast)に加入しないとアップデートできない。

  1. Windows Insider Programに加入する

    Windowsメニューから設定 > 更新とセキュリティ > Windows Insider Programより加入する。
    Windows_Insider_Program_1
    Windows_Insider_Program_2
    Windows_Insider_Program_3
    Windows_Insider_Program_4
    Windows_Insider_Program_5
    Windows_Insider_Program_6
    [スロー]か[ファスト]を選択
    Windows_Insider_Program_7
    Windows_Insider_Program_8
    Windows_Insider_Program_9

  2. Windows Updateを実施する

  3. WSL2を有効にする

    設定 > アプリ > アプリと機能 > オプションから「Windowsのその他の機能」リンクをクリックする。
    Activate WSL2_1
    Activate WSL2_2
    以下のチェックをONにする。
    - Linux 用 Windows サブシステム
    - 仮想マシン プラットフォーム
    Activate WSL2_3Activate WSL2_4

    PowerShellを管理者権限で起動し、以下を実行しても実施できる。

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

    その後、再起動する。

  4. Microsoft StoreからLinuxをインストールする

    後述のDocker Desktop for WSL 2が使えるのはUbuntu 18.04 LTSのみの模様。(2020/03/20時点 Storeには「Ubuntu」もあるが、そちらは対応していない)

  5. Ubuntu 18.04 LTSをダウンロードしたら起動する

    起動後にインストールが始まり、ユーザ名とパスワードを入力し、ユーザを作成する。

  6. PowerShellを起動する

    # インストールしたディストリビューションの一覧を表示する
    > wsl -l
    Linux 用 Windows サブシステム ディストリビューション:
    Ubuntu-18.04 (既定)
    
    # WSLの状態とバージョンを確認する(VERSIONが1はWSLということです)
    > wsl -l -v
      NAME            STATE           VERSION
    * Ubuntu-18.04    Stopped         1
    
    # Ubuntu-18.04をWSL2用に変換する
    > wsl --set-version Ubuntu-18.04 2
    
    # WSL2を標準設定とする
    > wsl --set-default-version 2
    WSL 2 を実行するには、カーネル コンポーネントの更新が必要です。詳細については https://aka.ms/wsl2kernel を参照してください。
    

    上記のメッセージの通り、WSL 2 Linux カーネルの更新へアクセスし、更新プログラムをインストールする。インストール後、以下を実行する。

    # WSL2を標準設定とする
    > wsl --set-default-version 2
    
    # WSLの状態とバージョンを確認する
    > wsl -l -v
      NAME            STATE           VERSION
    * Ubuntu-18.04    Stopped         2
    
  7. Ubuntuを起動/停止する

    # 起動する
    > wsl -d Ubuntu-18.04
    To run a command as administrator (user "root"), use "sudo <command>".
    See "man sudo_root" for details.
    
    # フォルダ階層は以下になる
    $ /home/user/ ← 登録したLinuxユーザのhome
      /mnt/c ← WindowsのCドライブ
      /mnt/d ← WindowsのDドライブ
    
    # Windowsに戻る
    $ exit
    

これでWindowsからLinuxが使えるようなりました。

WSLはWindowsからシームレスにシェルを使え、VirtualBoxやVMWareのようにOSが起動する時間を待つ必要もありません。


Docker Desktop for Windowsをインストールする

  1. Docker Desktop for Windowsをインストールする

  2. メニューバーにDockerのアイコンが表示されるので、[Settings]を選択する
    Docker_Desktop_for_Windows_1
    Docker_Desktop_for_Windows_2

  3. Settings画面が表示されるので、以下を確認する

    • Dockerのステータスがrunningになっていること
    • Use the WSL 2 based engineのチェックボックスがONになっていること Docker_Desktop_for_Windows_3

    また、Resources > WSL INTEGRATIONを選択し、Enable integration with my default WSL distroのチェックボックスがONになっていることを確認する
    Docker_Desktop_for_Windows_4

  4. DockerがWSL2 based engineで起動されていれば、Dockerコマンドを実施すると、上記でインストールしたWSL2のUbuntuにDockerコンテナが作成されるようになる

    PorwerShellを起動し、Dockerコマンドを実行することができる。
    Ubuntuのほうでもdockerコマンドの実行ができる。

    # powershellでdockerコマンドを実行
    > docker run hello-world
    
    ...
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    > docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    0629bbd84b50        hello-world         "/hello"            22 seconds ago      Exited (0) 20 seconds ago                       mystifying_almeida
    
    # WSL2に切り替える
    > wsl -d Ubuntu-18.04
    
    # Ubuntuでdockerコマンドを実行
    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    0629bbd84b50        hello-world         "/hello"            22 seconds ago      Exited (0) 20 seconds ago                       mystifying_almeida
    

    WindowsとUbuntuでコンテナが共有されていることが確認できる。


Docker Desktop for WSL 2を使うと、このようにWindowsからDockerコマンドを実行して、LinuxのDockerコンテナを操作することができます。


2020/04/11追記
DockerComposeは、PowerShellからは実行できないようです。(実行してもLinuxのコンテナとならない)。Linuxのほうで実行すればWindowsと共有できそうです。

2020/05/11追記
Docker Desktop For Windowsの(Edge版:バーション2.3.0.1)で、WindowsからDockerComposeできるようになりました。

こちらのバージョンではWindows10 Home Editionでも動作するようです。
Docker Desktop for Winodwsのバージョンアップに伴い本記事の内容を修正しました。

2021/03/07追記
Dockerのバージョンアップに伴いStable版に記述を修正しました。

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay