Roadmap › Docker & Containers › Module: Docker Compose
Lessons in this course (8)
5. Compose for a .NET API + SQL Server + Redis
Intermediate★ Must knowLesson 5 of 8
yaml
# compose.yaml
services:
api:
build:
context: .
dockerfile: src/Shop.Api/Dockerfile
ports:
- "8080:8080"
environment:
ASPNETCORE_ENVIRONMENT: Development
ConnectionStrings__Db: "Server=sql;Database=Shop;User Id=sa;Password=${SA_PASSWORD};TrustServerCertificate=True"
ConnectionStrings__Redis: "redis:6379"
depends_on:
sql:
condition: service_healthy
redis:
condition: service_started
sql:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: ${SA_PASSWORD}
volumes:
- sqldata:/var/opt/mssql
healthcheck:
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P \"$$MSSQL_SA_PASSWORD\" -C -Q 'SELECT 1' || exit 1"]
interval: 10s
retries: 10
redis:
image: redis:7-alpine
volumes:
sqldata:bash
docker compose up -d --build # build + start everything
docker compose ps
docker compose logs -f api
docker compose exec api sh
docker compose down # stop and remove containers + network (add -v to delete volumes)Compose creates a default network so services reach each other by service name (sql, redis). ${SA_PASSWORD} comes from a .env file (keep it out of Git). depends_on controls start order; with condition: service_healthy it waits for the health check.