start-vibing 2.0.8 → 2.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +177 -176
  2. package/dist/cli.js +38 -11
  3. package/package.json +42 -42
  4. package/template/.claude/CLAUDE.md +174 -152
  5. package/template/.claude/agents/01-orchestration/agent-selector.md +130 -123
  6. package/template/.claude/agents/01-orchestration/checkpoint-manager.md +142 -131
  7. package/template/.claude/agents/01-orchestration/context-manager.md +138 -124
  8. package/template/.claude/agents/01-orchestration/error-recovery.md +182 -176
  9. package/template/.claude/agents/01-orchestration/orchestrator.md +114 -107
  10. package/template/.claude/agents/01-orchestration/parallel-coordinator.md +141 -130
  11. package/template/.claude/agents/01-orchestration/task-decomposer.md +121 -118
  12. package/template/.claude/agents/01-orchestration/workflow-router.md +114 -111
  13. package/template/.claude/agents/02-typescript/bun-runtime-expert.md +197 -180
  14. package/template/.claude/agents/02-typescript/esm-resolver.md +193 -187
  15. package/template/.claude/agents/02-typescript/import-alias-enforcer.md +158 -149
  16. package/template/.claude/agents/02-typescript/ts-generics-helper.md +183 -165
  17. package/template/.claude/agents/02-typescript/ts-migration-helper.md +238 -227
  18. package/template/.claude/agents/02-typescript/ts-strict-checker.md +180 -162
  19. package/template/.claude/agents/02-typescript/ts-types-analyzer.md +199 -185
  20. package/template/.claude/agents/02-typescript/type-definition-writer.md +187 -183
  21. package/template/.claude/agents/02-typescript/zod-schema-designer.md +212 -197
  22. package/template/.claude/agents/02-typescript/zod-validator.md +158 -153
  23. package/template/.claude/agents/03-testing/playwright-assertions.md +265 -255
  24. package/template/.claude/agents/03-testing/playwright-e2e.md +247 -245
  25. package/template/.claude/agents/03-testing/playwright-fixtures.md +234 -240
  26. package/template/.claude/agents/03-testing/playwright-multi-viewport.md +256 -261
  27. package/template/.claude/agents/03-testing/playwright-page-objects.md +247 -247
  28. package/template/.claude/agents/03-testing/test-cleanup-manager.md +248 -256
  29. package/template/.claude/agents/03-testing/test-data-generator.md +254 -266
  30. package/template/.claude/agents/03-testing/tester-integration.md +278 -278
  31. package/template/.claude/agents/03-testing/tester-unit.md +207 -204
  32. package/template/.claude/agents/03-testing/vitest-config.md +287 -289
  33. package/template/.claude/agents/04-docker/container-health.md +255 -239
  34. package/template/.claude/agents/04-docker/deployment-validator.md +225 -217
  35. package/template/.claude/agents/04-docker/docker-compose-designer.md +281 -268
  36. package/template/.claude/agents/04-docker/docker-env-manager.md +235 -228
  37. package/template/.claude/agents/04-docker/docker-multi-stage.md +241 -229
  38. package/template/.claude/agents/04-docker/dockerfile-optimizer.md +208 -204
  39. package/template/.claude/agents/05-database/data-migration.md +0 -293
  40. package/template/.claude/agents/05-database/database-seeder.md +273 -270
  41. package/template/.claude/agents/05-database/mongodb-query-optimizer.md +230 -219
  42. package/template/.claude/agents/05-database/mongoose-aggregation.md +306 -280
  43. package/template/.claude/agents/05-database/mongoose-index-optimizer.md +182 -174
  44. package/template/.claude/agents/05-database/mongoose-schema-designer.md +267 -267
  45. package/template/.claude/agents/06-security/auth-session-validator.md +68 -65
  46. package/template/.claude/agents/06-security/input-sanitizer.md +80 -81
  47. package/template/.claude/agents/06-security/owasp-checker.md +97 -87
  48. package/template/.claude/agents/06-security/permission-auditor.md +100 -95
  49. package/template/.claude/agents/06-security/security-auditor.md +84 -82
  50. package/template/.claude/agents/06-security/sensitive-data-scanner.md +83 -85
  51. package/template/.claude/agents/07-documentation/api-documenter.md +136 -131
  52. package/template/.claude/agents/07-documentation/changelog-manager.md +105 -96
  53. package/template/.claude/agents/07-documentation/documenter.md +76 -73
  54. package/template/.claude/agents/07-documentation/domain-updater.md +81 -74
  55. package/template/.claude/agents/07-documentation/jsdoc-generator.md +114 -114
  56. package/template/.claude/agents/07-documentation/readme-generator.md +135 -132
  57. package/template/.claude/agents/08-git/branch-manager.md +58 -58
  58. package/template/.claude/agents/08-git/commit-manager.md +63 -61
  59. package/template/.claude/agents/08-git/pr-creator.md +76 -72
  60. package/template/.claude/agents/09-quality/code-reviewer.md +71 -64
  61. package/template/.claude/agents/09-quality/quality-checker.md +67 -67
  62. package/template/.claude/agents/10-research/best-practices-finder.md +89 -82
  63. package/template/.claude/agents/10-research/competitor-analyzer.md +106 -96
  64. package/template/.claude/agents/10-research/pattern-researcher.md +93 -86
  65. package/template/.claude/agents/10-research/research-cache-manager.md +76 -75
  66. package/template/.claude/agents/10-research/research-web.md +98 -91
  67. package/template/.claude/agents/10-research/tech-evaluator.md +101 -94
  68. package/template/.claude/agents/11-ui-ux/accessibility-auditor.md +136 -128
  69. package/template/.claude/agents/11-ui-ux/design-system-enforcer.md +125 -116
  70. package/template/.claude/agents/11-ui-ux/skeleton-generator.md +118 -120
  71. package/template/.claude/agents/11-ui-ux/ui-desktop.md +132 -126
  72. package/template/.claude/agents/11-ui-ux/ui-mobile.md +98 -94
  73. package/template/.claude/agents/11-ui-ux/ui-tablet.md +110 -111
  74. package/template/.claude/agents/12-performance/api-latency-analyzer.md +156 -149
  75. package/template/.claude/agents/12-performance/bundle-analyzer.md +113 -107
  76. package/template/.claude/agents/12-performance/memory-leak-detector.md +137 -126
  77. package/template/.claude/agents/12-performance/performance-profiler.md +115 -108
  78. package/template/.claude/agents/12-performance/query-optimizer.md +124 -116
  79. package/template/.claude/agents/12-performance/render-optimizer.md +154 -148
  80. package/template/.claude/agents/13-debugging/build-error-fixer.md +207 -188
  81. package/template/.claude/agents/13-debugging/debugger.md +149 -137
  82. package/template/.claude/agents/13-debugging/error-stack-analyzer.md +141 -131
  83. package/template/.claude/agents/13-debugging/network-debugger.md +208 -185
  84. package/template/.claude/agents/13-debugging/runtime-error-fixer.md +181 -173
  85. package/template/.claude/agents/13-debugging/type-error-resolver.md +185 -173
  86. package/template/.claude/agents/14-validation/final-validator.md +93 -83
  87. package/template/.claude/agents/_backup/analyzer.md +134 -125
  88. package/template/.claude/agents/_backup/code-reviewer.md +279 -272
  89. package/template/.claude/agents/_backup/commit-manager.md +219 -212
  90. package/template/.claude/agents/_backup/debugger.md +280 -271
  91. package/template/.claude/agents/_backup/documenter.md +237 -220
  92. package/template/.claude/agents/_backup/domain-updater.md +197 -194
  93. package/template/.claude/agents/_backup/final-validator.md +169 -164
  94. package/template/.claude/agents/_backup/orchestrator.md +149 -138
  95. package/template/.claude/agents/_backup/performance.md +232 -228
  96. package/template/.claude/agents/_backup/quality-checker.md +240 -240
  97. package/template/.claude/agents/_backup/research.md +315 -299
  98. package/template/.claude/agents/_backup/security-auditor.md +192 -186
  99. package/template/.claude/agents/_backup/tester.md +566 -564
  100. package/template/.claude/agents/_backup/ui-ux-reviewer.md +247 -242
  101. package/template/.claude/commands/feature.md +48 -48
  102. package/template/.claude/config/README.md +30 -30
  103. package/template/.claude/config/mcp-config.json +344 -330
  104. package/template/.claude/config/project-config.json +53 -53
  105. package/template/.claude/config/quality-gates.json +46 -46
  106. package/template/.claude/config/security-rules.json +45 -45
  107. package/template/.claude/config/testing-config.json +164 -168
  108. package/template/.claude/hooks/SETUP.md +126 -126
  109. package/template/.claude/hooks/run-hook.ts +176 -172
  110. package/template/.claude/hooks/stop-validator.ts +825 -353
  111. package/template/.claude/hooks/user-prompt-submit.ts +886 -794
  112. package/template/.claude/scripts/mcp-quick-install.ts +151 -151
  113. package/template/.claude/scripts/setup-mcps.ts +651 -628
  114. package/template/.claude/settings.json +275 -276
  115. package/template/.claude/skills/bun-runtime/SKILL.md +430 -430
  116. package/template/.claude/skills/codebase-knowledge/SKILL.md +145 -145
  117. package/template/.claude/skills/codebase-knowledge/domains/claude-system.md +431 -403
  118. package/template/.claude/skills/codebase-knowledge/domains/mcp-integration.md +295 -281
  119. package/template/.claude/skills/debugging-patterns/SKILL.md +485 -484
  120. package/template/.claude/skills/docker-patterns/SKILL.md +555 -547
  121. package/template/.claude/skills/docs-tracker/SKILL.md +239 -239
  122. package/template/.claude/skills/final-check/SKILL.md +284 -284
  123. package/template/.claude/skills/git-workflow/SKILL.md +454 -454
  124. package/template/.claude/skills/mongoose-patterns/SKILL.md +499 -512
  125. package/template/.claude/skills/nextjs-app-router/SKILL.md +327 -337
  126. package/template/.claude/skills/performance-patterns/SKILL.md +547 -549
  127. package/template/.claude/skills/playwright-automation/SKILL.md +438 -438
  128. package/template/.claude/skills/quality-gate/SKILL.md +294 -294
  129. package/template/.claude/skills/react-patterns/SKILL.md +389 -376
  130. package/template/.claude/skills/research-cache/SKILL.md +222 -207
  131. package/template/.claude/skills/security-scan/SKILL.md +222 -222
  132. package/template/.claude/skills/shadcn-ui/SKILL.md +511 -520
  133. package/template/.claude/skills/tailwind-patterns/SKILL.md +465 -467
  134. package/template/.claude/skills/test-coverage/SKILL.md +467 -464
  135. package/template/.claude/skills/trpc-api/SKILL.md +434 -435
  136. package/template/.claude/skills/typescript-strict/SKILL.md +367 -368
  137. package/template/.claude/skills/ui-ux-audit/SKILL.md +254 -254
  138. package/template/.claude/skills/zod-validation/SKILL.md +403 -405
  139. package/template/CLAUDE.md +25 -25
@@ -1,547 +1,555 @@
1
- # Docker Patterns - Production-Ready Containerization
2
-
3
- ## Purpose
4
-
5
- This skill provides battle-tested Docker patterns and best practices for building, securing, and deploying containerized applications in 2024-2025. Focus on Bun/TypeScript/MongoDB stack with production-grade configurations.
6
-
7
- ---
8
-
9
- ## Core Principles
10
-
11
- ### 1. Multi-Stage Everything
12
- - Separate build from runtime
13
- - Copy only artifacts, not tools
14
- - 50-80% size reduction
15
-
16
- ### 2. Security by Default
17
- - Non-root users always
18
- - Secrets via filesystem, never env vars
19
- - Read-only containers where possible
20
-
21
- ### 3. Cache Optimization
22
- - Dependencies before source code
23
- - Cleanup in same layer
24
- - BuildKit cache mounts
25
-
26
- ### 4. Observable & Resilient
27
- - Health checks on all services
28
- - Structured JSON logging
29
- - Graceful shutdown handling
30
-
31
- ---
32
-
33
- ## Quick Reference
34
-
35
- ### Multi-Stage Build Template (Bun)
36
-
37
- ```dockerfile
38
- # Base
39
- FROM oven/bun:1.1.10-alpine AS base
40
- WORKDIR /app
41
-
42
- # Dependencies
43
- FROM base AS deps
44
- COPY package.json bun.lockb ./
45
- RUN bun install --frozen-lockfile
46
-
47
- # Build
48
- FROM base AS build
49
- COPY --from=deps /app/node_modules ./node_modules
50
- COPY . .
51
- RUN bun run typecheck && bun run build
52
-
53
- # Production
54
- FROM base AS production
55
- RUN apk add --no-cache curl
56
- COPY --from=deps /app/node_modules ./node_modules
57
- COPY --from=build /app/dist ./dist
58
- USER bun
59
- EXPOSE 3000
60
- HEALTHCHECK CMD curl -f http://localhost:3000/health || exit 1
61
- CMD ["bun", "run", "dist/index.js"]
62
- ```
63
-
64
- ### Docker Compose with Secrets & Health Checks
65
-
66
- ```yaml
67
- version: '3.8'
68
-
69
- services:
70
- mongodb:
71
- image: mongo:7-alpine
72
- secrets:
73
- - mongo_root_password
74
- healthcheck:
75
- test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
76
- interval: 10s
77
- timeout: 5s
78
- retries: 5
79
- start_period: 30s
80
-
81
- app:
82
- build: .
83
- depends_on:
84
- mongodb:
85
- condition: service_healthy
86
- secrets:
87
- - mongo_password
88
- - jwt_secret
89
- healthcheck:
90
- test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
91
- interval: 30s
92
- timeout: 10s
93
- retries: 3
94
-
95
- secrets:
96
- mongo_root_password:
97
- file: ./secrets/mongo_root_password.txt
98
- mongo_password:
99
- file: ./secrets/mongo_password.txt
100
- jwt_secret:
101
- file: ./secrets/jwt_secret.txt
102
- ```
103
-
104
- ### .dockerignore Essentials
105
-
106
- ```gitignore
107
- node_modules/
108
- .bun/
109
- dist/
110
- *.log
111
- .git/
112
- .env
113
- .env.*
114
- !.env.example
115
- coverage/
116
- *.test.ts
117
- .DS_Store
118
- Dockerfile*
119
- docker-compose*
120
- ```
121
-
122
- ---
123
-
124
- ## Pattern Catalog
125
-
126
- ### Pattern 1: Layer Caching Optimization
127
-
128
- **Problem:** Rebuilds download all dependencies on code changes.
129
-
130
- **Solution:** Separate dependency installation from code copy.
131
-
132
- ```dockerfile
133
- # WRONG - Invalidates cache on any change
134
- COPY . .
135
- RUN bun install
136
-
137
- # RIGHT - Cache dependencies separately
138
- COPY package.json bun.lockb ./
139
- RUN bun install --frozen-lockfile
140
- COPY . .
141
- ```
142
-
143
- **Impact:** 10x faster rebuilds during development.
144
-
145
- ---
146
-
147
- ### Pattern 2: Non-Root User Security
148
-
149
- **Problem:** Container runs as root, enabling privilege escalation.
150
-
151
- **Solution:** Switch to non-privileged user.
152
-
153
- ```dockerfile
154
- # Create user (if not exists in base image)
155
- RUN addgroup --system --gid 1001 nodejs \
156
- && adduser --system --uid 1001 nodejs
157
-
158
- USER nodejs
159
-
160
- # Or use existing user (Bun images)
161
- USER bun
162
- ```
163
-
164
- **Impact:** Prevents container breakout attacks.
165
-
166
- ---
167
-
168
- ### Pattern 3: Docker Secrets (Not Env Vars)
169
-
170
- **Problem:** Environment variables expose secrets in logs and process lists.
171
-
172
- **Solution:** Use Docker Secrets mounted at `/run/secrets/`.
173
-
174
- ```yaml
175
- # docker-compose.yml
176
- secrets:
177
- db_password:
178
- file: ./secrets/db_password.txt
179
-
180
- services:
181
- app:
182
- secrets:
183
- - db_password
184
- ```
185
-
186
- ```typescript
187
- // src/config.ts
188
- import { readFileSync } from 'fs';
189
-
190
- const dbPassword = readFileSync('/run/secrets/db_password', 'utf8').trim();
191
- ```
192
-
193
- **Impact:** Secrets encrypted at rest, never in images.
194
-
195
- ---
196
-
197
- ### Pattern 4: Service Health Checks
198
-
199
- **Problem:** Dependent services start before dependencies are ready.
200
-
201
- **Solution:** Implement health checks with `depends_on: service_healthy`.
202
-
203
- ```yaml
204
- mongodb:
205
- healthcheck:
206
- test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
207
- interval: 10s
208
-
209
- app:
210
- depends_on:
211
- mongodb:
212
- condition: service_healthy
213
- healthcheck:
214
- test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
215
- ```
216
-
217
- **Impact:** Zero connection errors on startup.
218
-
219
- ---
220
-
221
- ### Pattern 5: Base Image Pinning
222
-
223
- **Problem:** `latest` tag changes between builds, causing non-deterministic deployments.
224
-
225
- **Solution:** Pin to specific versions or digests.
226
-
227
- ```dockerfile
228
- # POOR
229
- FROM oven/bun:latest
230
-
231
- # BETTER
232
- FROM oven/bun:1.1.10-alpine
233
-
234
- # BEST (immutable)
235
- FROM oven/bun:1.1.10-alpine@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c
236
- ```
237
-
238
- **Impact:** Reproducible builds, supply chain integrity.
239
-
240
- ---
241
-
242
- ### Pattern 6: Same-Layer Cleanup
243
-
244
- **Problem:** Cleanup in separate RUN command doesn't reduce image size.
245
-
246
- **Solution:** Install and cleanup in single layer.
247
-
248
- ```dockerfile
249
- # WRONG - Cache persists in layer
250
- RUN bun install
251
- RUN bun cache clean
252
-
253
- # RIGHT - Clean in same layer
254
- RUN bun install --frozen-lockfile && bun cache clean
255
- ```
256
-
257
- **Impact:** 30-50% smaller images.
258
-
259
- ---
260
-
261
- ### Pattern 7: BuildKit Cache Mounts
262
-
263
- **Problem:** Downloading packages repeatedly slows builds.
264
-
265
- **Solution:** Use persistent cache mounts (requires BuildKit).
266
-
267
- ```dockerfile
268
- RUN --mount=type=cache,target=/root/.bun/install/cache \
269
- bun install --frozen-lockfile
270
- ```
271
-
272
- **Impact:** 5x faster dependency installation in CI.
273
-
274
- ---
275
-
276
- ### Pattern 8: Read-Only Filesystem
277
-
278
- **Problem:** Compromised process can modify filesystem.
279
-
280
- **Solution:** Mount container filesystem as read-only.
281
-
282
- ```yaml
283
- services:
284
- app:
285
- read_only: true
286
- tmpfs:
287
- - /tmp # Writable temp directory
288
- ```
289
-
290
- **Impact:** Prevents malware persistence.
291
-
292
- ---
293
-
294
- ### Pattern 9: Resource Limits
295
-
296
- **Problem:** Container can consume all host resources.
297
-
298
- **Solution:** Define CPU and memory limits.
299
-
300
- ```yaml
301
- services:
302
- app:
303
- deploy:
304
- resources:
305
- limits:
306
- cpus: '2.0'
307
- memory: 512M
308
- reservations:
309
- cpus: '0.5'
310
- memory: 256M
311
- ```
312
-
313
- **Impact:** Prevents resource exhaustion attacks.
314
-
315
- ---
316
-
317
- ### Pattern 10: Structured Logging
318
-
319
- **Problem:** Logs difficult to parse and aggregate.
320
-
321
- **Solution:** Output JSON to stdout/stderr.
322
-
323
- ```typescript
324
- import pino from 'pino';
325
-
326
- export const logger = pino({
327
- level: process.env.LOG_LEVEL || 'info',
328
- formatters: {
329
- level: (label) => ({ level: label }),
330
- },
331
- });
332
-
333
- logger.info({ userId: 123, action: 'login' }, 'User logged in');
334
- // Output: {"level":"info","userId":123,"action":"login","msg":"User logged in"}
335
- ```
336
-
337
- **Impact:** Easy integration with log aggregators (Loki, Datadog).
338
-
339
- ---
340
-
341
- ## Production Readiness Checklist
342
-
343
- ### Build Time
344
- - [ ] Multi-stage Dockerfile implemented
345
- - [ ] Base image pinned to specific version
346
- - [ ] Non-root USER directive set
347
- - [ ] .dockerignore excludes secrets and build artifacts
348
- - [ ] Vulnerability scan passes (Trivy, Snyk)
349
- - [ ] Health check endpoint implemented
350
- - [ ] Logs output JSON to stdout
351
-
352
- ### Configuration
353
- - [ ] Secrets use Docker Secrets or external vault
354
- - [ ] Environment variables validated with Zod
355
- - [ ] Resource limits defined (CPU, memory)
356
- - [ ] Restart policy set (`unless-stopped`)
357
- - [ ] Health check configured in Compose
358
-
359
- ### Security
360
- - [ ] Container runs as non-root
361
- - [ ] Read-only filesystem where possible
362
- - [ ] `no-new-privileges` security option set
363
- - [ ] Unnecessary capabilities dropped
364
- - [ ] Network policies defined
365
-
366
- ### Deployment
367
- - [ ] Image tagged with Git SHA or semver
368
- - [ ] CI/CD pipeline builds and scans image
369
- - [ ] Zero-downtime deployment strategy defined
370
- - [ ] Rollback plan documented
371
- - [ ] Monitoring alerts configured
372
-
373
- ---
374
-
375
- ## Common Mistakes
376
-
377
- | Mistake | Why Bad | Fix |
378
- |---------|---------|-----|
379
- | Using `latest` tag | Non-deterministic | Pin to `1.2.3` or digest |
380
- | Secrets in ENV | Visible in logs | Use `/run/secrets/` |
381
- | Running as root | Privilege escalation | Add `USER` directive |
382
- | No health checks | Bad dependency management | Implement `/health` endpoint |
383
- | Large build context | Slow builds, leaks secrets | Create `.dockerignore` |
384
- | Separate cleanup RUN | Layers still contain files | Combine in `&&` chain |
385
- | Missing `depends_on: healthy` | Race conditions | Wait for health checks |
386
- | Hardcoded config | Can't change environments | Use env vars + validation |
387
-
388
- ---
389
-
390
- ## Integration with Project Stack
391
-
392
- ### TypeScript + Bun
393
-
394
- ```dockerfile
395
- # Build stage
396
- FROM oven/bun:1.1.10-alpine AS build
397
- COPY . .
398
- RUN bun run typecheck # Validates types
399
- RUN bun run build # Compiles to dist/
400
-
401
- # Production stage
402
- FROM oven/bun:1.1.10-alpine
403
- COPY --from=build /app/dist ./dist
404
- CMD ["bun", "run", "dist/index.js"]
405
- ```
406
-
407
- ### MongoDB
408
-
409
- ```yaml
410
- mongodb:
411
- image: mongo:7-alpine
412
- volumes:
413
- - mongodb_data:/data/db
414
- secrets:
415
- - mongo_root_password
416
- healthcheck:
417
- test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
418
- ```
419
-
420
- ### Mongoose + Connection Handling
421
-
422
- ```typescript
423
- // src/db/connection.ts
424
- import mongoose from 'mongoose';
425
- import { config } from '../config';
426
-
427
- export async function connectDB() {
428
- await mongoose.connect(config.DATABASE_URL, {
429
- serverSelectionTimeoutMS: 5000,
430
- socketTimeoutMS: 45000,
431
- });
432
-
433
- logger.info('MongoDB connected');
434
- }
435
-
436
- // Graceful shutdown
437
- process.on('SIGTERM', async () => {
438
- await mongoose.connection.close();
439
- process.exit(0);
440
- });
441
- ```
442
-
443
- ---
444
-
445
- ## Tools & Commands
446
-
447
- ### Build with BuildKit
448
-
449
- ```bash
450
- export DOCKER_BUILDKIT=1
451
- docker build --target production -t myapp:latest .
452
- ```
453
-
454
- ### Vulnerability Scanning
455
-
456
- ```bash
457
- # Install Trivy
458
- brew install aquasecurity/trivy/trivy
459
-
460
- # Scan image
461
- trivy image myapp:latest
462
- ```
463
-
464
- ### Image Size Analysis
465
-
466
- ```bash
467
- # Install Dive
468
- brew install dive
469
-
470
- # Explore layers
471
- dive myapp:latest
472
- ```
473
-
474
- ### Health Check Testing
475
-
476
- ```bash
477
- # Start services
478
- docker compose up -d
479
-
480
- # Check health status
481
- docker compose ps
482
- docker inspect myapp | jq '.[0].State.Health'
483
- ```
484
-
485
- ### Secrets Management
486
-
487
- ```bash
488
- # Create secret file
489
- echo "mysecret123" > ./secrets/db_password.txt
490
- chmod 600 ./secrets/db_password.txt
491
-
492
- # Verify secret mount
493
- docker compose exec app cat /run/secrets/db_password
494
- ```
495
-
496
- ---
497
-
498
- ## Debugging
499
-
500
- ### Container Won't Start
501
-
502
- ```bash
503
- # Check logs
504
- docker compose logs app
505
-
506
- # Run shell in container
507
- docker compose run app sh
508
-
509
- # Inspect health check
510
- docker inspect myapp | jq '.[0].State.Health'
511
- ```
512
-
513
- ### Build Cache Issues
514
-
515
- ```bash
516
- # Force rebuild without cache
517
- docker compose build --no-cache
518
-
519
- # Clear all build cache
520
- docker builder prune -a
521
- ```
522
-
523
- ### Permission Errors
524
-
525
- ```bash
526
- # Check user in container
527
- docker compose run app whoami
528
-
529
- # Fix volume permissions
530
- docker compose run app chown -R bun:bun /app/data
531
- ```
532
-
533
- ---
534
-
535
- ## References
536
-
537
- - [Docker Best Practices (Official)](https://docs.docker.com/build/building/best-practices/)
538
- - [OWASP Docker Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html)
539
- - [Bun Docker Guide](https://bun.com/docs/guides/ecosystem/docker)
540
- - [Docker Compose Healthchecks](https://last9.io/blog/docker-compose-health-checks/)
541
- - [Research Cache: docker-patterns-2024.md](.claude/skills/research-cache/cache/docker-patterns-2024.md)
542
-
543
- ---
544
-
545
- ## Version
546
-
547
- - **v1.0.0** (2026-01-03) - Initial skill based on comprehensive 2024-2025 research
1
+ # Docker Patterns - Production-Ready Containerization
2
+
3
+ ## Purpose
4
+
5
+ This skill provides battle-tested Docker patterns and best practices for building, securing, and deploying containerized applications in 2024-2025. Focus on Bun/TypeScript/MongoDB stack with production-grade configurations.
6
+
7
+ ---
8
+
9
+ ## Core Principles
10
+
11
+ ### 1. Multi-Stage Everything
12
+
13
+ - Separate build from runtime
14
+ - Copy only artifacts, not tools
15
+ - 50-80% size reduction
16
+
17
+ ### 2. Security by Default
18
+
19
+ - Non-root users always
20
+ - Secrets via filesystem, never env vars
21
+ - Read-only containers where possible
22
+
23
+ ### 3. Cache Optimization
24
+
25
+ - Dependencies before source code
26
+ - Cleanup in same layer
27
+ - BuildKit cache mounts
28
+
29
+ ### 4. Observable & Resilient
30
+
31
+ - Health checks on all services
32
+ - Structured JSON logging
33
+ - Graceful shutdown handling
34
+
35
+ ---
36
+
37
+ ## Quick Reference
38
+
39
+ ### Multi-Stage Build Template (Bun)
40
+
41
+ ```dockerfile
42
+ # Base
43
+ FROM oven/bun:1.1.10-alpine AS base
44
+ WORKDIR /app
45
+
46
+ # Dependencies
47
+ FROM base AS deps
48
+ COPY package.json bun.lockb ./
49
+ RUN bun install --frozen-lockfile
50
+
51
+ # Build
52
+ FROM base AS build
53
+ COPY --from=deps /app/node_modules ./node_modules
54
+ COPY . .
55
+ RUN bun run typecheck && bun run build
56
+
57
+ # Production
58
+ FROM base AS production
59
+ RUN apk add --no-cache curl
60
+ COPY --from=deps /app/node_modules ./node_modules
61
+ COPY --from=build /app/dist ./dist
62
+ USER bun
63
+ EXPOSE 3000
64
+ HEALTHCHECK CMD curl -f http://localhost:3000/health || exit 1
65
+ CMD ["bun", "run", "dist/index.js"]
66
+ ```
67
+
68
+ ### Docker Compose with Secrets & Health Checks
69
+
70
+ ```yaml
71
+ version: '3.8'
72
+
73
+ services:
74
+ mongodb:
75
+ image: mongo:7-alpine
76
+ secrets:
77
+ - mongo_root_password
78
+ healthcheck:
79
+ test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
80
+ interval: 10s
81
+ timeout: 5s
82
+ retries: 5
83
+ start_period: 30s
84
+
85
+ app:
86
+ build: .
87
+ depends_on:
88
+ mongodb:
89
+ condition: service_healthy
90
+ secrets:
91
+ - mongo_password
92
+ - jwt_secret
93
+ healthcheck:
94
+ test: ['CMD', 'curl', '-f', 'http://localhost:3000/health']
95
+ interval: 30s
96
+ timeout: 10s
97
+ retries: 3
98
+
99
+ secrets:
100
+ mongo_root_password:
101
+ file: ./secrets/mongo_root_password.txt
102
+ mongo_password:
103
+ file: ./secrets/mongo_password.txt
104
+ jwt_secret:
105
+ file: ./secrets/jwt_secret.txt
106
+ ```
107
+
108
+ ### .dockerignore Essentials
109
+
110
+ ```gitignore
111
+ node_modules/
112
+ .bun/
113
+ dist/
114
+ *.log
115
+ .git/
116
+ .env
117
+ .env.*
118
+ !.env.example
119
+ coverage/
120
+ *.test.ts
121
+ .DS_Store
122
+ Dockerfile*
123
+ docker-compose*
124
+ ```
125
+
126
+ ---
127
+
128
+ ## Pattern Catalog
129
+
130
+ ### Pattern 1: Layer Caching Optimization
131
+
132
+ **Problem:** Rebuilds download all dependencies on code changes.
133
+
134
+ **Solution:** Separate dependency installation from code copy.
135
+
136
+ ```dockerfile
137
+ # WRONG - Invalidates cache on any change
138
+ COPY . .
139
+ RUN bun install
140
+
141
+ # RIGHT - Cache dependencies separately
142
+ COPY package.json bun.lockb ./
143
+ RUN bun install --frozen-lockfile
144
+ COPY . .
145
+ ```
146
+
147
+ **Impact:** 10x faster rebuilds during development.
148
+
149
+ ---
150
+
151
+ ### Pattern 2: Non-Root User Security
152
+
153
+ **Problem:** Container runs as root, enabling privilege escalation.
154
+
155
+ **Solution:** Switch to non-privileged user.
156
+
157
+ ```dockerfile
158
+ # Create user (if not exists in base image)
159
+ RUN addgroup --system --gid 1001 nodejs \
160
+ && adduser --system --uid 1001 nodejs
161
+
162
+ USER nodejs
163
+
164
+ # Or use existing user (Bun images)
165
+ USER bun
166
+ ```
167
+
168
+ **Impact:** Prevents container breakout attacks.
169
+
170
+ ---
171
+
172
+ ### Pattern 3: Docker Secrets (Not Env Vars)
173
+
174
+ **Problem:** Environment variables expose secrets in logs and process lists.
175
+
176
+ **Solution:** Use Docker Secrets mounted at `/run/secrets/`.
177
+
178
+ ```yaml
179
+ # docker-compose.yml
180
+ secrets:
181
+ db_password:
182
+ file: ./secrets/db_password.txt
183
+
184
+ services:
185
+ app:
186
+ secrets:
187
+ - db_password
188
+ ```
189
+
190
+ ```typescript
191
+ // src/config.ts
192
+ import { readFileSync } from 'fs';
193
+
194
+ const dbPassword = readFileSync('/run/secrets/db_password', 'utf8').trim();
195
+ ```
196
+
197
+ **Impact:** Secrets encrypted at rest, never in images.
198
+
199
+ ---
200
+
201
+ ### Pattern 4: Service Health Checks
202
+
203
+ **Problem:** Dependent services start before dependencies are ready.
204
+
205
+ **Solution:** Implement health checks with `depends_on: service_healthy`.
206
+
207
+ ```yaml
208
+ mongodb:
209
+ healthcheck:
210
+ test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
211
+ interval: 10s
212
+
213
+ app:
214
+ depends_on:
215
+ mongodb:
216
+ condition: service_healthy
217
+ healthcheck:
218
+ test: ['CMD', 'curl', '-f', 'http://localhost:3000/health']
219
+ ```
220
+
221
+ **Impact:** Zero connection errors on startup.
222
+
223
+ ---
224
+
225
+ ### Pattern 5: Base Image Pinning
226
+
227
+ **Problem:** `latest` tag changes between builds, causing non-deterministic deployments.
228
+
229
+ **Solution:** Pin to specific versions or digests.
230
+
231
+ ```dockerfile
232
+ # POOR
233
+ FROM oven/bun:latest
234
+
235
+ # BETTER
236
+ FROM oven/bun:1.1.10-alpine
237
+
238
+ # BEST (immutable)
239
+ FROM oven/bun:1.1.10-alpine@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c
240
+ ```
241
+
242
+ **Impact:** Reproducible builds, supply chain integrity.
243
+
244
+ ---
245
+
246
+ ### Pattern 6: Same-Layer Cleanup
247
+
248
+ **Problem:** Cleanup in separate RUN command doesn't reduce image size.
249
+
250
+ **Solution:** Install and cleanup in single layer.
251
+
252
+ ```dockerfile
253
+ # WRONG - Cache persists in layer
254
+ RUN bun install
255
+ RUN bun cache clean
256
+
257
+ # RIGHT - Clean in same layer
258
+ RUN bun install --frozen-lockfile && bun cache clean
259
+ ```
260
+
261
+ **Impact:** 30-50% smaller images.
262
+
263
+ ---
264
+
265
+ ### Pattern 7: BuildKit Cache Mounts
266
+
267
+ **Problem:** Downloading packages repeatedly slows builds.
268
+
269
+ **Solution:** Use persistent cache mounts (requires BuildKit).
270
+
271
+ ```dockerfile
272
+ RUN --mount=type=cache,target=/root/.bun/install/cache \
273
+ bun install --frozen-lockfile
274
+ ```
275
+
276
+ **Impact:** 5x faster dependency installation in CI.
277
+
278
+ ---
279
+
280
+ ### Pattern 8: Read-Only Filesystem
281
+
282
+ **Problem:** Compromised process can modify filesystem.
283
+
284
+ **Solution:** Mount container filesystem as read-only.
285
+
286
+ ```yaml
287
+ services:
288
+ app:
289
+ read_only: true
290
+ tmpfs:
291
+ - /tmp # Writable temp directory
292
+ ```
293
+
294
+ **Impact:** Prevents malware persistence.
295
+
296
+ ---
297
+
298
+ ### Pattern 9: Resource Limits
299
+
300
+ **Problem:** Container can consume all host resources.
301
+
302
+ **Solution:** Define CPU and memory limits.
303
+
304
+ ```yaml
305
+ services:
306
+ app:
307
+ deploy:
308
+ resources:
309
+ limits:
310
+ cpus: '2.0'
311
+ memory: 512M
312
+ reservations:
313
+ cpus: '0.5'
314
+ memory: 256M
315
+ ```
316
+
317
+ **Impact:** Prevents resource exhaustion attacks.
318
+
319
+ ---
320
+
321
+ ### Pattern 10: Structured Logging
322
+
323
+ **Problem:** Logs difficult to parse and aggregate.
324
+
325
+ **Solution:** Output JSON to stdout/stderr.
326
+
327
+ ```typescript
328
+ import pino from 'pino';
329
+
330
+ export const logger = pino({
331
+ level: process.env.LOG_LEVEL || 'info',
332
+ formatters: {
333
+ level: (label) => ({ level: label }),
334
+ },
335
+ });
336
+
337
+ logger.info({ userId: 123, action: 'login' }, 'User logged in');
338
+ // Output: {"level":"info","userId":123,"action":"login","msg":"User logged in"}
339
+ ```
340
+
341
+ **Impact:** Easy integration with log aggregators (Loki, Datadog).
342
+
343
+ ---
344
+
345
+ ## Production Readiness Checklist
346
+
347
+ ### Build Time
348
+
349
+ - [ ] Multi-stage Dockerfile implemented
350
+ - [ ] Base image pinned to specific version
351
+ - [ ] Non-root USER directive set
352
+ - [ ] .dockerignore excludes secrets and build artifacts
353
+ - [ ] Vulnerability scan passes (Trivy, Snyk)
354
+ - [ ] Health check endpoint implemented
355
+ - [ ] Logs output JSON to stdout
356
+
357
+ ### Configuration
358
+
359
+ - [ ] Secrets use Docker Secrets or external vault
360
+ - [ ] Environment variables validated with Zod
361
+ - [ ] Resource limits defined (CPU, memory)
362
+ - [ ] Restart policy set (`unless-stopped`)
363
+ - [ ] Health check configured in Compose
364
+
365
+ ### Security
366
+
367
+ - [ ] Container runs as non-root
368
+ - [ ] Read-only filesystem where possible
369
+ - [ ] `no-new-privileges` security option set
370
+ - [ ] Unnecessary capabilities dropped
371
+ - [ ] Network policies defined
372
+
373
+ ### Deployment
374
+
375
+ - [ ] Image tagged with Git SHA or semver
376
+ - [ ] CI/CD pipeline builds and scans image
377
+ - [ ] Zero-downtime deployment strategy defined
378
+ - [ ] Rollback plan documented
379
+ - [ ] Monitoring alerts configured
380
+
381
+ ---
382
+
383
+ ## Common Mistakes
384
+
385
+ | Mistake | Why Bad | Fix |
386
+ | ----------------------------- | -------------------------- | ---------------------------- |
387
+ | Using `latest` tag | Non-deterministic | Pin to `1.2.3` or digest |
388
+ | Secrets in ENV | Visible in logs | Use `/run/secrets/` |
389
+ | Running as root | Privilege escalation | Add `USER` directive |
390
+ | No health checks | Bad dependency management | Implement `/health` endpoint |
391
+ | Large build context | Slow builds, leaks secrets | Create `.dockerignore` |
392
+ | Separate cleanup RUN | Layers still contain files | Combine in `&&` chain |
393
+ | Missing `depends_on: healthy` | Race conditions | Wait for health checks |
394
+ | Hardcoded config | Can't change environments | Use env vars + validation |
395
+
396
+ ---
397
+
398
+ ## Integration with Project Stack
399
+
400
+ ### TypeScript + Bun
401
+
402
+ ```dockerfile
403
+ # Build stage
404
+ FROM oven/bun:1.1.10-alpine AS build
405
+ COPY . .
406
+ RUN bun run typecheck # Validates types
407
+ RUN bun run build # Compiles to dist/
408
+
409
+ # Production stage
410
+ FROM oven/bun:1.1.10-alpine
411
+ COPY --from=build /app/dist ./dist
412
+ CMD ["bun", "run", "dist/index.js"]
413
+ ```
414
+
415
+ ### MongoDB
416
+
417
+ ```yaml
418
+ mongodb:
419
+ image: mongo:7-alpine
420
+ volumes:
421
+ - mongodb_data:/data/db
422
+ secrets:
423
+ - mongo_root_password
424
+ healthcheck:
425
+ test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
426
+ ```
427
+
428
+ ### Mongoose + Connection Handling
429
+
430
+ ```typescript
431
+ // src/db/connection.ts
432
+ import mongoose from 'mongoose';
433
+ import { config } from '../config';
434
+
435
+ export async function connectDB() {
436
+ await mongoose.connect(config.DATABASE_URL, {
437
+ serverSelectionTimeoutMS: 5000,
438
+ socketTimeoutMS: 45000,
439
+ });
440
+
441
+ logger.info('MongoDB connected');
442
+ }
443
+
444
+ // Graceful shutdown
445
+ process.on('SIGTERM', async () => {
446
+ await mongoose.connection.close();
447
+ process.exit(0);
448
+ });
449
+ ```
450
+
451
+ ---
452
+
453
+ ## Tools & Commands
454
+
455
+ ### Build with BuildKit
456
+
457
+ ```bash
458
+ export DOCKER_BUILDKIT=1
459
+ docker build --target production -t myapp:latest .
460
+ ```
461
+
462
+ ### Vulnerability Scanning
463
+
464
+ ```bash
465
+ # Install Trivy
466
+ brew install aquasecurity/trivy/trivy
467
+
468
+ # Scan image
469
+ trivy image myapp:latest
470
+ ```
471
+
472
+ ### Image Size Analysis
473
+
474
+ ```bash
475
+ # Install Dive
476
+ brew install dive
477
+
478
+ # Explore layers
479
+ dive myapp:latest
480
+ ```
481
+
482
+ ### Health Check Testing
483
+
484
+ ```bash
485
+ # Start services
486
+ docker compose up -d
487
+
488
+ # Check health status
489
+ docker compose ps
490
+ docker inspect myapp | jq '.[0].State.Health'
491
+ ```
492
+
493
+ ### Secrets Management
494
+
495
+ ```bash
496
+ # Create secret file
497
+ echo "mysecret123" > ./secrets/db_password.txt
498
+ chmod 600 ./secrets/db_password.txt
499
+
500
+ # Verify secret mount
501
+ docker compose exec app cat /run/secrets/db_password
502
+ ```
503
+
504
+ ---
505
+
506
+ ## Debugging
507
+
508
+ ### Container Won't Start
509
+
510
+ ```bash
511
+ # Check logs
512
+ docker compose logs app
513
+
514
+ # Run shell in container
515
+ docker compose run app sh
516
+
517
+ # Inspect health check
518
+ docker inspect myapp | jq '.[0].State.Health'
519
+ ```
520
+
521
+ ### Build Cache Issues
522
+
523
+ ```bash
524
+ # Force rebuild without cache
525
+ docker compose build --no-cache
526
+
527
+ # Clear all build cache
528
+ docker builder prune -a
529
+ ```
530
+
531
+ ### Permission Errors
532
+
533
+ ```bash
534
+ # Check user in container
535
+ docker compose run app whoami
536
+
537
+ # Fix volume permissions
538
+ docker compose run app chown -R bun:bun /app/data
539
+ ```
540
+
541
+ ---
542
+
543
+ ## References
544
+
545
+ - [Docker Best Practices (Official)](https://docs.docker.com/build/building/best-practices/)
546
+ - [OWASP Docker Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html)
547
+ - [Bun Docker Guide](https://bun.com/docs/guides/ecosystem/docker)
548
+ - [Docker Compose Healthchecks](https://last9.io/blog/docker-compose-health-checks/)
549
+ - [Research Cache: docker-patterns-2024.md](.claude/skills/research-cache/cache/docker-patterns-2024.md)
550
+
551
+ ---
552
+
553
+ ## Version
554
+
555
+ - **v1.0.0** (2026-01-03) - Initial skill based on comprehensive 2024-2025 research