containerize-aspnetcore by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --skill containerize-aspnetcore将下方设置中指定的 ASP.NET Core (.NET) 项目容器化,仅专注于应用程序在 Linux Docker 容器中运行所需的更改。容器化应考虑此处指定的所有设置。
遵循容器化 .NET Core 应用程序的最佳实践,确保容器在性能、安全性和可维护性方面得到优化。
提示的这一部分包含容器化 ASP.NET Core 应用程序所需的具体设置和配置。在运行此提示之前,请确保设置中已填写必要的信息。请注意,在许多情况下,只需要前几个设置。如果后面的设置不适用于正在容器化的项目,可以保留为默认值。
任何未指定的设置都将设置为默认值。默认值在 [方括号] 中提供。
[项目名称(提供 .csproj 文件的路径)][8.0 或 9.0 (默认 8.0)][debian, alpine, ubuntu, chiseled, 或 Azure Linux (mariner) (默认 debian)]广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
[指定用于构建阶段的基础镜像 (默认 None)][指定用于运行阶段的基础镜像 (默认 None)][例如,8080][列出任何其他端口,或 "None"][用户账户,或默认为 "$APP_UID"][指定 ASPNETCORE_URLS,或默认为 "http://+:8080"][列出任何特定的构建步骤,或 "None"][列出任何特定的构建步骤,或 "None"][列出任何带有身份验证信息的私有 NuGet 源,或 "None"][所选 Linux 发行版的包名称,或 "None"][库名称和路径,或 "None"][工具名称和版本,或 "None"][变量名称和值,或 "使用默认值"][相对于项目根目录的路径,或 "None"][容器路径,或 "不适用"][要排除的路径,或 "None"][用于持久化数据的卷路径,或 "None"].dockerignore 文件中的模式(.dockerignore 文件已包含常见的默认模式;这些是额外的模式):
[列出任何额外模式,或 "None"][健康检查 URL 路径,或 "None"][间隔和超时值,或 "使用默认值"][特定要求,或 "None"][描述任何已知问题,或 "None"]progress.md 文件,用勾选标记跟踪更改TargetFramework 元素,从项目的 .csproj 文件确定 .NET 版本$APP_UID))
$APP_UID 变量指定用户账户。.dockerignore 文件,以从 Docker 镜像中排除不必要的文件。.dockerignore 文件必须至少包含以下元素以及容器化设置中指定的额外模式:
确认 Dockerfile 完成后 Docker 构建成功。使用以下命令构建 Docker 镜像:
docker build -t aspnetcore-app:latest .
如果构建失败,请查看错误消息并对 Dockerfile 或项目配置进行必要的调整。报告成功/失败。
维护一个具有以下结构的 progress.md 文件:
# 容器化进度
## 环境检测
- [ ] .NET 版本检测 (版本: ___)
- [ ] Linux 发行版选择 (发行版: ___)
## 配置更改
- [ ] 应用程序配置验证以支持环境变量
- [ ] NuGet 包源配置(如果适用)
## 容器化
- [ ] Dockerfile 创建
- [ ] .dockerignore 文件创建
- [ ] 使用 SDK 镜像创建构建阶段
- [ ] 复制 csproj 文件以进行包还原
- [ ] 复制 NuGet.config(如果适用)
- [ ] 使用运行时镜像创建运行时阶段
- [ ] 非 root 用户配置
- [ ] 依赖项处理(系统包、原生库、工具等)
- [ ] 健康检查配置(如果适用)
- [ ] 特殊要求实现
## 验证
- [ ] 审查容器化设置并确保满足所有要求
- [ ] Docker 构建成功
不要在步骤之间暂停等待确认。有条不紊地继续,直到应用程序完成容器化且 Docker 构建成功。
在所有复选框被标记之前,您的工作尚未完成! 这包括成功构建 Docker 镜像并解决构建过程中出现的任何问题。
使用 Linux 基础镜像的 ASP.NET Core (.NET) 应用程序的示例 Dockerfile。
# ============================================================
# 阶段 1: 构建并发布应用程序
# ============================================================
# 基础镜像 - 选择适当的 .NET SDK 版本和 Linux 发行版
# 可能的标签包括:
# - 8.0-bookworm-slim (Debian 12)
# - 8.0-noble (Ubuntu 24.04)
# - 8.0-alpine (Alpine Linux)
# - 9.0-bookworm-slim (Debian 12)
# - 9.0-noble (Ubuntu 24.04)
# - 9.0-alpine (Alpine Linux)
# 使用 .NET SDK 镜像构建应用程序
FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# 首先复制项目文件以获得更好的缓存
COPY ["YourProject/YourProject.csproj", "YourProject/"]
COPY ["YourOtherProject/YourOtherProject.csproj", "YourOtherProject/"]
# 如果存在,则复制 NuGet 配置
COPY ["NuGet.config", "."]
# 还原 NuGet 包
RUN dotnet restore "YourProject/YourProject.csproj"
# 复制源代码
COPY . .
# 如果需要,在此处执行自定义的构建前步骤
# RUN echo "正在运行构建前步骤..."
# 构建并发布应用程序
WORKDIR "/src/YourProject"
RUN dotnet build "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/build
# 发布应用程序
RUN dotnet publish "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# 如果需要,在此处执行自定义的构建后步骤
# RUN echo "正在运行构建后步骤..."
# ============================================================
# 阶段 2: 最终运行时镜像
# ============================================================
# 基础镜像 - 选择适当的 .NET 运行时版本和 Linux 发行版
# 可能的标签包括:
# - 8.0-bookworm-slim (Debian 12)
# - 8.0-noble (Ubuntu 24.04)
# - 8.0-alpine (Alpine Linux)
# - 8.0-noble-chiseled (Ubuntu 24.04 Chiseled)
# - 8.0-azurelinux3.0 (Azure Linux)
# - 9.0-bookworm-slim (Debian 12)
# - 9.0-noble (Ubuntu 24.04)
# - 9.0-alpine (Alpine Linux)
# - 9.0-noble-chiseled (Ubuntu 24.04 Chiseled)
# - 9.0-azurelinux3.0 (Azure Linux)
# 使用 .NET 运行时镜像运行应用程序
FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim AS final
# 如果需要,安装系统包(取消注释并根据需要修改)
# RUN apt-get update && apt-get install -y \
# curl \
# wget \
# ca-certificates \
# libgdiplus \
# && rm -rf /var/lib/apt/lists/*
# 如果需要,安装额外的 .NET 工具(取消注释并根据需要修改)
# RUN dotnet tool install --global dotnet-ef --version 8.0.0
# ENV PATH="$PATH:/root/.dotnet/tools"
WORKDIR /app
# 从构建阶段复制已发布的应用程序
COPY --from=build /app/publish .
# 如果需要,复制额外的文件(取消注释并根据需要修改)
# COPY ./config/appsettings.Production.json .
# COPY ./certificates/ ./certificates/
# 设置环境变量
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:8080
# 如果需要,添加自定义环境变量(取消注释并根据需要修改)
# ENV CONNECTIONSTRINGS__DEFAULTCONNECTION="your-connection-string"
# ENV FEATURE_FLAG_ENABLED=true
# 如果需要,配置 SSL/TLS 证书(取消注释并根据需要修改)
# ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/app/certificates/app.pfx
# ENV ASPNETCORE_Kestrel__Certificates__Default__Password=your_password
# 暴露应用程序监听的端口
EXPOSE 8080
# EXPOSE 8081 # 如果使用 HTTPS,请取消注释
# 如果尚未安装,则安装 curl 用于健康检查
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# 配置健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# 如果需要,为持久化数据创建卷(取消注释并根据需要修改)
# VOLUME ["/app/data", "/app/logs"]
# 切换到非 root 用户以提高安全性
USER $APP_UID
# 设置应用程序的入口点
ENTRYPOINT ["dotnet", "YourProject.dll"]
注意: 根据容器化设置中的具体要求自定义此模板。
调整此示例 Dockerfile 时:
YourProject.csproj、YourProject.dll 等替换为您的实际项目名称为了获得更小的镜像大小,您可以使用 Alpine Linux:
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
# ... 构建步骤 ...
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
# 使用 apk 安装包
RUN apk update && apk add --no-cache curl ca-certificates
为了最小化攻击面,请考虑使用 chiseled 镜像:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled AS final
# 注意:Chiseled 镜像包含最少的包,因此您可能需要使用不同的基础镜像来安装额外的依赖项
对于 Azure 优化的容器:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-azurelinux3.0 AS final
# 使用 tdnf 安装包
RUN tdnf update -y && tdnf install -y curl ca-certificates && tdnf clean all
AS stage-name 语法为每个阶段命名--from=stage-name 从先前阶段复制文件final 阶段是成为最终容器镜像的阶段latest每周安装
7.3K
仓库
GitHub 星标
26.9K
首次出现
2026年2月25日
安全审计
安装于
codex7.2K
gemini-cli7.2K
opencode7.2K
cursor7.2K
github-copilot7.2K
kimi-cli7.2K
Containerize the ASP.NET Core (.NET) project specified in the settings below, focusing exclusively on changes required for the application to run in a Linux Docker container. Containerization should consider all settings specified here.
Abide by best practices for containerizing .NET Core applications, ensuring that the container is optimized for performance, security, and maintainability.
This section of the prompt contains the specific settings and configurations required for containerizing the ASP.NET Core application. Prior to running this prompt, ensure that the settings are filled out with the necessary information. Note that in many cases, only the first few settings are required. Later settings can be left as defaults if they do not apply to the project being containerized.
Any settings that are not specified will be set to default values. The default values are provided in [square brackets].
Project to containerize:
[ProjectName (provide path to .csproj file)].NET version to use:
[8.0 or 9.0 (Default 8.0)]Linux distribution to use:
[debian, alpine, ubuntu, chiseled, or Azure Linux (mariner) (Default debian)]Custom base image for the build stage of the Docker image ("None" to use standard Microsoft base image):
[Specify base image to use for build stage (Default None)]Custom base image for the run stage of the Docker image ("None" to use standard Microsoft base image):
[Specify base image to use for run stage (Default None)]Ports that must be exposed in the container image:
[e.g., 8080][List any additional ports, or "None"]User account the container should run as:
[User account, or default to "$APP_UID"]Application URL configuration:
[Specify ASPNETCORE_URLS, or default to "http://+:8080"]Custom build steps that must be performed before building the container image:
[List any specific build steps, or "None"]Custom build steps that must be performed after building the container image:
[List any specific build steps, or "None"]NuGet package sources that must be configured:
[List any private NuGet feeds with authentication details, or "None"]System packages that must be installed in the container image:
[Package names for the chosen Linux distribution, or "None"]Native libraries that must be copied to the container image:
[Library names and paths, or "None"]Additional .NET tools that must be installed:
[Tool names and versions, or "None"][Variable names and values, or "Use defaults"]Files/directories that need to be copied to the container image:
[Paths relative to project root, or "None"][Container paths, or "Not applicable"]Files/directories to exclude from containerization:
[Paths to exclude, or "None"]Volume mount points that should be configured:
[Volume paths for persistent data, or "None"].dockerignore file (.dockerignore will already have common defaults; these are additional patterns):
[List any additional patterns, or "None"]Health check endpoint:
[Health check URL path, or "None"]Health check interval and timeout:
[Interval and timeout values, or "Use defaults"]Other instructions that must be followed to containerize the project:
[Specific requirements, or "None"]Known issues to address:
[Describe any known issues, or "None"]progress.md file to track changes with check marksTargetFramework elementConfirm that Docker build succeeds once the Dockerfile is completed. Use the following command to build the Docker image:
docker build -t aspnetcore-app:latest .
If the build fails, review the error messages and make necessary adjustments to the Dockerfile or project configuration. Report success/failure.
Maintain a progress.md file with the following structure:
# Containerization Progress
## Environment Detection
- [ ] .NET version detection (version: ___)
- [ ] Linux distribution selection (distribution: ___)
## Configuration Changes
- [ ] Application configuration verification for environment variable support
- [ ] NuGet package source configuration (if applicable)
## Containerization
- [ ] Dockerfile creation
- [ ] .dockerignore file creation
- [ ] Build stage created with SDK image
- [ ] csproj file(s) copied for package restore
- [ ] NuGet.config copied if applicable
- [ ] Runtime stage created with runtime image
- [ ] Non-root user configuration
- [ ] Dependency handling (system packages, native libraries, tools, etc.)
- [ ] Health check configuration (if applicable)
- [ ] Special requirements implementation
## Verification
- [ ] Review containerization settings and make sure that all requirements are met
- [ ] Docker build success
Do not pause for confirmation between steps. Continue methodically until the application has been containerized and Docker build succeeds.
YOU ARE NOT DONE UNTIL ALL CHECKBOXES ARE MARKED! This includes building the Docker image successfully and addressing any issues that arise during the build process.
An example Dockerfile for an ASP.NET Core (.NET) application using a Linux base image.
# ============================================================
# Stage 1: Build and publish the application
# ============================================================
# Base Image - Select the appropriate .NET SDK version and Linux distribution
# Possible tags include:
# - 8.0-bookworm-slim (Debian 12)
# - 8.0-noble (Ubuntu 24.04)
# - 8.0-alpine (Alpine Linux)
# - 9.0-bookworm-slim (Debian 12)
# - 9.0-noble (Ubuntu 24.04)
# - 9.0-alpine (Alpine Linux)
# Uses the .NET SDK image for building the application
FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy project files first for better caching
COPY ["YourProject/YourProject.csproj", "YourProject/"]
COPY ["YourOtherProject/YourOtherProject.csproj", "YourOtherProject/"]
# Copy NuGet configuration if it exists
COPY ["NuGet.config", "."]
# Restore NuGet packages
RUN dotnet restore "YourProject/YourProject.csproj"
# Copy source code
COPY . .
# Perform custom pre-build steps here, if needed
# RUN echo "Running pre-build steps..."
# Build and publish the application
WORKDIR "/src/YourProject"
RUN dotnet build "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Publish the application
RUN dotnet publish "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Perform custom post-build steps here, if needed
# RUN echo "Running post-build steps..."
# ============================================================
# Stage 2: Final runtime image
# ============================================================
# Base Image - Select the appropriate .NET runtime version and Linux distribution
# Possible tags include:
# - 8.0-bookworm-slim (Debian 12)
# - 8.0-noble (Ubuntu 24.04)
# - 8.0-alpine (Alpine Linux)
# - 8.0-noble-chiseled (Ubuntu 24.04 Chiseled)
# - 8.0-azurelinux3.0 (Azure Linux)
# - 9.0-bookworm-slim (Debian 12)
# - 9.0-noble (Ubuntu 24.04)
# - 9.0-alpine (Alpine Linux)
# - 9.0-noble-chiseled (Ubuntu 24.04 Chiseled)
# - 9.0-azurelinux3.0 (Azure Linux)
# Uses the .NET runtime image for running the application
FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim AS final
# Install system packages if needed (uncomment and modify as needed)
# RUN apt-get update && apt-get install -y \
# curl \
# wget \
# ca-certificates \
# libgdiplus \
# && rm -rf /var/lib/apt/lists/*
# Install additional .NET tools if needed (uncomment and modify as needed)
# RUN dotnet tool install --global dotnet-ef --version 8.0.0
# ENV PATH="$PATH:/root/.dotnet/tools"
WORKDIR /app
# Copy published application from build stage
COPY --from=build /app/publish .
# Copy additional files if needed (uncomment and modify as needed)
# COPY ./config/appsettings.Production.json .
# COPY ./certificates/ ./certificates/
# Set environment variables
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:8080
# Add custom environment variables if needed (uncomment and modify as needed)
# ENV CONNECTIONSTRINGS__DEFAULTCONNECTION="your-connection-string"
# ENV FEATURE_FLAG_ENABLED=true
# Configure SSL/TLS certificates if needed (uncomment and modify as needed)
# ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/app/certificates/app.pfx
# ENV ASPNETCORE_Kestrel__Certificates__Default__Password=your_password
# Expose the port the application listens on
EXPOSE 8080
# EXPOSE 8081 # Uncomment if using HTTPS
# Install curl for health checks if not already present
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# Configure health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# Create volumes for persistent data if needed (uncomment and modify as needed)
# VOLUME ["/app/data", "/app/logs"]
# Switch to non-root user for security
USER $APP_UID
# Set the entry point for the application
ENTRYPOINT ["dotnet", "YourProject.dll"]
Note: Customize this template based on the specific requirements in containerization settings.
When adapting this example Dockerfile:
YourProject.csproj, YourProject.dll, etc. with your actual project namesFor smaller image sizes, you can use Alpine Linux:
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
# ... build steps ...
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
# Install packages using apk
RUN apk update && apk add --no-cache curl ca-certificates
For minimal attack surface, consider using chiseled images:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled AS final
# Note: Chiseled images have minimal packages, so you may need to use a different base for additional dependencies
For Azure-optimized containers:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-azurelinux3.0 AS final
# Install packages using tdnf
RUN tdnf update -y && tdnf install -y curl ca-certificates && tdnf clean all
AS stage-name syntax gives each stage a name--from=stage-name to copy files from a previous stagefinal stage is the one that becomes the final container imagelatestWeekly Installs
7.3K
Repository
GitHub Stars
26.9K
First Seen
Feb 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykFail
Installed on
codex7.2K
gemini-cli7.2K
opencode7.2K
cursor7.2K
github-copilot7.2K
kimi-cli7.2K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
$APP_UID))
$APP_UID variable to specify the user account..dockerignore file in the root of the project directory to exclude unnecessary files from the Docker image. The .dockerignore file MUST include at least the following elements as well as additional patterns as specified in the containerization settings: