====== Runbook: Docker ====== **Duration:** ~15 minutes \\ **Role:** DevOps, Developer \\ **Prerequisite:** Docker Engine 20+ Run Data Gateway as Docker container. ---- ===== Workflow ===== flowchart TD A[Start] --> B[Create Dockerfile] B --> C[Build image] C --> D[Start container] D --> E[Health Check] E --> F{OK?} F -->|Yes| G[Done] F -->|No| H[Check docker logs] style G fill:#e8f5e9 style H fill:#ffebee ---- ===== 1. Dockerfile ===== # Dockerfile FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime WORKDIR /app # Copy Gateway files COPY publish/ . # Non-root user RUN useradd --system --no-create-home appuser USER appuser # Port EXPOSE 5000 # Healthcheck HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ CMD curl -f http://localhost:5000/health || exit 1 # Start ENTRYPOINT ["dotnet", "WvdS.WebAPI.Data.Gateway.Api.dll"] ---- ===== 2. Build Image ===== # Build image docker build -t data-gateway:latest . # Tag image (for registry) docker tag data-gateway:latest registry.example.com/data-gateway:v3.0 ---- ===== 3. Start Container ===== **Simple start:** docker run -d \ --name gateway \ -p 5000:5000 \ -v $(pwd)/appsettings.json:/app/appsettings.json:ro \ -v $(pwd)/data:/app/data \ data-gateway:latest **With all options:** docker run -d \ --name gateway \ --restart unless-stopped \ -p 5000:5000 \ -v $(pwd)/config/appsettings.json:/app/appsettings.json:ro \ -v $(pwd)/data:/app/data \ -v $(pwd)/logs:/app/logs \ -e ASPNETCORE_ENVIRONMENT=Production \ -e TZ=Europe/Berlin \ --memory=512m \ --cpus=1 \ --health-cmd="curl -f http://localhost:5000/health || exit 1" \ --health-interval=30s \ data-gateway:latest ---- ===== 4. Docker Compose ===== # docker-compose.yml version: '3.8' services: gateway: image: data-gateway:latest build: context: . dockerfile: Dockerfile container_name: data-gateway restart: unless-stopped ports: - "5000:5000" volumes: - ./config/appsettings.json:/app/appsettings.json:ro - ./data:/app/data - gateway-logs:/app/logs environment: - ASPNETCORE_ENVIRONMENT=Production - TZ=Europe/Berlin deploy: resources: limits: memory: 512M cpus: '1' healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/health"] interval: 30s timeout: 5s retries: 3 start_period: 10s networks: - gateway-net # Optional: Database for demo demo-db: image: postgres:16-alpine container_name: demo-db restart: unless-stopped environment: - POSTGRES_USER=gateway - POSTGRES_PASSWORD=gateway123 - POSTGRES_DB=demo volumes: - postgres-data:/var/lib/postgresql/data networks: - gateway-net volumes: gateway-logs: postgres-data: networks: gateway-net: driver: bridge **Start:** # Start all services docker-compose up -d # Show logs docker-compose logs -f gateway # Stop docker-compose down ---- ===== 5. Health Check ===== # Container status docker ps # Health status docker inspect --format='{{.State.Health.Status}}' gateway # API test curl http://localhost:5000/health curl http://localhost:5000/api/v1/dsn/demo/tables ---- ===== 6. Manage Container ===== | Command | Description | |---------|-------------| | ''docker start gateway'' | Start | | ''docker stop gateway'' | Stop | | ''docker restart gateway'' | Restart | | ''docker logs gateway'' | Show logs | | ''docker logs -f gateway'' | Live logs | | ''docker exec -it gateway sh'' | Shell in container | ---- ===== 7. Checklist ===== | # | Check | Done | |---|-------|------| | 1 | Dockerfile created | [ ] | | 2 | Image built | [ ] | | 3 | appsettings.json mounted | [ ] | | 4 | Container started | [ ] | | 5 | Health Check OK | [ ] | | 6 | Logs without errors | [ ] | ---- ===== Troubleshooting ===== | Problem | Cause | Solution | |---------|-------|----------| | ''Container won't start'' | Config error | ''docker logs gateway'' | | ''Port already in use'' | Host port taken | Use different port | | ''Permission denied'' | Volume permissions | ''chmod'' on host | | ''Cannot connect to DB'' | Network problem | Check Docker network | **Container shell for debugging:** # Shell in running container docker exec -it gateway sh # Or with bash (if available) docker exec -it gateway bash # Check files in container docker exec gateway ls -la /app/ docker exec gateway cat /app/appsettings.json ---- ===== Multi-Stage Build (optimized) ===== # Multi-Stage Dockerfile FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY *.csproj . RUN dotnet restore COPY . . RUN dotnet publish -c Release -o /app/publish FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime WORKDIR /app COPY --from=build /app/publish . RUN useradd --system --no-create-home appuser USER appuser EXPOSE 5000 HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost:5000/health || exit 1 ENTRYPOINT ["dotnet", "WvdS.WebAPI.Data.Gateway.Api.dll"] ---- ===== Related Runbooks ===== * [[.:kubernetes|Kubernetes]] - Cluster deployment * [[..:monitoring:prometheus|Prometheus]] - Export metrics * [[.:systemd|systemd]] - Bare-metal alternative ---- << [[.:systemd|<- systemd]] | [[.:kubernetes|-> Kubernetes]] >> ---- //Wolfgang van der Stille @ EMSR DATA d.o.o. - Data Gateway Professional// {{tag>operator runbook docker container compose}}