0
2

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 で Docker をインストールして MySQL実行(WSL2・Ubuntu・Docker・MySQL)

Last updated at Posted at 2023-03-03

Windows11 で Docker をインストールして MySQL 実行してみる
手順を大雑把に書くと以下の感じで進める

  1. PowerShell 7 インストール ※Windows PowerShell じゃない、次の処理でエラーでた...
  2. WSL2 インストール(Ubuntu v22.04 インストール)
  3. Docker インストール
  4. MySQL の Docker Compose 作成・実行・停止

PowerShell 7 インストール

基本的には以下の公式サイトを参考にインストール
https://learn.microsoft.com/ja-jp/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3

  1. Windows PowerShell を管理者で実行
    image.png

  2. インストールバージョン確認 (今回は 7.3.0.0 で実施してます)

    PowerShell
    winget search Microsoft.PowerShell
    
  3. インストール実行

    PowerShell
    winget install --id Microsoft.Powershell --source winget
    
  4. 念のため再起動

WSL2 インストール(Ubuntu v22.04インストール)

PowerShell でコマンド一つで終了!!
インストール後に再起動して Ubuntu の初期設定をする
参考としてMSの公式サイト: WSL を使用して Windows に Linux をインストールする

  1. PowerShell 7 を管理者で実行
    image.png

  2. インストール実行

    PowerShell
    wsl --install
    
  3. 再起動して Ubuntu の初期設定

    • ユーザ/パスワード:任意(なんでもOK, ユーザ/パスワードを忘れないように注意)
  4. バージョン確認 (今回は 22.04 で実施してます)

    Ubuntu
    cat /etc/os-release
    
  5. root ユーザに変更 (Windows の管理者実行みたいなやつ)
    パスワードは Ubuntu の初期設定で設定したものを使う

    Ubuntu
    sudo -su root
    
  6. アップデート実行 ※失敗が残った時は再起動して再実行

    Ubuntu
    # コマンドは二つ
    sudo apt update -y
    sudo apt upgrade -y
    
  7. 念のため再起動

Docker インストール

Ubunts に Docker, Docker Compose をインストールしていく

  1. Ubunts を管理者で実行
    ユーザ・パスワード入力があるときは Ubuntu の初期設定時に設定したものを使う
    image.png

  2. root ユーザに変更 (Windows の管理者実行みたいなやつ)
    パスワードは Ubuntu の初期設定で設定したものを使う

    Ubuntu
    sudo -su root
    
  3. DockerインストールのGCP鍵取得

    Ubuntu
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
  4. 安定版(stable)リポジトリをセットアップ

    Ubuntu
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
  5. パッケージ更新

    Ubuntu
    sudo apt update
    
  6. Dockerインストール実行

    Ubuntu
    sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    
  7. iptables, ip6tables 設定変更

    Ubuntu
    # コマンド2つ実行 
    sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
    sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
    
  8. root ユーザに変更 (Windows の管理者実行みたいなやつ)
    パスワードは Ubuntu の初期設定で設定したものを使う

    Ubuntu
    sudo -su root
    
  9. Docker Compose インストールディレクトリ作成

    Ubuntu
    sudo mkdir -p /usr/local/lib/docker/cli-plugins
    
  10. Docker Compose インストール実行

    Ubuntu
    sudo curl -SL https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
    
  11. シンボリックリンク設定

    Ubuntu
    sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
    sudo ln -s /usr/local/lib/docker/cli-plugins/docker-compose /usr/bin/docker-compose
    
  12. Docker, Docker Compose バージョン確認 (今回は Docker 23.0.1, Docker Compose 2.4.1 で実施してます)

    Ubuntu
    docker --version
    docker-compose --version
    

MySQL の Docker Compose 作成・実行・停止

Docker Compose の設定ファイルを作って実行、停止する

  1. Ubunts を管理者で実行
    ユーザ・パスワード入力があるときは Ubuntu の初期設定時に設定したものを使う
    image.png

  2. root ユーザに変更 (Windows の管理者実行みたいなやつ)
    パスワードは Ubuntu の初期設定で設定したものを使う

    Ubuntu
    sudo -su root
    
  3. Docker Compose を作るディレクトリを作成・移動(任意でどこでもOK)

    Ubuntu
    # 例はかなり適当... ディレクトリ名は慎重に!!!
    mkdir -p tekito1/tekito2/tekito3
    cd tekito1/tekito2/tekito3
    
  4. Docker Compose 設定ファイル作成(docker-compose.yml, .env の2つ)

    docker-compose.yml
    version: '3'
    
    services:
      db:
        image: mysql:8
        container_name: mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: ${ROOT_PASS}
          MYSQL_DATABASE: ${DB_NAME}
          MYSQL_USER: ${DB_USER}
          MYSQL_PASSWORD: ${DB_PASS}
          TZ: ${TZ}
        ports:
            - ${DB_PORT}:3306
        volumes:
            - db-store:/var/lib/mysql
            - ./conf/my.cnf:/etc/mysql/conf.d/my.cnf
    volumes:
      db-store:
    
    .env
    ROOT_PASS=root
    DB_NAME=WEB_APP_DB
    DB_USER=webapp
    DB_PASS=webapp
    DB_PORT=13306
    TZ=Asia/Tokyo
    
  5. Docker 起動

    Ubuntu
    service docker start
    
  6. Docker Compose 起動・停止 etc...

    Ubuntu
    # 起動
    docker-compose up -d
    
    # 停止
    docker-compose down
    
    # 起動プロセス確認
    docker-compose ps
    
    # 起動コンテナにログイン ( ${serviceName} はここだと db )
    docker-compose exec ${serviceName} /bin/bash
    
    

※ 参考 Docker の MySQL にアクセス(ローカルJavaなど)
port, user, password などを Docker Compose の設定に合わせる感じでコネクション生成

参考コネクション生成
    String dbUrl = "jdbc:mysql://localhost:13306/WEB_APP_DB";
    String dbUser = "webapp";
    String dbPassword = "webapp";
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);

    // 以降は SQL 実行などやってく感じ

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?