Introduction
Containerising applications solves the classic "works on my machine" problem, yet shipping containers reliably into production requires disciplined CI/CD and target-specific rollout mechanics. A pipeline that works seamlessly for Azure Kubernetes Service (AKS)—where Kubernetes handles ReplicaSets, rolling updates, readiness probes, and Ingress/service routing differs significantly from deploying containers to dedicated Linux virtual machines or bare-metal servers, where you manage Docker Engine or systemd services, reverse proxies, and host disk/firewall boundaries directly.
Many teams run both topologies: cloud-native services on AKS alongside dedicated Linux hosts for specialized workloads, isolated environments, or cost-predictable infrastructure. Having a single, deterministic build-and-push stage feeding clean deployment paths for either target keeps releases fast, observable, and reversible.
This article covers:
Building immutable container images once in CI and tagging them cleanly for downstream promotion.
Deploying to Azure Kubernetes Service (AKS) using
KubernetesManifest@1orkubectlwith Azure Pipelines Environments.Deploying to dedicated Linux hosts over SSH using Docker Compose, health checks, and safe cleanup.
Comparing operational trade-offs and avoiding common container deployment pitfalls.
Architectural Blueprint: Build Once, Deploy to Target
Whether your runtime target is an AKS cluster or a dedicated Linux host, separate image build/publish from environment rollout:
[Git Push / Merge to main]
│
▼
┌───────────────────────────────┐
│ Stage 1: Build & Push Image │
│ - Build & test .NET 9 app │
│ - Build Docker image │
│ - Push immutable tag to ACR │
└───────────────┬───────────────┘
│
┌────────┴────────┐
▼ ▼
┌─────────────┐ ┌─────────────┐
│ AKS Target │ │ Linux Host │
│ (K8s / Helm│ │ (SSH + │
│ Manifests) │ │ Compose) │
└─────────────┘ └─────────────┘
Core Image Hygiene Rules
Never rely on
:latestin production: Always tag builds with an immutable identifier such as$(Build.BuildId)or$(Build.SourceVersion)(short commit SHA), and optionally move an environment pointer tag (likestagingorprod) only after verification.Use multi-stage Dockerfiles: Compile and test in an SDK stage, then copy only the published runtime output into a lean ASP.NET runtime image running as a non-root user.
Authenticate without static secrets: Use Workload Identity Federation for Azure/ACR service connections, and store SSH private keys in Azure Pipelines Secure files (or restricted variable groups) rather than plain YAML.
Part 1: Multi-Stage Dockerfile and Build/Push Stage
Here is a production-ready multi-stage Dockerfile for a .NET 9 API:
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY ["Directory.Packages.props", "./"]
COPY ["src/Orders.Api/Orders.Api.csproj", "src/Orders.Api/"]
RUN dotnet restore "src/Orders.Api/Orders.Api.csproj"
COPY . .
RUN dotnet publish "src/Orders.Api/Orders.Api.csproj" \
-c Release \
-o /app/publish \
/p:UseAppHost=false
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app
EXPOSE 8080
# Run as non-root user provided by .NET container images
USER app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "Orders.Api.dll"]
In the CI stage, build and push the immutable image tag to Azure Container Registry (or another registry):
YAML
trigger:
branches:
include:
- main
variables:
buildConfiguration: 'Release'
acrLoginServer: 'enterpriseprodacr.azurecr.io'
imageRepository: 'orders-api'
imageTag: '$(Build.BuildId)'
azureServiceConnection: 'sc-prod-wif'
stages:
- stage: BuildAndPush
displayName: 'Build .NET App & Push Container Image'
jobs:
- job: BuildImage
displayName: 'Build & Push Image'
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
displayName: 'Install .NET 9 SDK'
inputs:
packageType: 'sdk'
version: '9.0.x'
- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests'
inputs:
command: 'test'
projects: '**/*Tests.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: Docker@2
displayName: 'Login to Container Registry'
inputs:
containerRegistry: '$(azureServiceConnection)'
command: 'login'
- task: Docker@2
displayName: 'Build and Push Immutable Tag'
inputs:
containerRegistry: '$(azureServiceConnection)'
repository: '$(imageRepository)'
command: 'buildAndPush'
Dockerfile: 'src/Orders.Api/Dockerfile'
buildContext: '.'
tags: |
$(imageTag)
Part 2: Deploying to Azure Kubernetes Service (AKS)
When deploying to AKS, Kubernetes manages pod replacement, rolling updates, and service endpoints. Binding the deployment job to an Azure Pipelines Environment (for example, aks-production) gives you deployment history, resource tracking, and manual approval gates.
Example Kubernetes Deployment & Service Manifest (k8s/orders-api.yaml)
YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders-api
namespace: workloads
spec:
replicas: 3
selector:
matchLabels:
app: orders-api
template:
metadata:
labels:
app: orders-api
spec:
containers:
- name: orders-api
image: enterpriseprodacr.azurecr.io/orders-api:latest
ports:
- containerPort: 8080
env:
- name: ASPNETCORE_URLS
value: "http://+:8080"
- name: ASPNETCORE_ENVIRONMENT
value: "Production"
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
---
apiVersion: v1
kind: Service
metadata:
name: orders-api
namespace: workloads
spec:
selector:
app: orders-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Join the conversation! Your thoughts help the community grow.