つばろぐ

主に C#, .NET, Azure の備忘録です。たまに日記。

Visual StudioでDocker Desktopの代わりにPodmanを使う

この記事に沿って、Windows に Podman をセットアップすればよい。

zenn.dev

セットアップ時に感じたことをメモしておく。

  • BUILD_KIT の無効化も行ったほうが安定している気がする
  • VS2022 Preview での特別な設定は不要だった
  • WSL2 の Ubuntu に Docker Engine をインストールした状態でも干渉しない

devcontainerのAzuriteにホスト側のAzure Storage Explorerからアクセスする

devcontainer で Azure Functions などの実装を行う際、Azure Storage のエミュレーターとして Azurite を使用します。

learn.microsoft.com

devcontainer の構成

docker-compose.yml

version: '3'

services:
  app:
    (アプリケーションの構成)

    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
    network_mode: service:storage

  storage:
    container_name: azurite
    image: mcr.microsoft.com/azure-storage/azurite:latest
    restart: unless-stopped
    volumes:
      - storage-data:/data

volumes:
  storage-data:

devcontainer.json

{
    "name": "Node.js & Azurite (Community)",
    "dockerComposeFile": "docker-compose.yml",
    "service": "app",
    "workspaceFolder": "/workspace",

    "customizations": {
        "vscode": {
            "extensions": [
                "ms-azuretools.vscode-azurestorage"
            ]
        }
    },

    // Azuriteが使用しているポートを転送する
    //   Blob: 10000
    //   Queue: 10001
    //   Table: 10002
    "forwardPorts": [10000, 10001, 10002],

    "remoteUser": "node"
}

Azure Storage Explorerから接続

Azure Storage Explorer から devcontainer の Azurite に接続する場合、Azurite の接続文字列を使用します。
ただし http の接続文字列を使用しましょう。 https では繋がりません。

learn.microsoft.com

参考

github.com

codingfor.beer

Azure CLIプロファイルの使い分けにはdevcontainerが便利

Azure CLI を開発環境で使用する場合、Windows や WSL2 の Ubuntu にインストールして使っていましたが、devcontainer にしちゃうのが便利だなと感じてきました。

learn.microsoft.com

Azure CLI devcontainer の作成

プロファイル用のディレクトリの作成

devcontainer 用のディレクトリを作成し、Visual Studio Code で開きます。

$ mkdir az-profile-development
$ code az-profile-development

devcontainer 定義の作成

Command Palette > Dev Containers: Add Dev Container Configuration Files... > Azure CLI を選びます。

.devcontainer/devcontainer.json はこうなります。

{
    "name": "Azure CLI",
    "dockerFile": "Dockerfile",
    
    // Configure tool-specific properties.
    "customizations": {
        // Configure properties specific to VS Code.
        "vscode": {     
            // Add the IDs of extensions you want installed when the container is created.
            "extensions": [
                "ms-vscode.azurecli"
            ]
        }
    },
    
    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],

    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "az --version",

    // Uncomment when using a ptrace-based debugger like C++, Go, and Rust
    // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

    // Set `remoteUser` to `root` to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "vscode"
}

コンテナーの起動

devcontainer 定義を作成すると Reopen in Container がポップアップされるため、クリックします。

az login の実行

コンテナーを起動したばかりの時は、Azure CLI はどのアカウントにもログインしていません。
VSCode のターミナルでコンテナー内のシェルを起動して、コマンドで確認します。

$ az account list -o table
Please run "az login" to access your accounts.

az login でアカウントへのログインを行います。

Azure CLI 環境の準備完了

Azure にログインしたあとは目的のサブスクリプションに接続し、CLI を実行していきます。


この方法であれば、ローカル環境に Azure CLI をインストールする必要がなくなります。
また、プロファイルごとにディレクトリと devcontainer 定義を作成すれば、アカウントの使い分けを行うこともできるのが便利です。


(追記)

Dockerイメージがプロファイルごとに増えるのはちょっと気になるけど、まぁ自分の場合は同時3つくらいなので許容範囲ということで。

※自分の環境では【1.99GB】のコンテナーイメージになりました