0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows11でWSL+vscode+Docker環境を整備した時のメモ 2024-05

Last updated at Posted at 2024-05-07

概要

  • 2024-05ごろに Windows11環境で vscodeを使って WSL(ubuntu)+dockerで何かしらの開発作業を行える環境をざっくり整備した時のメモ
  • PCを新調して まっさらのWindows11Proに開発環境を1から構築する機会があったが、Windows環境に触るのが久しぶりすぎて構築方法が浦島太郎状態だったので改めて調べた
  • 最近はマイクロソフトが丁寧なドキュメントを揃えて提供してくれている事を認識した

とりあえず実現したかった事

  • WSL(ubuntu)を使える状態にしたい
  • vscodeを使ってWSLファイルシステム上の環境で開発できる状態にしたい
  • Docker desktopなしで WSLだけでDocker使いたい
  • WSLでnginxコンテナを起動し WindowsのChromeからhttp://localhost:8080にアクセスしてWelcomeページを見られる状態にしたい

作業開始時の状態

  • PCを買って来てWindows初期セットアップ完了し、ネットにつないでWindowsアップデートを最新まで適用、Chromeをインストールして標準ブラウザに設定してからYoutubeを少し見ながら休憩した。くらいの状態から

WSLの導入ガイドが用意されている

WSL インストール

  • https://learn.microsoft.com/ja-jp/windows/wsl/setup/ この辺に従う
  • 管理者モードPowerShellからコマンド打てば完了
    wsl --install
    
  • インストール処理後に Windowsを一度再起動すれば WSLを使えるようになった
  • wsl --list --online コマンドでインストール可能なディストリビューション一覧を見られるのは知らなかったな
    wsl --list --online
    インストールできる有効なディストリビューションの一覧を次に示します。
    'wsl.exe --install <Distro>' を使用してインストールします。
    
    NAME                                   FRIENDLY NAME
    Ubuntu                                 Ubuntu
    Debian                                 Debian GNU/Linux
    kali-linux                             Kali Linux Rolling
    Ubuntu-18.04                           Ubuntu 18.04 LTS
    Ubuntu-20.04                           Ubuntu 20.04 LTS
    Ubuntu-22.04                           Ubuntu 22.04 LTS
    Ubuntu-24.04                           Ubuntu 24.04 LTS
    OracleLinux_7_9                        Oracle Linux 7.9
    OracleLinux_8_7                        Oracle Linux 8.7
    OracleLinux_9_1                        Oracle Linux 9.1
    openSUSE-Leap-15.5                     openSUSE Leap 15.5
    SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
    SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
    openSUSE-Tumbleweed                    openSUSE Tumbleweed
    

Ubuntu初期設定

https://learn.microsoft.com/ja-jp/windows/wsl/setup/environment#set-up-your-linux-username-and-password に従う

  • user名とPasswordを設定
  • ここで設定したPasswordはsudoなどでも使うので厳重管理する

Ubuntu上でaptパッケージ最新化

  • sudoするので先に設定したPasswordを聞かれる
    sudo apt update
    sudo apt-get install wget ca-certificates
    sudo apt upgrade
    

vscode と WSL拡張をインストール

vscode

WSL拡張を含むRemote Development拡張の導入

WSLからvscode を起動

ここまでで WSLとvscodeを連動できるようになったので続けてDokcer環境を整備していく

WSLにDocker CEを導入

  • https://docs.docker.com/engine/install/ubuntu/ 公式を見た
  • https://docs.docker.com/engine/install/ubuntu/#installation-methods こちらに従い作業
    • aptリポジトリの設定
      # Add Docker's official GPG key:
      sudo apt-get update
      sudo apt-get install ca-certificates curl
      sudo install -m 0755 -d /etc/apt/keyrings
      sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
      sudo chmod a+r /etc/apt/keyrings/docker.asc
      
      # Add the repository to Apt sources:
      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
        $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      sudo apt-get update
    
    • Dockerパッケージのインストール
     sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
    • Service起動とServiceが起動しているかの確認
    sudo service docker start
    
    sudo service docker status
    
    • hello-world
    $ sudo docker run hello-world
    
      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/
    

(必要に応じて)WSLのユーザがsudoしなくてもDockerを操作できるようにする

  • sudoしなくてもDockerを操作できるようにdockerグループにWSLユーザを追加
    sudo usermod -aG docker ${USER}
    
  • コマンドの実行後、一度WSLのターミナルからログアウトして再ログインすると sudo せずにDockerコマンドを使えるようになる
    • idコマンドで dockerグループに属していることを確認できるはず
    $ id
    uid=1002(user01) gid=1002(user01) groups=1002(user01),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),100(users),107(netdev),989(docker)
    

WSL上でnginxコンテナを起動して WindowsのChromeから確認する

  • WSL上でポートマッピングしてnginx コンテナを起動してみる
    docker run -p 8080:80
    
  • WindowsのChromeから http://localhost:8080へアクセスすると Welcomeページを確認できた
  • image.png

そのほか

いくつかアプリ更新と追加導入をした

PowerShell 7

winget

Windows Terminal

  • SSHなど使いたい場合はTeraTermなどのフリーアプリを自分で用意していたが、最近はMicrosoft純正のターミナルアプリが用意されているらしいのを知ったので導入した
  • Microsoft Storeからインストールすればアップデート等の適用も自動化されるので安心
  • タブ別にWSLだったりPwerShellだったりを指定できて結構よさそうな雰囲気

Oh My Posh による Windows Terminal と WSL プロンプトのカスタマイズ

WSL on ubuntuにbrewを導入

  • https://brew.sh/https://docs.brew.sh/Homebrew-on-Linux あたりを見てインストール
    • 前提ツールインストール
      sudo apt-get install build-essential procps curl file git
      
    • インストールスクリプトの実行
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
    • PATHの設定
      test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
      test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
      echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bashrc
      
    • 動作確認
      brew install hello
      

devcontainer


以上でWSL環境を使って開発作業できる環境をざっくり用意できた。
調べながらだったので疲れたけど いろいろ新しくなっているのを感じられて楽しかった

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?