ASP.NET Core  

How to Deploy ASP.NET Core App with Docker on Azure Web App

Deploying an ASP.NET Core application using Docker and Azure Web App for Containers is a modern and scalable approach for hosting cloud-native applications. This deployment strategy combines containerization with fully managed Azure infrastructure, enabling portability, consistency across environments, and simplified DevOps workflows. By packaging your application inside a Docker container and deploying it to Azure Web App for Containers, you gain flexibility, isolation, and production-ready scalability without managing virtual machines.

This article provides a complete practical guide, including architecture explanation, Dockerfile configuration, container registry setup, Azure deployment steps, real-world scenarios, advantages, disadvantages, and production best practices.

What Does It Mean to Deploy Using Docker and Azure Web App for Containers?

In traditional deployment, applications are published directly to a server. In containerized deployment:

  • The ASP.NET Core application is packaged into a Docker image.

  • The image contains application code, runtime, dependencies, and configuration.

  • The image is pushed to a container registry.

  • Azure Web App pulls and runs the container.

In simple terms, instead of deploying files, you deploy a portable runtime environment.

Why Use Docker with ASP.NET Core?

Docker provides:

  • Environment consistency (works the same in development and production)

  • Isolation from host system

  • Simplified CI/CD integration

  • Easier scaling and rollback

  • Microservices compatibility

For modern DevOps pipelines, containerization is a best practice.

Real-World Analogy

Think of Docker like shipping goods in standardized containers. Regardless of what is inside, ports and ships handle containers uniformly. Azure Web App acts like a managed port that runs your container without worrying about the internal contents.

High-Level Architecture Flow

Developer → Build Docker Image → Push to Azure Container Registry → Azure Web App Pulls Image → Container Runs → Public Endpoint Available

This ensures consistent deployment across environments.

Step 1: Create ASP.NET Core Application

Create a Web API project:

dotnet new webapi -n MyApp
cd MyApp

Step 2: Create Dockerfile

Inside project root, create a file named Dockerfile:

# Build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /publish

# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /publish .
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]

This uses multi-stage build to reduce final image size.

Step 3: Build Docker Image Locally

docker build -t myapp:latest .

Test locally:

docker run -p 8080:8080 myapp

Access via:

http://localhost:8080

Step 4: Push Image to Azure Container Registry (ACR)

Create ACR:

az acr create --resource-group MyResourceGroup --name MyContainerRegistry --sku Basic

Login to ACR:

az acr login --name MyContainerRegistry

Tag image:

docker tag myapp:latest mycontainerregistry.azurecr.io/myapp:latest

Push image:

docker push mycontainerregistry.azurecr.io/myapp:latest

Step 5: Create Azure Web App for Containers

Create App Service Plan:

az appservice plan create --name MyAppServicePlan --resource-group MyResourceGroup --is-linux --sku B1

Create Web App:

az webapp create \
  --resource-group MyResourceGroup \
  --plan MyAppServicePlan \
  --name MyDockerWebApp \
  --deployment-container-image-name mycontainerregistry.azurecr.io/myapp:latest

Configure container registry credentials:

az webapp config container set \
  --name MyDockerWebApp \
  --resource-group MyResourceGroup \
  --docker-custom-image-name mycontainerregistry.azurecr.io/myapp:latest \
  --docker-registry-server-url https://mycontainerregistry.azurecr.io

Azure will pull the image and run the container.

Step 6: Configure Environment Variables

In Azure Portal or CLI:

az webapp config appsettings set \
  --name MyDockerWebApp \
  --resource-group MyResourceGroup \
  --settings ASPNETCORE_ENVIRONMENT=Production

Environment variables should not be hardcoded inside Docker image.

Real Business Scenario: SaaS Application

Consider a SaaS platform serving thousands of users:

  • Application is containerized.

  • CI/CD pipeline builds image on each commit.

  • Image pushed to ACR.

  • Azure Web App automatically pulls new image.

  • Auto-scaling handles traffic spikes.

If a deployment fails, rollback is as simple as redeploying previous image tag.

Detailed Difference: Traditional App Service vs Web App for Containers

FeatureTraditional App ServiceWeb App for Containers
Deployment TypePublish filesDeploy container image
Environment ConsistencyMediumHigh
Dependency ControlManaged by hostFully controlled in image
PortabilityLimitedHighly portable
Microservices FriendlyModerateHigh
CI/CD IntegrationGoodExcellent
Custom RuntimeLimitedFull control
ScalingSupportedSupported
DevOps FlexibilityModerateHigh
RollbackManual redeployImage tag rollback

Advantages of Docker Deployment on Azure

  • Consistent environment

  • Better isolation

  • Easier horizontal scaling

  • Simplified DevOps pipelines

  • Supports blue-green deployments

  • Works well in microservices

Disadvantages

  • Additional learning curve

  • Docker image maintenance required

  • Larger initial setup complexity

  • Improper image optimization can increase cost

Common Mistakes Developers Make

  • Not using multi-stage builds

  • Hardcoding secrets inside Dockerfile

  • Ignoring container health checks

  • Using large base images

  • Not configuring correct exposed port

  • Forgetting to enable logging

Best Practices for Production Deployment

  • Use multi-stage Docker builds

  • Keep images small

  • Use managed identity instead of storing credentials

  • Enable Application Insights

  • Implement health checks

  • Use proper logging configuration

  • Automate deployment with GitHub Actions or Azure DevOps

  • Tag images with version numbers instead of "latest"

Enterprise Architecture Flow Example

Client → Azure Front Door → Azure Web App for Containers → Docker Container → ASP.NET Core Application → Database → Monitoring and Logging

This setup ensures scalability, high availability, and production readiness.

When NOT to Use Containers

  • Very small internal apps

  • Teams unfamiliar with Docker

  • Extremely simple CRUD apps without scaling needs

In such cases, traditional App Service deployment may be simpler.

FAQ

Is Azure Web App for Containers different from AKS?

Yes. Web App for Containers is fully managed PaaS. AKS provides full Kubernetes orchestration control.

Can we use Docker Compose?

For local development yes. In Azure Web App, single container images are typically used.

Does Azure automatically scale containers?

Yes, based on configured scaling rules in App Service Plan.

Conclusion

Deploying an ASP.NET Core application using Docker and Azure Web App for Containers provides a modern, portable, and scalable hosting strategy suitable for cloud-native and microservices architectures. By packaging the application into a Docker image, pushing it to Azure Container Registry, and configuring Azure Web App to pull and run the container, developers gain full control over runtime dependencies while benefiting from managed infrastructure. Although container-based deployment introduces additional setup complexity, its advantages in consistency, DevOps automation, scalability, and rollback capabilities make it a preferred choice for production-grade enterprise applications.