mcpField
C
containerize-aspnet-framework

containerize-aspnet-framework

github.com/github云与 DevOps40.5k8.7k 次安装1 天前更新
代码仓库

关于 containerize-aspnet-framework

用于将 ASP.NET .NET Framework 项目容器化为 Windows Docker 容器的提示词技能,面向维护旧版 ASP.NET 应用的开发者。它按项目设置指导 Dockerfile、依赖配置和构建验证。

主要功能

  1. 01按项目配置 Windows 容器
  2. 02创建多阶段 Dockerfile
  3. 03从环境变量读取应用设置
  4. 04处理 GAC、MSI 和 COM 依赖
  5. 05配置 Docker 忽略文件与健康检查
  6. 06跟踪进度并验证构建

适用场景

  1. 01为旧版 ASP.NET 项目准备 Windows 容器
  2. 02配置 IIS 和 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 镜像构建阶段的自定义基础镜像("None" 表示使用标准 Microsoft 基础镜像):

    • [指定用于构建阶段的基础镜像(默认 None)]
  5. Docker 镜像运行阶段的自定义基础镜像("None" 表示使用标准 Microsoft 基础镜像):

    • [指定用于运行阶段的基础镜像(默认 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. 必须在容器镜像中设置的环境变量:

    • [变量名称和值,或 "Use defaults"]
  3. 必须在容器镜像中安装的 Windows Server 角色和功能:

    • [角色/功能名称,或 "None"]
文件系统
  1. 需要复制到容器镜像的文件/目录:

    • [相对于项目根的路径,或 "None"]
    • 容器中的目标位置:[容器路径,或 "Not applicable"]
  2. 要从容器化中排除的文件/目录:

    • [要排除的路径,或 "None"]
.dockerignore 配置
  1. 要包含在 .dockerignore 文件中的模式(.dockerignore 已包含常见默认值;这些是附加模式):
    • 附加模式:[列出任何附加模式,或 "None"]
健康检查配置
  1. 健康检查终结点:

    • [健康检查 URL 路径,或 "None"]
  2. 健康检查间隔和超时:

    • [间隔和超时值,或 "Use defaults"]
其他说明
  1. 容器化项目时必须遵循的其他说明:

    • [具体需求,或“无”]
  2. 需要解决的已知问题:

    • [描述任何已知问题,或“无”]

范围

  • ✅ 修改应用配置,确保使用配置生成器从环境变量中读取应用设置和连接字符串
  • ✅ 为 ASP.NET 应用程序创建和配置 Dockerfile
  • ✅ 在 Dockerfile 中指定多个阶段,以构建/发布应用程序并将输出复制到最终镜像
  • ✅ 配置 Windows 容器平台兼容性(Windows Server Core 或 Full)
  • ✅ 正确处理依赖项(GAC 程序集、MSI、COM 组件)
  • ❌ 不进行基础设施设置(假定单独处理)
  • ❌ 除容器化所需之外,不进行任何代码更改

执行流程

  1. 查看上面的容器化设置,以了解容器化要求
  2. 创建一个 progress.md 文件,用勾选标记跟踪更改
  3. 通过检查 TargetFrameworkVersion 元素,从项目的 .csproj 文件确定 .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. 在将创建 Dockerfile 的文件夹中创建一个 LogMonitorConfig.json 文件,方法是复制本提示词末尾的参考 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)复制到最终镜像
        • 将容器的入口点设置为运行 LogMonitor.exe 并使用 ServiceMonitor.exe 监视 IIS 服务
          • ENTRYPOINT [ "C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc" ]
    • 务必考虑容器化设置中的所有要求:
      • Windows Server SKU 和版本
      • 暴露的端口
      • 容器的用户账户
      • IIS 设置
      • GAC 程序集注册
      • MSI 安装
      • COM 组件注册
      • 注册表项
      • 环境变量
      • Windows 角色和功能
      • 文件/目录复制
    • 以本提示词末尾提供的示例为模板来编写 Dockerfile,但要确保根据具体项目要求和设置对其进行定制。
    • 重要: 请使用 Windows Server Core 基础镜像,除非用户在设置文件中 特别要求 完整的 Windows Server 镜像
  9. 在项目目录的根目录中创建 .dockerignore 文件,以从 Docker 镜像中排除不必要的文件。.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"
          }
        ]
      }
    ]
  }
}

评论

支持 Markdown