OK
HomeBlogResourcesAboutContactSponsor Start Reading →
.NET

Deploy ASP.NET Core Web API to Docker, Kubernetes, and AWS: A Practical Guide

SEO keywords: deploy ASP.NET Core to Docker, .NET Kubernetes deployment, ASP.NET Core AWS, Docker Web API, Kubernetes health checks.

Deploying an ASP.NET Core Web API to production is easier when the application is designed for containers from the beginning. Docker gives repeatable builds, Kubernetes gives orchestration, and AWS gives managed infrastructure for hosting, secrets, networking, and observability.

Start with a Production Dockerfile

A good Dockerfile should use multi-stage builds, keep the runtime image small, and avoid copying development files into production.

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

COPY *.sln ./
COPY src/Api/Api.csproj src/Api/
RUN dotnet restore

COPY . .
RUN dotnet publish src/Api/Api.csproj -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app
COPY --from=build /app/publish .

ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

ENTRYPOINT ["dotnet", "Api.dll"]

Add Health Checks

Kubernetes and AWS load balancers need reliable health endpoints. Add a simple health check first, then expand it to databases and external services.

builder.Services.AddHealthChecks();

var app = builder.Build();

app.MapHealthChecks("/health/live");
app.MapHealthChecks("/health/ready");

Kubernetes Deployment Example

A basic Kubernetes deployment should define replicas, container ports, environment variables, and probes.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: codewithchai-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: codewithchai-api
  template:
    metadata:
      labels:
        app: codewithchai-api
    spec:
      containers:
        - name: api
          image: your-registry/codewithchai-api:latest
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /health/ready
              port: 8080
          livenessProbe:
            httpGet:
              path: /health/live
              port: 8080

AWS Deployment Options

  • Elastic Beanstalk: easiest path for simple .NET apps.
  • ECS Fargate: good for containerized APIs without managing servers.
  • EKS: best when your team already uses Kubernetes.
  • App Runner: simple container hosting for web APIs.

Configuration Rules

Use environment variables for deployment-specific settings. Keep secrets out of source control and Docker images.

ConnectionStrings__Default=from-secret-manager
ASPNETCORE_ENVIRONMENT=Production
Logging__LogLevel__Default=Information

Final Thoughts

Docker, Kubernetes, and AWS work best when your ASP.NET Core app is built with production basics: health checks, externalized configuration, small images, logs, and clear deployment boundaries. Start simple, then add CI/CD and observability as your application grows.

Finished reading. Nice work.

Related Articles