start-vibing 2.0.11 → 2.0.13
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.
- package/README.md +177 -177
- package/dist/cli.js +19 -2
- package/package.json +42 -42
- package/template/.claude/CLAUDE.md +174 -174
- package/template/.claude/agents/01-orchestration/agent-selector.md +130 -130
- package/template/.claude/agents/01-orchestration/checkpoint-manager.md +142 -142
- package/template/.claude/agents/01-orchestration/context-manager.md +138 -138
- package/template/.claude/agents/01-orchestration/error-recovery.md +182 -182
- package/template/.claude/agents/01-orchestration/orchestrator.md +114 -114
- package/template/.claude/agents/01-orchestration/parallel-coordinator.md +141 -141
- package/template/.claude/agents/01-orchestration/task-decomposer.md +121 -121
- package/template/.claude/agents/01-orchestration/workflow-router.md +114 -114
- package/template/.claude/agents/02-typescript/bun-runtime-expert.md +197 -197
- package/template/.claude/agents/02-typescript/esm-resolver.md +193 -193
- package/template/.claude/agents/02-typescript/import-alias-enforcer.md +158 -158
- package/template/.claude/agents/02-typescript/ts-generics-helper.md +183 -183
- package/template/.claude/agents/02-typescript/ts-migration-helper.md +238 -238
- package/template/.claude/agents/02-typescript/ts-strict-checker.md +180 -180
- package/template/.claude/agents/02-typescript/ts-types-analyzer.md +199 -199
- package/template/.claude/agents/02-typescript/type-definition-writer.md +187 -187
- package/template/.claude/agents/02-typescript/zod-schema-designer.md +212 -212
- package/template/.claude/agents/02-typescript/zod-validator.md +158 -158
- package/template/.claude/agents/03-testing/playwright-assertions.md +265 -265
- package/template/.claude/agents/03-testing/playwright-e2e.md +247 -247
- package/template/.claude/agents/03-testing/playwright-fixtures.md +234 -234
- package/template/.claude/agents/03-testing/playwright-multi-viewport.md +256 -256
- package/template/.claude/agents/03-testing/playwright-page-objects.md +247 -247
- package/template/.claude/agents/03-testing/test-cleanup-manager.md +248 -248
- package/template/.claude/agents/03-testing/test-data-generator.md +254 -254
- package/template/.claude/agents/03-testing/tester-integration.md +278 -278
- package/template/.claude/agents/03-testing/tester-unit.md +207 -207
- package/template/.claude/agents/03-testing/vitest-config.md +287 -287
- package/template/.claude/agents/04-docker/container-health.md +255 -255
- package/template/.claude/agents/04-docker/deployment-validator.md +225 -225
- package/template/.claude/agents/04-docker/docker-compose-designer.md +281 -281
- package/template/.claude/agents/04-docker/docker-env-manager.md +235 -235
- package/template/.claude/agents/04-docker/docker-multi-stage.md +241 -241
- package/template/.claude/agents/04-docker/dockerfile-optimizer.md +208 -208
- package/template/.claude/agents/05-database/database-seeder.md +273 -273
- package/template/.claude/agents/05-database/mongodb-query-optimizer.md +230 -230
- package/template/.claude/agents/05-database/mongoose-aggregation.md +306 -306
- package/template/.claude/agents/05-database/mongoose-index-optimizer.md +182 -182
- package/template/.claude/agents/05-database/mongoose-schema-designer.md +267 -267
- package/template/.claude/agents/06-security/auth-session-validator.md +68 -68
- package/template/.claude/agents/06-security/input-sanitizer.md +80 -80
- package/template/.claude/agents/06-security/owasp-checker.md +97 -97
- package/template/.claude/agents/06-security/permission-auditor.md +100 -100
- package/template/.claude/agents/06-security/security-auditor.md +84 -84
- package/template/.claude/agents/06-security/sensitive-data-scanner.md +83 -83
- package/template/.claude/agents/07-documentation/api-documenter.md +136 -136
- package/template/.claude/agents/07-documentation/changelog-manager.md +105 -105
- package/template/.claude/agents/07-documentation/documenter.md +76 -76
- package/template/.claude/agents/07-documentation/domain-updater.md +81 -81
- package/template/.claude/agents/07-documentation/jsdoc-generator.md +114 -114
- package/template/.claude/agents/07-documentation/readme-generator.md +135 -135
- package/template/.claude/agents/08-git/branch-manager.md +58 -58
- package/template/.claude/agents/08-git/commit-manager.md +63 -63
- package/template/.claude/agents/08-git/pr-creator.md +76 -76
- package/template/.claude/agents/09-quality/code-reviewer.md +71 -71
- package/template/.claude/agents/09-quality/quality-checker.md +67 -67
- package/template/.claude/agents/10-research/best-practices-finder.md +89 -89
- package/template/.claude/agents/10-research/competitor-analyzer.md +106 -106
- package/template/.claude/agents/10-research/pattern-researcher.md +93 -93
- package/template/.claude/agents/10-research/research-cache-manager.md +76 -76
- package/template/.claude/agents/10-research/research-web.md +98 -98
- package/template/.claude/agents/10-research/tech-evaluator.md +101 -101
- package/template/.claude/agents/11-ui-ux/accessibility-auditor.md +136 -136
- package/template/.claude/agents/11-ui-ux/design-system-enforcer.md +125 -125
- package/template/.claude/agents/11-ui-ux/skeleton-generator.md +118 -118
- package/template/.claude/agents/11-ui-ux/ui-desktop.md +132 -132
- package/template/.claude/agents/11-ui-ux/ui-mobile.md +98 -98
- package/template/.claude/agents/11-ui-ux/ui-tablet.md +110 -110
- package/template/.claude/agents/12-performance/api-latency-analyzer.md +156 -156
- package/template/.claude/agents/12-performance/bundle-analyzer.md +113 -113
- package/template/.claude/agents/12-performance/memory-leak-detector.md +137 -137
- package/template/.claude/agents/12-performance/performance-profiler.md +115 -115
- package/template/.claude/agents/12-performance/query-optimizer.md +124 -124
- package/template/.claude/agents/12-performance/render-optimizer.md +154 -154
- package/template/.claude/agents/13-debugging/build-error-fixer.md +207 -207
- package/template/.claude/agents/13-debugging/debugger.md +149 -149
- package/template/.claude/agents/13-debugging/error-stack-analyzer.md +141 -141
- package/template/.claude/agents/13-debugging/network-debugger.md +208 -208
- package/template/.claude/agents/13-debugging/runtime-error-fixer.md +181 -181
- package/template/.claude/agents/13-debugging/type-error-resolver.md +185 -185
- package/template/.claude/agents/14-validation/final-validator.md +93 -93
- package/template/.claude/agents/_backup/analyzer.md +134 -134
- package/template/.claude/agents/_backup/code-reviewer.md +279 -279
- package/template/.claude/agents/_backup/commit-manager.md +219 -219
- package/template/.claude/agents/_backup/debugger.md +280 -280
- package/template/.claude/agents/_backup/documenter.md +237 -237
- package/template/.claude/agents/_backup/domain-updater.md +197 -197
- package/template/.claude/agents/_backup/final-validator.md +169 -169
- package/template/.claude/agents/_backup/orchestrator.md +149 -149
- package/template/.claude/agents/_backup/performance.md +232 -232
- package/template/.claude/agents/_backup/quality-checker.md +240 -240
- package/template/.claude/agents/_backup/research.md +315 -315
- package/template/.claude/agents/_backup/security-auditor.md +192 -192
- package/template/.claude/agents/_backup/tester.md +566 -566
- package/template/.claude/agents/_backup/ui-ux-reviewer.md +247 -247
- package/template/.claude/config/README.md +30 -30
- package/template/.claude/config/mcp-config.json +344 -344
- package/template/.claude/config/project-config.json +53 -53
- package/template/.claude/config/quality-gates.json +46 -46
- package/template/.claude/config/security-rules.json +45 -45
- package/template/.claude/config/testing-config.json +164 -164
- package/template/.claude/hooks/SETUP.md +126 -126
- package/template/.claude/hooks/run-hook.ts +176 -176
- package/template/.claude/hooks/stop-validator.ts +914 -824
- package/template/.claude/hooks/user-prompt-submit.ts +886 -886
- package/template/.claude/scripts/mcp-quick-install.ts +151 -151
- package/template/.claude/scripts/setup-mcps.ts +651 -651
- package/template/.claude/settings.json +275 -275
- package/template/.claude/skills/bun-runtime/SKILL.md +430 -430
- package/template/.claude/skills/codebase-knowledge/domains/claude-system.md +431 -431
- package/template/.claude/skills/codebase-knowledge/domains/mcp-integration.md +295 -295
- package/template/.claude/skills/debugging-patterns/SKILL.md +485 -485
- package/template/.claude/skills/docker-patterns/SKILL.md +555 -555
- package/template/.claude/skills/git-workflow/SKILL.md +454 -454
- package/template/.claude/skills/mongoose-patterns/SKILL.md +499 -499
- package/template/.claude/skills/nextjs-app-router/SKILL.md +327 -327
- package/template/.claude/skills/performance-patterns/SKILL.md +547 -547
- package/template/.claude/skills/playwright-automation/SKILL.md +438 -438
- package/template/.claude/skills/react-patterns/SKILL.md +389 -389
- package/template/.claude/skills/research-cache/SKILL.md +222 -222
- package/template/.claude/skills/shadcn-ui/SKILL.md +511 -511
- package/template/.claude/skills/tailwind-patterns/SKILL.md +465 -465
- package/template/.claude/skills/test-coverage/SKILL.md +467 -467
- package/template/.claude/skills/trpc-api/SKILL.md +434 -434
- package/template/.claude/skills/typescript-strict/SKILL.md +367 -367
- package/template/.claude/skills/zod-validation/SKILL.md +403 -403
- package/template/CLAUDE.md +117 -117
|
@@ -1,555 +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
|
-
|
|
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
|
|
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
|