Introduction
Docker packages ShopNest API + dependencies into portable images — same container runs on your laptop, Azure, or client data center. Multi-stage builds keep images small.
After this article you will
- Write multi-stage Dockerfile
- Use Docker Compose with SQL + Redis
- Configure env vars and volumes
- Add HEALTHCHECK and .dockerignore
- Push to Azure Container Registry
Prerequisites
- Article 59 — Deploying to IIS
- ShopNest solution builds and tests pass locally
Concept deep-dive
# Dockerfile — ShopNest.Api
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore ShopNest.Api/ShopNest.Api.csproj
RUN dotnet publish ShopNest.Api/ShopNest.Api.csproj -c Release -o /app
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=build /app .
EXPOSE 8080
HEALTHCHECK CMD curl -f http://localhost:8080/health/ready || exit 1
ENTRYPOINT ["dotnet", "ShopNest.Api.dll"]
# docker-compose.yml
services:
api:
build: .
ports: ["8080:8080"]
environment:
ConnectionStrings__Default: "Server=sql;Database=ShopNest;User=sa;Password=..."
depends_on: [sql, redis]
sql:
image: mcr.microsoft.com/mssql/server:2022-latest
redis:
image: redis:7-alpineHands-on — ShopNest Microservices Application
- docker build -t shopnest-api .
- docker-compose up — API + SQL + Redis.
- .dockerignore excludes bin/obj.
- VS 2022 Container Tools attach debugger.
Common errors & best practices
- SDK image in production — use aspnet runtime only in final stage.
- Linux container on Windows without switching — mode mismatch.
- Secrets in Dockerfile ENV — use secrets/compose env files.
Interview questions
Q: Container vs VM?
A: Containers share OS kernel — lighter, faster start than full VMs.
Q: Multi-stage build?
A: Build in SDK image; copy published output to slim runtime image.
Summary
- Multi-stage Dockerfile for small production images
- Compose runs full ShopNest stack locally
- Health checks integrate with orchestrators
- ACR for private Azure images
Previous: Deploying to IIS
Next: Azure App Service
FAQ
Docker Hub vs ACR?
ACR private registry integrated with Azure RBAC.
Image size tips?
Alpine-based runtime, .dockerignore, no SDK in final stage.