mcpField
C
containerize-aspnet-framework

containerize-aspnet-framework

github.com/githubクラウド・DevOps40.5k8.7k 回インストール今日更新
リポジトリ

containerize-aspnet-framework について

ASP.NET .NET Framework アプリを Windows Docker コンテナにするためのプロンプト型スキルです。プロジェクトの設定に沿って、Dockerfile や依存関係の構成、ビルド検証を進めます。

主な機能

  1. 01プロジェクト別の Windows コンテナ設定
  2. 02マルチステージ Dockerfile の作成
  3. 03環境変数からのアプリ設定読み込み
  4. 04GAC、MSI、COM 依存関係の処理
  5. 05Docker ignore とヘルスチェックの設定
  6. 06進捗管理とビルド検証

活用シーン

  1. 01旧版 ASP.NET プロジェクトの Windows コンテナ化
  2. 02IIS と Windows コンテナ依存関係の設定
  3. 03.NET Framework プロジェクトからのイメージ構築と検証

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/github/awesome-copilot --skill containerize-aspnet-framework

インストール後、Claude Code で/containerize-aspnet-frameworkを入力して読み込まれたか確認します。

詳細

ASP.NET .NET Framework コンテナ化プロンプト

以下に示すコンテナ化設定で指定された ASP.NET(.NET Framework)プロジェクトをコンテナ化します。アプリケーションを Windows Docker コンテナで実行するために必要な変更のみに焦点を当ててください。コンテナ化では、ここで指定されたすべての設定を考慮する必要があります。

注意: これは .NET Framework アプリケーションであり、.NET Core ではありません。コンテナ化のプロセスは .NET Core アプリケーションの場合とは異なります。

コンテナ化設定

プロンプトのこのセクションには、ASP.NET(.NET Framework)アプリケーションをコンテナ化するために必要な具体的な設定と構成が含まれています。このプロンプトを実行する前に、設定が必要な情報で入力されていることを確認してください。多くの場合、最初のいくつかの設定のみが必要であることに注意してください。後続の設定は、コンテナ化するプロジェクトに該当しない場合はデフォルトのままにしておくことができます。

指定されていない設定はすべてデフォルト値に設定されます。デフォルト値は [角括弧] 内に示されています。

プロジェクトの基本情報
  1. コンテナ化するプロジェクト:

    • [ProjectName (.csproj ファイルへのパスを指定)]
  2. 使用する Windows Server SKU:

    • [Windows Server Core(デフォルト)または Windows Server Full]
  3. 使用する Windows Server のバージョン:

    • [2022、2019、または 2016(デフォルトは 2022)]
  4. Docker イメージのビルドステージ用のカスタムベースイメージ(標準の Microsoft ベースイメージを使用する場合は「None」):

    • [ビルドステージで使用するベースイメージを指定(デフォルトは None)]
  5. Docker イメージの実行ステージ用のカスタムベースイメージ(標準の Microsoft ベースイメージを使用する場合は「None」):

    • [実行ステージで使用するベースイメージを指定(デフォルトは None)]
コンテナ構成
  1. コンテナイメージで公開する必要があるポート:

    • プライマリ HTTP ポート: [例: 80]
    • 追加ポート: [追加ポートをリストするか、「None」]
  2. コンテナを実行するユーザーアカウント:

    • [ユーザーアカウント、またはデフォルトの「ContainerUser」]
  3. コンテナイメージで構成する必要がある IIS 設定:

    • [特定の IIS 設定をリストするか、「None」]
ビルド構成
  1. コンテナイメージをビルドする前に実行する必要があるカスタムビルド手順:

    • [特定のビルド手順をリストするか、「None」]
  2. コンテナイメージをビルドした後に実行する必要があるカスタムビルド手順:

    • [特定のビルド手順をリストするか、「None」]
依存関係
  1. コンテナイメージの GAC に登録する必要がある .NET アセンブリ:

    • [アセンブリ名とバージョン、または「None」]
  2. コンテナイメージにコピーしてインストールする必要がある MSI:

    • [MSI 名とバージョン、または「None」]
  3. コンテナイメージに登録する必要がある COM コンポーネント:

    • [COM コンポーネント名、または「None」]
システム構成
  1. コンテナイメージに追加する必要があるレジストリキーと値:

    • [レジストリパスと値、または「None」]
  2. コンテナイメージに設定する必要がある環境変数:

    • [変数名と値、または「デフォルトを使用」]
  3. コンテナイメージにインストールする必要がある Windows Server の役割と機能:

    • [役割/機能名、または「None」]
ファイルシステム
  1. コンテナイメージにコピーする必要があるファイル/ディレクトリ:

    • [プロジェクトルートからの相対パス、または「None」]
    • コンテナ内のターゲット場所: [コンテナパス、または「該当なし」]
  2. コンテナ化から除外するファイル/ディレクトリ:

    • [除外するパス、または「None」]
.dockerignore の構成
  1. .dockerignore ファイルに含めるパターン(.dockerignore にはすでに一般的なデフォルトが含まれています。これらは追加のパターンです):
    • 追加パターン: [追加パターンをリストするか、「None」]
ヘルスチェックの構成
  1. ヘルスチェックエンドポイント:

    • [ヘルスチェック URL パス、または「None」]
  2. ヘルスチェックの間隔とタイムアウト:

    • [間隔とタイムアウトの値、または「デフォルトを使用」]
追加の手順
  1. プロジェクトをコンテナ化するために従う必要があるその他の手順:

    • [具体的な要件、または「なし」]
  2. 対処すべき既知の問題:

    • [既知の問題を記述するか、「なし」と記載してください]

スコープ

  • ✅ 環境変数からアプリ設定と接続文字列を読み取るために構成ビルダーが使用されるようにする、アプリ構成の変更
  • ✅ ASP.NET アプリケーション用の Dockerfile の作成と構成
  • ✅ アプリケーションをビルド/公開し、出力を最終イメージにコピーするための、Dockerfile における複数ステージの指定
  • ✅ Windows コンテナープラットフォーム互換性(Windows Server Core または Full)の構成
  • ✅ 依存関係(GAC アセンブリ、MSI、COM コンポーネント)の適切な処理
  • ❌ インフラストラクチャのセットアップは行いません(別途対応される想定です)
  • ❌ コンテナ化に必要なもの以外のコード変更は行いません

実行プロセス

  1. 上記のコンテナ化設定を確認し、コンテナ化の要件を理解します
  2. チェックマークを付けて変更を追跡するための progress.md ファイルを作成します
  3. プロジェクトの .csproj ファイルで TargetFrameworkVersion 要素を確認し、.NET Framework のバージョンを判別します
  4. 以下に基づいて適切な Windows Server コンテナーイメージを選択します:
  5. 必要な NuGet パッケージがインストールされていることを確認します。インストールされていない場合は、これらを インストールしないでください。インストールされていない場合、ユーザーが手動でインストールする必要があります。インストールされていない場合は、このプロンプトの実行を一時停止し、Visual Studio の NuGet パッケージマネージャーまたは Visual Studio パッケージマネージャーコンソールを使用してインストールするようユーザーに依頼してください。以下のパッケージが必要です:
    • Microsoft.Configuration.ConfigurationBuilders.Environment
  6. web.config ファイルを変更し、構成ビルダーのセクションと設定を追加して、環境変数からアプリ設定と接続文字列を読み取るようにします:
    • configSections に ConfigBuilders セクションを追加する
    • ルートに configBuilders セクションを追加する
    • appSettings と connectionStrings の両方に対して EnvironmentConfigBuilder を構成する
    • パターン例:
      <configSections>
        <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
      </configSections>
      <configBuilders>
        <builders>
          <add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
        </builders>
      </configBuilders>
      <appSettings configBuilders="Environment">
        
      </appSettings>
      <connectionStrings configBuilders="Environment">
        
      </connectionStrings>
      
  7. このプロンプトの末尾にある参照用の LogMonitorConfig.json ファイルをコピーして、Dockerfileを作成するフォルダーに LogMonitorConfig.json ファイルを作成します。コンテナー化の設定で別途指示されていない限り、ファイルの内容は変更せず、参照用の内容と完全に一致させる必要があります。
    • 特に、ログに記録する問題のレベルを変更しないでください。EventLog ソースに Information レベルを使用すると、不要なノイズが発生します。
  8. アプリケーションをコンテナー化するために、プロジェクトディレクトリのルートにDockerfileを作成します。
    • Dockerfileでは、複数のステージを使用する必要があります。
      • ビルドステージ:Windows Server Core イメージを使用してアプリケーションをビルドします。
        • 設定ファイルでカスタムのベースイメージが指定されていない限り、ビルドステージでは mcr.microsoft.com/dotnet/framework/sdk のベースイメージを使用する必要があります。
        • 最初に sln、csproj、packages.config ファイルをコピーします。
        • NuGet.config がある場合はコピーし、プライベートフィードを設定します。
        • NuGet パッケージを復元します。
        • 次に、残りのソースコードをコピーし、MSBuild を使用してアプリケーションをビルドし、C:\publish に発行します。
      • 最終ステージ:選択した Windows Server イメージを使用してアプリケーションを実行します。
        • 設定ファイルでカスタムのベースイメージが指定されていない限り、最終ステージでは mcr.microsoft.com/dotnet/framework/aspnet のベースイメージを使用する必要があります。
        • LogMonitorConfig.json ファイルをコンテナー内のディレクトリ(例:C:\LogMonitor)にコピーします。
        • Microsoft リポジトリから同じディレクトリに LogMonitor.exe をダウンロードします。
        • 作業ディレクトリを C:\inetpub\wwwroot に設定します。
        • ビルドステージで発行した出力(C:\publish 内)を最終イメージにコピーします。
        • IIS サービスを監視するため、ServiceMonitor.exe とともに LogMonitor.exe を実行するようにコンテナーのエントリーポイントを設定します。
          • ENTRYPOINT [ "C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc" ]
    • コンテナー化の設定にあるすべての要件を考慮してください。
      • Windows Server の SKU とバージョン
      • 公開ポート
      • コンテナーで使用するユーザーアカウント
      • IIS の設定
      • GAC アセンブリの登録
      • MSI のインストール
      • COM コンポーネントの登録
      • レジストリキー
      • 環境変数
      • Windows の役割と機能
      • ファイル/ディレクトリのコピー
    • このプロンプトの末尾にある例を参考にDockerfileを作成します。ただし、対象プロジェクト固有の要件と設定に合わせてカスタマイズしてください。
    • 重要: 設定ファイルでユーザーが完全な Windows Server イメージを明示的に要求している場合を除き、Windows Server Core のベースイメージを使用します。
  9. Docker イメージに不要なファイルが含まれないように、プロジェクトディレクトリのルートに .dockerignore ファイルを作成します。.dockerignore ファイルには、コンテナー化の設定で指定された追加パターンとともに、少なくとも次の要素を含める必要があります。
    • packages/
    • bin/
    • obj/
    • .dockerignore
    • Dockerfile
    • .git/
    • .github/
    • .vs/
    • .vscode/
    • **/node_modules/
    • *.user
    • *.suo
    • **/.DS_Store
    • **/Thumbs.db
    • コンテナー化の設定で指定された追加パターン
  10. 設定で指定されている場合は、ヘルスチェックを構成します。
  • ヘルスチェックのエンドポイントが指定されている場合は、Dockerfile に HEALTHCHECK 命令を追加します。
  1. プロジェクトファイルに次の項目を追加して、Dockerfile をプロジェクトに追加します:<None Include="Dockerfile" />
  2. タスクを完了としてマークします:[ ] → [✓]
  3. すべてのタスクが完了し、Docker ビルドが成功するまで続行します

ビルドとランタイムの検証

Dockerfile が完成したら、Docker ビルドが成功することを確認します。次のコマンドを使用して Docker イメージをビルドします。

docker build -t aspnet-app:latest .

ビルドが失敗した場合は、エラーメッセージを確認し、Dockerfile またはプロジェクト設定に必要な調整を加えてください。成功/失敗を報告します。

進捗の追跡

次の構造で progress.md ファイルを維持します。

# Containerization Progress

## Environment Detection
- [ ] .NET Framework version detection (version: ___)
- [ ] Windows Server SKU selection (SKU: ___)
- [ ] Windows Server version selection (Version: ___)

## Configuration Changes
- [ ] Web.config modifications for configuration builders
- [ ] NuGet package source configuration (if applicable)
- [ ] Copy LogMonitorConfig.json and adjust if required by settings

## Containerization
- [ ] Dockerfile creation
- [ ] .dockerignore file creation
- [ ] Build stage created with SDK image
- [ ] sln, csproj, packages.config, and (if applicable) NuGet.config copied for package restore
- [ ] Runtime stage created with runtime image
- [ ] Non-root user configuration
- [ ] Dependency handling (GAC, MSI, COM, registry, additional files, etc.)
- [ ] Health check configuration (if applicable)
- [ ] Special requirements implementation

## Verification
- [ ] Review containerization settings and make sure that all requirements are met
- [ ] Docker build success

ステップ間で確認のために停止しないでください。アプリケーションがコンテナ化され、Docker ビルドが成功するまで、方法論的に続けてください。

すべてのチェックボックスにチェックが入るまで、あなたは完了していません! これには、Docker イメージのビルドを正常に完了すること、およびビルドプロセス中に発生するあらゆる問題に対処することが含まれます。

参考資料

Dockerfile の例

Windows Server Core ベースイメージを使用した ASP.NET (.NET Framework) アプリケーション用の Dockerfile の例です。

# escape=`
# The escape directive changes the escape character from \ to `
# This is especially useful in Windows Dockerfiles where \ is the path separator

# ============================================================
# Stage 1: Build and publish the application
# ============================================================

# Base Image - Select the appropriate .NET Framework version and Windows Server Core version
# Possible tags include:
# - 4.8.1-windowsservercore-ltsc2025 (Windows Server 2025)
# - 4.8-windowsservercore-ltsc2022 (Windows Server 2022)
# - 4.8-windowsservercore-ltsc2019 (Windows Server 2019)
# - 4.8-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7.2-windowsservercore-ltsc2019 (Windows Server 2019)
# - 4.7.2-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7.1-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.6.2-windowsservercore-ltsc2016 (Windows Server 2016)
# - 3.5-windowsservercore-ltsc2025 (Windows Server 2025)
# - 3.5-windowsservercore-ltsc2022 (Windows Server 2022)
# - 3.5-windowsservercore-ltsc2019 (Windows Server 2019)
# - 3.5-windowsservercore-ltsc2019 (Windows Server 2016)
# Uses the .NET Framework SDK image for building the application
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2022 AS build
ARG BUILD_CONFIGURATION=Release

# Set the default shell to PowerShell
SHELL ["powershell", "-command"]

WORKDIR /app

# Copy the solution and project files
COPY YourSolution.sln .
COPY YourProject/*.csproj ./YourProject/
COPY YourOtherProject/*.csproj ./YourOtherProject/

# Copy packages.config files
COPY YourProject/packages.config ./YourProject/
COPY YourOtherProject/packages.config ./YourOtherProject/

# Restore NuGet packages
RUN nuget restore YourSolution.sln

# Copy source code
COPY . .

# Perform custom pre-build steps here, if needed

# Build and publish the application to C:\publish
RUN msbuild /p:Configuration=$BUILD_CONFIGURATION `
            /p:WebPublishMethod=FileSystem `
            /p:PublishUrl=C:\publish `
            /p:DeployDefaultTarget=WebPublish

# Perform custom post-build steps here, if needed

# ============================================================
# Stage 2: Final runtime image
# ============================================================

# Base Image - Select the appropriate .NET Framework version and Windows Server Core version
# Possible tags include:
# - 4.8.1-windowsservercore-ltsc2025 (Windows Server 2025)
# - 4.8-windowsservercore-ltsc2022 (Windows Server 2022)
# - 4.8-windowsservercore-ltsc2019 (Windows Server 2019)
# - 4.8-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7.2-windowsservercore-ltsc2019 (Windows Server 2019)
# - 4.7.2-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7.1-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.7-windowsservercore-ltsc2016 (Windows Server 2016)
# - 4.6.2-windowsservercore-ltsc2016 (Windows Server 2016)
# - 3.5-windowsservercore-ltsc2025 (Windows Server 2025)
# - 3.5-windowsservercore-ltsc2022 (Windows Server 2022)
# - 3.5-windowsservercore-ltsc2019 (Windows Server 2019)
# - 3.5-windowsservercore-ltsc2019 (Windows Server 2016)
# Uses the .NET Framework ASP.NET image for running the application
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2022

# Set the default shell to PowerShell
SHELL ["powershell", "-command"]

WORKDIR /inetpub/wwwroot

# Copy from build stage
COPY --from=build /publish .

# Add any additional environment variables needed for your application (uncomment and modify as needed)
# ENV KEY=VALUE

# Install MSI packages (uncomment and modify as needed)
# COPY ./msi-installers C:/Installers
# RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'

# Install custom Windows Server roles and features (uncomment and modify as needed)
# RUN dism /Online /Enable-Feature /FeatureName:YOUR-FEATURE-NAME

# Add additional Windows features (uncomment and modify as needed)
# RUN Add-WindowsFeature Some-Windows-Feature; `
#    Add-WindowsFeature Another-Windows-Feature

# Install MSI packages if needed (uncomment and modify as needed)
# COPY ./msi-installers C:/Installers
# RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'

# Register assemblies in GAC if needed (uncomment and modify as needed)
# COPY ./assemblies C:/Assemblies
# RUN C:\Windows\Microsoft.NET\Framework64\v4.0.30319\gacutil -i C:/Assemblies/YourAssembly.dll

# Register COM components if needed (uncomment and modify as needed)
# COPY ./com-components C:/Components
# RUN regsvr32 /s C:/Components/YourComponent.dll

# Add registry keys if needed (uncomment and modify as needed)
# RUN New-Item -Path 'HKLM:\Software\YourApp' -Force; `
#     Set-ItemProperty -Path 'HKLM:\Software\YourApp' -Name 'Setting' -Value 'Value'

# Configure IIS settings if needed (uncomment and modify as needed)
# RUN Import-Module WebAdministration; `
#     Set-ItemProperty 'IIS:\AppPools\DefaultAppPool' -Name somePropertyName -Value 'SomePropertyValue'; `
#     Set-ItemProperty 'IIS:\Sites\Default Web Site' -Name anotherPropertyName -Value 'AnotherPropertyValue'

# Expose necessary ports - By default, IIS uses port 80
EXPOSE 80
# EXPOSE 443  # Uncomment if using HTTPS

# Copy LogMonitor from the microsoft/windows-container-tools repository
WORKDIR /LogMonitor
RUN curl -fSLo LogMonitor.exe https://github.com/microsoft/windows-container-tools/releases/download/v2.1.1/LogMonitor.exe

# Copy LogMonitorConfig.json from local files
COPY LogMonitorConfig.json .

# Set non-administrator user
USER ContainerUser

# Override the container's default entry point to take advantage of the LogMonitor
ENTRYPOINT [ "C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc" ]

この例の適応

注記: コンテナ化設定の具体的な要件に基づいて、このテンプレートをカスタマイズしてください。

この例の Dockerfile を適応する際は、次のようにします。

  1. YourSolution.sln、YourProject.csproj などを実際のファイル名に置き換えます
  2. 必要に応じて Windows Server と .NET Framework のバージョンを調整します
  3. 要件に基づいて依存関係のインストール手順を変更し、不要なものは削除します
  4. 特定のワークフローに応じて、必要に応じてステージを追加または削除します

ステージ命名に関する注記

  • AS stage-name 構文は、各ステージに名前を付けます
  • --from=stage-name を使用して、前のステージからファイルをコピーします
  • 最終イメージで使用されない複数の中間ステージを持つことができます
LogMonitorConfig.json

LogMonitorConfig.json ファイルは、プロジェクトディレクトリのルートに作成する必要があります。これは、コンテナ内のログを監視する LogMonitor ツールを設定するために使用されます。このファイルの内容は、適切なログ機能を確保するために、正確に次のようになっている必要があります。

{
  "LogConfig": {
    "sources": [
      {
        "type": "EventLog",
        "startAtOldestRecord": true,
        "eventFormatMultiLine": false,
        "channels": [
          {
            "name": "system",
            "level": "Warning"
          },
          {
            "name": "application",
            "level": "Error"
          }
        ]
      },
      {
        "type": "File",
        "directory": "c:\\inetpub\\logs",
        "filter": "*.log",
        "includeSubdirectories": true,
        "includeFileNames": false
      },
      {
        "type": "ETW",
        "eventFormatMultiLine": false,
        "providers": [
          {
            "providerName": "IIS: WWW Server",
            "providerGuid": "3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83",
            "level": "Information"
          },
          {
            "providerName": "Microsoft-Windows-IIS-Logging",
            "providerGuid": "7E8AD27F-B271-4EA2-A783-A47BDE29143B",
            "level": "Information"
          }
        ]
      }
    ]
  }
}

コメント