$npx -y skills add softspark/ai-toolkit --skill docker-devopsDocker/K8s: Dockerfile, multi-stage, compose, manifests, Helm. Triggers: Docker, Dockerfile, container, Kubernetes, k8s, compose, Helm, pod.
| 1 | # Docker & DevOps Skill |
| 2 | |
| 3 | ## Dockerfile Best Practices |
| 4 | |
| 5 | ### Multi-Stage Build (Node.js) |
| 6 | |
| 7 | ```dockerfile |
| 8 | # Build stage |
| 9 | FROM node:20-alpine AS builder |
| 10 | WORKDIR /app |
| 11 | COPY package*.json ./ |
| 12 | RUN npm ci |
| 13 | COPY . . |
| 14 | RUN npm run build |
| 15 | |
| 16 | # Production stage |
| 17 | FROM node:20-alpine AS runner |
| 18 | WORKDIR /app |
| 19 | ENV NODE_ENV=production |
| 20 | |
| 21 | # Non-root user |
| 22 | RUN addgroup -g 1001 -S nodejs |
| 23 | RUN adduser -S nextjs -u 1001 |
| 24 | |
| 25 | COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 26 | COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 27 | COPY --from=builder --chown=nextjs:nodejs /app/public ./public |
| 28 | |
| 29 | USER nextjs |
| 30 | EXPOSE 3000 |
| 31 | CMD ["node", "server.js"] |
| 32 | ``` |
| 33 | |
| 34 | ### Multi-Stage Build (Python) |
| 35 | |
| 36 | ```dockerfile |
| 37 | # Build stage |
| 38 | FROM python:3.12-slim AS builder |
| 39 | WORKDIR /app |
| 40 | RUN pip install --no-cache-dir poetry |
| 41 | COPY pyproject.toml poetry.lock ./ |
| 42 | RUN poetry export -f requirements.txt -o requirements.txt |
| 43 | |
| 44 | # Production stage |
| 45 | FROM python:3.12-slim |
| 46 | WORKDIR /app |
| 47 | |
| 48 | # Non-root user |
| 49 | RUN useradd -m -u 1000 appuser |
| 50 | |
| 51 | COPY --from=builder /app/requirements.txt . |
| 52 | RUN pip install --no-cache-dir -r requirements.txt |
| 53 | |
| 54 | COPY --chown=appuser:appuser . . |
| 55 | USER appuser |
| 56 | |
| 57 | EXPOSE 8000 |
| 58 | CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] |
| 59 | ``` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## Docker Compose Patterns |
| 64 | |
| 65 | ### Development Setup |
| 66 | |
| 67 | ```yaml |
| 68 | version: '3.8' |
| 69 | |
| 70 | services: |
| 71 | app: |
| 72 | build: |
| 73 | context: . |
| 74 | dockerfile: Dockerfile.dev |
| 75 | volumes: |
| 76 | - .:/app |
| 77 | - /app/node_modules |
| 78 | ports: |
| 79 | - "3000:3000" |
| 80 | environment: |
| 81 | - NODE_ENV=development |
| 82 | - DATABASE_URL=postgresql://postgres:postgres@db:5432/app |
| 83 | depends_on: |
| 84 | - db |
| 85 | - redis |
| 86 | |
| 87 | db: |
| 88 | image: postgres:16-alpine |
| 89 | volumes: |
| 90 | - postgres_data:/var/lib/postgresql/data |
| 91 | environment: |
| 92 | POSTGRES_USER: postgres |
| 93 | POSTGRES_PASSWORD: postgres |
| 94 | POSTGRES_DB: app |
| 95 | ports: |
| 96 | - "5432:5432" |
| 97 | |
| 98 | redis: |
| 99 | image: redis:7-alpine |
| 100 | ports: |
| 101 | - "6379:6379" |
| 102 | |
| 103 | volumes: |
| 104 | postgres_data: |
| 105 | ``` |
| 106 | |
| 107 | ### Production Setup |
| 108 | |
| 109 | ```yaml |
| 110 | version: '3.8' |
| 111 | |
| 112 | services: |
| 113 | app: |
| 114 | image: ${REGISTRY}/app:${TAG} |
| 115 | restart: unless-stopped |
| 116 | ports: |
| 117 | - "3000:3000" |
| 118 | environment: |
| 119 | - NODE_ENV=production |
| 120 | - DATABASE_URL=${DATABASE_URL} |
| 121 | healthcheck: |
| 122 | test: ["CMD", "curl", "-f", "http://localhost:3000/health"] |
| 123 | interval: 30s |
| 124 | timeout: 10s |
| 125 | retries: 3 |
| 126 | deploy: |
| 127 | resources: |
| 128 | limits: |
| 129 | cpus: '1' |
| 130 | memory: 512M |
| 131 | ``` |
| 132 | |
| 133 | --- |
| 134 | |
| 135 | ## CI/CD Patterns |
| 136 | |
| 137 | ### GitHub Actions (Node.js) |
| 138 | |
| 139 | ```yaml |
| 140 | name: CI/CD |
| 141 | |
| 142 | on: |
| 143 | push: |
| 144 | branches: [main] |
| 145 | pull_request: |
| 146 | branches: [main] |
| 147 | |
| 148 | jobs: |
| 149 | test: |
| 150 | runs-on: ubuntu-latest |
| 151 | steps: |
| 152 | - uses: actions/checkout@v6 |
| 153 | - uses: actions/setup-node@v6 |
| 154 | with: |
| 155 | node-version: '20' |
| 156 | cache: 'npm' |
| 157 | - run: npm ci |
| 158 | - run: npm run lint |
| 159 | - run: npm run test |
| 160 | - run: npm run build |
| 161 | |
| 162 | deploy: |
| 163 | needs: test |
| 164 | if: github.ref == 'refs/heads/main' |
| 165 | runs-on: ubuntu-latest |
| 166 | steps: |
| 167 | - uses: actions/checkout@v6 |
| 168 | - name: Deploy to production |
| 169 | run: | |
| 170 | # Deploy commands |
| 171 | ``` |
| 172 | |
| 173 | ### GitHub Actions (Python) |
| 174 | |
| 175 | ```yaml |
| 176 | name: CI/CD |
| 177 | |
| 178 | on: |
| 179 | push: |
| 180 | branches: [main] |
| 181 | pull_request: |
| 182 | |
| 183 | jobs: |
| 184 | test: |
| 185 | runs-on: ubuntu-latest |
| 186 | steps: |
| 187 | - uses: actions/checkout@v6 |
| 188 | - uses: actions/setup-python@v5 |
| 189 | with: |
| 190 | python-version: '3.12' |
| 191 | - name: Install dependencies |
| 192 | run: | |
| 193 | pip install poetry |
| 194 | poetry install |
| 195 | - name: Lint |
| 196 | run: poetry run ruff check . |
| 197 | - name: Type check |
| 198 | run: poetry run mypy . |
| 199 | - name: Test |
| 200 | run: poetry run pytest --cov |
| 201 | ``` |
| 202 | |
| 203 | --- |
| 204 | |
| 205 | ## Infrastructure as Code |
| 206 | |
| 207 | ### Terraform Basic Structure |
| 208 | |
| 209 | ``` |
| 210 | infrastructure/ |
| 211 | ├── main.tf |
| 212 | ├── variables.tf |
| 213 | ├── outputs.tf |
| 214 | ├── providers.tf |
| 215 | ├── modules/ |
| 216 | │ ├── networking/ |
| 217 | │ ├── compute/ |
| 218 | │ └── database/ |
| 219 | └── environments/ |
| 220 | ├── dev/ |
| 221 | ├── staging/ |
| 222 | └── prod/ |
| 223 | ``` |
| 224 | |
| 225 | ### Terraform Best Practices |
| 226 | |
| 227 | ```hcl |
| 228 | # variables.tf |
| 229 | variable "environment" { |
| 230 | description = "Environment name" |
| 231 | type = string |
| 232 | validation { |
| 233 | condition = contains(["dev", "staging", "prod"], var.environment) |
| 234 | error_message = "Environment must be dev, staging, or prod." |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | # main.tf |
| 239 | resource "aws_instance" "app" { |
| 240 | ami = var.ami_id |
| 241 | instance_type = var.instance_type |
| 242 | |
| 243 | tags = { |
| 244 | Name = "${var.project}-${var.environment}-app" |
| 245 | Environment = var.environment |
| 246 | ManagedBy = "terraform" |
| 247 | } |
| 248 | } |
| 249 | ``` |
| 250 | |
| 251 | --- |
| 252 | |
| 253 | ## Health Checks |
| 254 | |
| 255 | ### HTTP Health Check |
| 256 | |
| 257 | ```python |
| 258 | # FastAPI |
| 259 | @app.get("/health") |
| 260 | async def health_check(): |
| 261 | return { |
| 262 | "status": "healthy", |
| 263 | "timestamp": datetime.utcnow().isoformat(), |
| 264 | "version": settings.VERSION |
| 265 | } |
| 266 | |
| 267 | @app.get("/ready") |
| 268 | async def readiness_check(): |
| 269 | # Check datab |