| name | containerize-aspnetcore |
| description | Containerize an ASP.NET Core project by creating Dockerfile and .dockerfile files customized for the project. |
ASP.NET Core Docker Containerization Prompt
Containerization Request
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. Follow best practices for containerizing .NET Core applications, ensuring that the container is optimized for performance, security, and maintainability.
Containerization Settings
Basic Project Information
- Project to containerize:
[ProjectName (provide path to .csproj file)]
- .NET version to use:
[8.0 or 9.0 (Default 8.0)]
- Linux distribution:
[debian, alpine, ubuntu, chiseled, or Azure Linux (mariner) (Default debian)]
Container Configuration
- Ports to expose: Primary HTTP port
[e.g., 8080]
- User account:
[User account, or default to "$APP_UID"]
- Application URL:
[ASPNETCORE_URLS, or default to "http://+:8080"]
Dependencies
- System packages:
[Package names, or "None"]
- Native libraries:
[Library names and paths, or "None"]
- Additional .NET tools:
[Tool names and versions, or "None"]
Execution Process
- Determine the .NET version from the project's .csproj
TargetFramework element
- Select appropriate Linux container image based on .NET version and Linux distribution
- Create a multi-stage Dockerfile:
- Build stage: Use .NET SDK image to build and publish the application
- Final stage: Use .NET runtime image to run the application
- Create a
.dockerignore file excluding unnecessary files
- Configure health checks if a health endpoint is provided
- Run
docker build -t aspnetcore-app:latest . to verify
Example Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["YourProject/YourProject.csproj", "YourProject/"]
RUN dotnet restore "YourProject/YourProject.csproj"
COPY . .
WORKDIR "/src/YourProject"
RUN dotnet publish "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim AS final
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
COPY --from=build /app/publish .
USER $APP_UID
ENTRYPOINT ["dotnet", "YourProject.dll"]
Linux Distribution Variations
Alpine Linux
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
RUN apk update && apk add --no-cache curl ca-certificates
Ubuntu Chiseled (minimal attack surface)
FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled AS final
Azure Linux (Mariner)
FROM mcr.microsoft.com/dotnet/aspnet:8.0-azurelinux3.0 AS final
RUN tdnf update -y && tdnf install -y curl ca-certificates && tdnf clean all
Security Best Practices
- Always run as a non-root user in production (
USER $APP_UID)
- Use specific image tags instead of
latest
- Minimize the number of installed packages
- Use multi-stage builds to exclude build dependencies
- Keep base images updated