@umacloud/knowledge 1.0.1

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 (418) hide show
  1. package/00-governance/governance-capabilities.md +557 -0
  2. package/00-governance/knowledge-map.md +39 -0
  3. package/00-governance/maintenance-policy.md +76 -0
  4. package/00-governance/review-checklist.md +81 -0
  5. package/README.md +13 -0
  6. package/ai/01-standards/agent-development-complete.md +691 -0
  7. package/ai/01-standards/llm-application-complete.md +488 -0
  8. package/ai/01-standards/mlops-complete.md +798 -0
  9. package/ai/01-standards/prompt-engineering-complete.md +646 -0
  10. package/ai/01-standards/rag-architecture-complete.md +649 -0
  11. package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
  12. package/ai/03-checklists/ai-project-checklist.md +215 -0
  13. package/ai/04-antipatterns/ai-antipatterns.md +661 -0
  14. package/ai/05-cases/case-rag-production.md +147 -0
  15. package/ai/06-glossary/ai-glossary.md +162 -0
  16. package/ai/agent-evaluation-benchmark.md +53 -0
  17. package/ai/ai-agent-memory-context-management.md +41 -0
  18. package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
  19. package/ai/ai-data-security-and-compliance-playbook.md +37 -0
  20. package/ai/ai-domain-index-and-checklist.md +40 -0
  21. package/ai/ai-governance-maturity-model.md +50 -0
  22. package/ai/ai-model-selection-and-routing-strategy.md +47 -0
  23. package/ai/ai-observability-and-oncall-runbook.md +52 -0
  24. package/ai/ai-rag-engineering-playbook.md +42 -0
  25. package/ai/ai-red-team-and-safety-evaluation.md +42 -0
  26. package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
  27. package/ai/llm-agent-engineering-deep-dive.md +57 -0
  28. package/ai/prompt-and-tool-guardrails.md +52 -0
  29. package/api/01-standards/enterprise-api-standards.md +198 -0
  30. package/api/01-standards/rest-api-design-guide.md +63 -0
  31. package/api/02-playbooks/api-pagination-playbook.md +93 -0
  32. package/api/02-playbooks/graphql-production-playbook.md +176 -0
  33. package/api/03-checklists/api-review-checklist.md +55 -0
  34. package/api/04-antipatterns/api-antipatterns.md +112 -0
  35. package/architecture/01-standards/api-gateway-patterns.md +496 -0
  36. package/architecture/01-standards/cloud-native-patterns.md +644 -0
  37. package/architecture/01-standards/distributed-systems-patterns.md +591 -0
  38. package/architecture/01-standards/event-driven-architecture.md +595 -0
  39. package/architecture/01-standards/microservices-patterns-complete.md +968 -0
  40. package/architecture/01-standards/microservices-patterns.md +495 -0
  41. package/architecture/01-standards/system-design-interview.md +664 -0
  42. package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
  43. package/architecture/02-playbooks/migration-playbook.md +780 -0
  44. package/architecture/02-playbooks/system-design-playbook.md +779 -0
  45. package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
  46. package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
  47. package/architecture/05-cases/case-netflix-microservices.md +413 -0
  48. package/architecture/06-glossary/architecture-glossary.md +164 -0
  49. package/architecture/adr-template-and-examples.md +38 -0
  50. package/architecture/api-gateway-deep-dive.md +1291 -0
  51. package/architecture/configuration-management.md +1162 -0
  52. package/architecture/distributed-transactions.md +1220 -0
  53. package/architecture/microservices-complete.md +735 -0
  54. package/architecture/resilience-and-disaster-patterns.md +37 -0
  55. package/architecture/service-governance.md +1198 -0
  56. package/architecture/system-architecture-deep-dive.md +37 -0
  57. package/backend/01-standards/analytics-and-growth.md +65 -0
  58. package/backend/01-standards/api-and-error-conventions.md +120 -0
  59. package/backend/01-standards/application-layering-and-packaging.md +160 -0
  60. package/backend/01-standards/auth-implementation.md +104 -0
  61. package/backend/01-standards/backend-framework-idioms.md +74 -0
  62. package/backend/01-standards/background-jobs-and-async.md +66 -0
  63. package/backend/01-standards/caching-strategies-complete.md +390 -0
  64. package/backend/01-standards/config-and-observability.md +77 -0
  65. package/backend/01-standards/data-modeling-and-persistence.md +94 -0
  66. package/backend/01-standards/django-complete.md +1765 -0
  67. package/backend/01-standards/email-and-notifications.md +64 -0
  68. package/backend/01-standards/fastapi-complete.md +925 -0
  69. package/backend/01-standards/file-upload-and-storage.md +66 -0
  70. package/backend/01-standards/graphql-api-complete.md +416 -0
  71. package/backend/01-standards/llm-application-standard.md +78 -0
  72. package/backend/01-standards/message-queue-patterns.md +379 -0
  73. package/backend/01-standards/microservices-and-distributed.md +78 -0
  74. package/backend/01-standards/nestjs-complete.md +2167 -0
  75. package/backend/01-standards/payment-integration.md +80 -0
  76. package/backend/01-standards/rate-limiting-complete.md +451 -0
  77. package/backend/01-standards/realtime-and-websocket.md +65 -0
  78. package/backend/01-standards/search-and-filtering.md +64 -0
  79. package/backend/01-standards/spring-boot-complete.md +445 -0
  80. package/backend/02-playbooks/api-design-playbook.md +718 -0
  81. package/backend/02-playbooks/email-send-playbook.md +130 -0
  82. package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
  83. package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
  84. package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
  85. package/backend/03-checklists/api-launch-checklist.md +189 -0
  86. package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
  87. package/blockchain/01-standards/blockchain-basics.md +557 -0
  88. package/blockchain/01-standards/smart-contract-development.md +1315 -0
  89. package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
  90. package/cicd/01-standards/github-actions-complete.md +473 -0
  91. package/cicd/01-standards/release-and-store-submission.md +75 -0
  92. package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
  93. package/cicd/02-playbooks/release-management-playbook.md +605 -0
  94. package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
  95. package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
  96. package/cicd/05-cases/case-deployment-automation.md +221 -0
  97. package/cicd/05-cases/case-gitops-transformation.md +212 -0
  98. package/cicd/06-glossary/cicd-glossary.md +114 -0
  99. package/cicd/cicd-blueprint-deep-dive.md +38 -0
  100. package/cicd/release-readiness-gate.md +37 -0
  101. package/cloud-native/01-standards/container-security.md +741 -0
  102. package/cloud-native/01-standards/kubernetes-complete.md +812 -0
  103. package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
  104. package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
  105. package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
  106. package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
  107. package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
  108. package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
  109. package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
  110. package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
  111. package/cloud-native/03-checklists/container-security-checklist.md +431 -0
  112. package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
  113. package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
  114. package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
  115. package/cloud-native/05-cases/case-k8s-migration.md +478 -0
  116. package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
  117. package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
  118. package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
  119. package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
  120. package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
  121. package/data/01-standards/elasticsearch-complete.md +2098 -0
  122. package/data/01-standards/postgresql-complete.md +1613 -0
  123. package/data/01-standards/redis-complete.md +1527 -0
  124. package/data/02-playbooks/database-optimization-playbook.md +403 -0
  125. package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
  126. package/data/03-checklists/database-launch-checklist.md +187 -0
  127. package/data/04-antipatterns/database-antipatterns.md +873 -0
  128. package/data/05-cases/case-database-migration.md +310 -0
  129. package/data/06-glossary/database-glossary.md +440 -0
  130. package/data/data-governance-and-modeling-deep-dive.md +39 -0
  131. package/data-engineering/01-standards/airflow-complete.md +523 -0
  132. package/data-engineering/01-standards/kafka-complete.md +1521 -0
  133. package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
  134. package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
  135. package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
  136. package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
  137. package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
  138. package/database/01-standards/database-schema-standards.md +147 -0
  139. package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
  140. package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
  141. package/database/02-playbooks/postgresql-production-playbook.md +146 -0
  142. package/database/02-playbooks/redis-caching-playbook.md +117 -0
  143. package/database/03-checklists/database-review-checklist.md +50 -0
  144. package/database/04-antipatterns/database-antipatterns.md +112 -0
  145. package/design/01-standards/ui-design-system-complete.md +423 -0
  146. package/design/02-playbooks/design-handoff-playbook.md +254 -0
  147. package/design/02-playbooks/design-review-playbook.md +388 -0
  148. package/design/03-checklists/design-review-checklist.md +246 -0
  149. package/design/04-antipatterns/design-antipatterns.md +378 -0
  150. package/design/05-cases/case-design-system-adoption.md +328 -0
  151. package/design/06-glossary/design-glossary.md +329 -0
  152. package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
  153. package/design/ux-system-deep-dive.md +38 -0
  154. package/design-systems/00-craft-rules.md +71 -0
  155. package/design-systems/aesthetic-families.md +43 -0
  156. package/design-systems/anti-ai-slop.md +162 -0
  157. package/design-systems/bold-geometric.md +120 -0
  158. package/design-systems/brutalist-bold.md +103 -0
  159. package/design-systems/editorial-clean.md +109 -0
  160. package/design-systems/glass-aurora.md +108 -0
  161. package/design-systems/modern-minimal.md +145 -0
  162. package/design-systems/premium-luxury.md +106 -0
  163. package/design-systems/product-type-design-map.md +48 -0
  164. package/design-systems/soft-warm.md +123 -0
  165. package/design-systems/tech-utility.md +113 -0
  166. package/desktop/01-standards/desktop-app-standard.md +72 -0
  167. package/desktop/01-standards/desktop-design.md +71 -0
  168. package/development/00-governance/document-template.md +41 -0
  169. package/development/01-standards/api-versioning-strategies.md +432 -0
  170. package/development/01-standards/authentication-patterns-complete.md +479 -0
  171. package/development/01-standards/css-architecture-complete.md +550 -0
  172. package/development/01-standards/database-migration-strategies.md +484 -0
  173. package/development/01-standards/elasticsearch-complete.md +347 -0
  174. package/development/01-standards/git-complete.md +371 -0
  175. package/development/01-standards/golang-complete.md +1565 -0
  176. package/development/01-standards/graphql-complete.md +298 -0
  177. package/development/01-standards/javascript-bundlers-complete.md +469 -0
  178. package/development/01-standards/javascript-typescript-complete.md +528 -0
  179. package/development/01-standards/jest-complete.md +275 -0
  180. package/development/01-standards/linux-complete.md +234 -0
  181. package/development/01-standards/logging-observability-complete.md +526 -0
  182. package/development/01-standards/microservices-communication.md +502 -0
  183. package/development/01-standards/mongodb-complete.md +406 -0
  184. package/development/01-standards/oauth2-complete.md +285 -0
  185. package/development/01-standards/performance-optimization-complete.md +289 -0
  186. package/development/01-standards/playwright-complete.md +247 -0
  187. package/development/01-standards/postgresql-complete.md +456 -0
  188. package/development/01-standards/pytest-complete.md +340 -0
  189. package/development/01-standards/python-async-programming.md +902 -0
  190. package/development/01-standards/python-complete.md +956 -0
  191. package/development/01-standards/python-decorators-complete.md +799 -0
  192. package/development/01-standards/python-design-patterns.md +2854 -0
  193. package/development/01-standards/python-packaging-distribution.md +420 -0
  194. package/development/01-standards/python-testing-strategies.md +607 -0
  195. package/development/01-standards/python-web-frameworks-comparison.md +471 -0
  196. package/development/01-standards/redis-complete.md +317 -0
  197. package/development/01-standards/rest-api-complete.md +316 -0
  198. package/development/01-standards/rust-complete.md +578 -0
  199. package/development/01-standards/typescript-advanced-types.md +1513 -0
  200. package/development/01-standards/web-security-complete.md +292 -0
  201. package/development/02-playbooks/api-design-playbook.md +810 -0
  202. package/development/02-playbooks/database-migration-playbook.md +580 -0
  203. package/development/02-playbooks/debugging-playbook.md +692 -0
  204. package/development/02-playbooks/feature-delivery-playbook.md +430 -0
  205. package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
  206. package/development/02-playbooks/performance-optimization-playbook.md +531 -0
  207. package/development/02-playbooks/performance-tuning-playbook.md +652 -0
  208. package/development/02-playbooks/refactor-playbook.md +403 -0
  209. package/development/02-playbooks/release-playbook.md +469 -0
  210. package/development/03-checklists/architecture-review-checklist.md +168 -0
  211. package/development/03-checklists/data-migration-checklist.md +157 -0
  212. package/development/03-checklists/oncall-handover-checklist.md +173 -0
  213. package/development/03-checklists/pr-checklist.md +158 -0
  214. package/development/03-checklists/production-readiness-checklist.md +190 -0
  215. package/development/03-checklists/release-readiness-checklist.md +154 -0
  216. package/development/03-checklists/security-review-checklist.md +182 -0
  217. package/development/04-antipatterns/api-antipatterns.md +657 -0
  218. package/development/04-antipatterns/architecture-antipatterns.md +686 -0
  219. package/development/04-antipatterns/backend-antipatterns.md +648 -0
  220. package/development/04-antipatterns/cicd-antipatterns.md +540 -0
  221. package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
  222. package/development/04-antipatterns/data-antipatterns.md +658 -0
  223. package/development/04-antipatterns/database-antipatterns.md +578 -0
  224. package/development/04-antipatterns/frontend-antipatterns.md +635 -0
  225. package/development/04-antipatterns/reliability-antipatterns.md +700 -0
  226. package/development/04-antipatterns/security-antipatterns.md +747 -0
  227. package/development/05-cases/case-api-version-migration.md +428 -0
  228. package/development/05-cases/case-authorization-hardening.md +383 -0
  229. package/development/05-cases/case-bluegreen-rollback.md +466 -0
  230. package/development/05-cases/case-cache-snowball-protection.md +485 -0
  231. package/development/05-cases/case-ci-cd-pipeline.md +544 -0
  232. package/development/05-cases/case-database-scaling.md +500 -0
  233. package/development/05-cases/case-db-hotspot-optimization.md +487 -0
  234. package/development/05-cases/case-incident-mttr-reduction.md +563 -0
  235. package/development/05-cases/case-microservice-migration.md +375 -0
  236. package/development/05-cases/case-performance-optimization.md +406 -0
  237. package/development/05-cases/case-security-incident-response.md +345 -0
  238. package/development/06-glossary/full-stack-glossary.md +166 -0
  239. package/development/09-maturity/quarterly-audit-template.md +35 -0
  240. package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
  241. package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
  242. package/development/12-scenarios/development-scenarios-guide.md +565 -0
  243. package/development/13-implementation-assets/implementation-toolkit.md +282 -0
  244. package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
  245. package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
  246. package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
  247. package/development/api-contract-and-versioning-guide.md +36 -0
  248. package/development/api-governance-complete.md +43 -0
  249. package/development/backend-engineering-complete.md +43 -0
  250. package/development/code-review-quality-complete.md +43 -0
  251. package/development/concurrency-reliability-complete.md +43 -0
  252. package/development/database-engineering-complete.md +43 -0
  253. package/development/engineering-effectiveness-complete.md +43 -0
  254. package/development/engineering-standards-deep-dive.md +38 -0
  255. package/development/frontend-engineering-complete.md +43 -0
  256. package/development/performance-capacity-complete.md +43 -0
  257. package/development/refactor-migration-complete.md +42 -0
  258. package/development/refactoring-and-techdebt-playbook.md +37 -0
  259. package/development/security-in-development-complete.md +43 -0
  260. package/devops/01-standards/cicd-pipeline-complete.md +262 -0
  261. package/devops/01-standards/docker-complete.md +1490 -0
  262. package/devops/01-standards/github-actions-complete.md +337 -0
  263. package/devops/01-standards/kubernetes-complete.md +638 -0
  264. package/devops/01-standards/terraform-complete.md +2117 -0
  265. package/devops/02-playbooks/docker-compose-playbook.md +233 -0
  266. package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
  267. package/devops/02-playbooks/docker-production-playbook.md +952 -0
  268. package/edge-iot/01-standards/edge-iot-complete.md +473 -0
  269. package/experts/architect/api-design.md +178 -0
  270. package/experts/architect/methodology.md +124 -0
  271. package/experts/architect/security.md +75 -0
  272. package/experts/backend-lead/methodology.md +216 -0
  273. package/experts/devops/methodology.md +160 -0
  274. package/experts/frontend-lead/methodology.md +178 -0
  275. package/experts/product-manager/industry/ecommerce.md +43 -0
  276. package/experts/product-manager/industry/saas.md +40 -0
  277. package/experts/product-manager/methodology.md +97 -0
  278. package/experts/qa-lead/methodology.md +123 -0
  279. package/experts/qa-lead/test-strategy.md +128 -0
  280. package/experts/uiux-designer/methodology.md +125 -0
  281. package/frontend/01-standards/accessibility-complete.md +532 -0
  282. package/frontend/01-standards/accessibility-standard.md +74 -0
  283. package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
  284. package/frontend/01-standards/design-tokens-complete.md +444 -0
  285. package/frontend/01-standards/forms-and-validation.md +77 -0
  286. package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
  287. package/frontend/01-standards/i18n-and-localization.md +65 -0
  288. package/frontend/01-standards/nextjs-complete.md +451 -0
  289. package/frontend/01-standards/react-complete.md +713 -0
  290. package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
  291. package/frontend/01-standards/react-hooks-complete.md +1171 -0
  292. package/frontend/01-standards/seo-and-web-vitals.md +77 -0
  293. package/frontend/01-standards/state-management-complete.md +444 -0
  294. package/frontend/01-standards/vue-complete.md +499 -0
  295. package/frontend/01-standards/vue3-complete.md +2002 -0
  296. package/frontend/01-standards/web-framework-best-practices.md +64 -0
  297. package/frontend/01-standards/web-performance-complete.md +495 -0
  298. package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
  299. package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
  300. package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
  301. package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
  302. package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
  303. package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
  304. package/frontend/03-checklists/component-quality-checklist.md +166 -0
  305. package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
  306. package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
  307. package/frontend/05-cases/case-performance-optimization.md +274 -0
  308. package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
  309. package/harmony/01-standards/harmonyos-design.md +65 -0
  310. package/high-quality-engineering-playbook.md +54 -0
  311. package/incident/01-standards/incident-response-complete.md +303 -0
  312. package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
  313. package/incident/02-playbooks/postmortem-playbook.md +398 -0
  314. package/incident/03-checklists/incident-readiness-checklist.md +181 -0
  315. package/incident/04-antipatterns/incident-antipatterns.md +490 -0
  316. package/incident/05-cases/case-cascade-failure.md +176 -0
  317. package/incident/06-glossary/incident-glossary.md +114 -0
  318. package/incident/postmortem-and-response-deep-dive.md +39 -0
  319. package/industries/ecommerce/ecommerce-complete.md +631 -0
  320. package/industries/education/education-complete.md +555 -0
  321. package/industries/fintech/fintech-complete.md +501 -0
  322. package/industries/gaming/gaming-complete.md +587 -0
  323. package/industries/healthcare/healthcare-complete.md +452 -0
  324. package/low-code/01-standards/low-code-complete.md +944 -0
  325. package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
  326. package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
  327. package/miniprogram/01-standards/miniprogram-design.md +61 -0
  328. package/miniprogram/01-standards/miniprogram-standard.md +81 -0
  329. package/mobile/01-standards/android-material-design.md +70 -0
  330. package/mobile/01-standards/flutter-complete.md +384 -0
  331. package/mobile/01-standards/ios-design-hig.md +78 -0
  332. package/mobile/01-standards/mobile-app-standard.md +85 -0
  333. package/mobile/01-standards/react-native-complete.md +352 -0
  334. package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
  335. package/mobile/02-playbooks/mobile-performance.md +473 -0
  336. package/mobile/03-checklists/mobile-release-checklist.md +234 -0
  337. package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
  338. package/mobile/05-cases/case-app-performance.md +500 -0
  339. package/mobile/05-cases/case-app-startup-optimization.md +218 -0
  340. package/mobile/06-glossary/mobile-glossary.md +484 -0
  341. package/observability/01-standards/observability-standards.md +103 -0
  342. package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
  343. package/observability/02-playbooks/structured-logging-playbook.md +73 -0
  344. package/observability/03-checklists/observability-checklist.md +54 -0
  345. package/observability/04-antipatterns/observability-antipatterns.md +106 -0
  346. package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
  347. package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
  348. package/operations/03-checklists/production-launch-checklist.md +365 -0
  349. package/operations/04-antipatterns/operations-antipatterns.md +664 -0
  350. package/operations/05-cases/case-sre-practices.md +581 -0
  351. package/operations/06-glossary/operations-glossary.md +120 -0
  352. package/operations/aiops-anomaly-detection.md +758 -0
  353. package/operations/capacity-planning.md +1061 -0
  354. package/operations/chaos-engineering.md +659 -0
  355. package/operations/incident-command-system.md +38 -0
  356. package/operations/observability-complete.md +442 -0
  357. package/operations/slo-sli-playbook.md +517 -0
  358. package/operations/sre-operations-deep-dive.md +39 -0
  359. package/package.json +8 -0
  360. package/performance/01-standards/performance-and-scalability.md +80 -0
  361. package/performance/01-standards/performance-standards.md +156 -0
  362. package/performance/02-playbooks/query-optimization-playbook.md +103 -0
  363. package/performance/03-checklists/performance-checklist.md +56 -0
  364. package/performance/04-antipatterns/performance-antipatterns.md +146 -0
  365. package/product/01-standards/product-management-complete.md +285 -0
  366. package/product/02-playbooks/feature-launch-playbook.md +207 -0
  367. package/product/02-playbooks/user-research-playbook.md +532 -0
  368. package/product/03-checklists/feature-launch-checklist.md +275 -0
  369. package/product/04-antipatterns/product-antipatterns.md +355 -0
  370. package/product/05-cases/case-mvp-to-scale.md +384 -0
  371. package/product/06-glossary/product-glossary.md +462 -0
  372. package/product/feature-prioritization-framework.md +40 -0
  373. package/product/kpi-and-metric-tree.md +37 -0
  374. package/product/product-discovery-and-prd-deep-dive.md +41 -0
  375. package/quantum/01-standards/quantum-complete.md +1186 -0
  376. package/security/01-standards/api-security-complete.md +511 -0
  377. package/security/01-standards/container-runtime-security.md +574 -0
  378. package/security/01-standards/data-protection-gdpr.md +543 -0
  379. package/security/01-standards/owasp-top10-complete.md +1890 -0
  380. package/security/01-standards/secure-coding-baseline.md +90 -0
  381. package/security/01-standards/supply-chain-security.md +441 -0
  382. package/security/01-standards/web-security-checklist.md +108 -0
  383. package/security/01-standards/zero-trust-architecture.md +521 -0
  384. package/security/02-playbooks/auth-sso-playbook.md +166 -0
  385. package/security/02-playbooks/incident-response-security-playbook.md +588 -0
  386. package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
  387. package/security/02-playbooks/payment-integration-playbook.md +119 -0
  388. package/security/02-playbooks/penetration-testing-playbook.md +517 -0
  389. package/security/03-checklists/security-audit-checklist.md +356 -0
  390. package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
  391. package/security/05-cases/case-log4shell-incident.md +537 -0
  392. package/security/05-cases/case-major-breaches.md +468 -0
  393. package/security/06-glossary/security-glossary.md +212 -0
  394. package/security/compliance-automation.md +993 -0
  395. package/security/container-security.md +680 -0
  396. package/security/devsecops-complete.md +426 -0
  397. package/security/sast-dast-sca.md +775 -0
  398. package/security/secrets-management.md +594 -0
  399. package/security/security-architecture-deep-dive.md +37 -0
  400. package/security/threat-modeling-stride-playbook.md +40 -0
  401. package/seed-templates/auth-system.md +59 -0
  402. package/seed-templates/blog-content.md +94 -0
  403. package/seed-templates/dashboard.md +89 -0
  404. package/seed-templates/docs-site.md +73 -0
  405. package/seed-templates/e-commerce.md +50 -0
  406. package/seed-templates/saas-landing.md +92 -0
  407. package/seed-templates/settings-page.md +51 -0
  408. package/testing/01-standards/test-strategy-and-layering.md +83 -0
  409. package/testing/01-standards/testing-strategy-complete.md +422 -0
  410. package/testing/01-standards/unit-testing-best-practices.md +118 -0
  411. package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
  412. package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
  413. package/testing/03-checklists/test-strategy-checklist.md +208 -0
  414. package/testing/04-antipatterns/testing-antipatterns.md +718 -0
  415. package/testing/05-cases/case-testing-transformation.md +300 -0
  416. package/testing/06-glossary/testing-glossary.md +110 -0
  417. package/testing/risk-based-test-matrix.md +36 -0
  418. package/testing/testing-strategy-deep-dive.md +37 -0
@@ -0,0 +1,1490 @@
1
+ ---
2
+ id: docker-complete
3
+ title: Docker 完整指南
4
+ domain: devops
5
+ category: 01-standards
6
+ difficulty: intermediate
7
+ tags: [complete, compose, devops, docker, dockerfile, 实战, 最佳实践, 核心概念]
8
+ quality_score: 70
9
+ last_updated: 2026-06-15
10
+ ---
11
+ # Docker 完整指南
12
+
13
+ > 文档版本: v1.0 | 最后更新: 2026-03-28 | 适用范围: Docker 24.x / 25.x + Compose V2
14
+
15
+ ---
16
+
17
+ ## 目录
18
+
19
+ 1. [概述](#概述)
20
+ 2. [核心概念](#核心概念)
21
+ 3. [Dockerfile 最佳实践](#dockerfile-最佳实践)
22
+ 4. [Docker Compose 实战](#docker-compose-实战)
23
+ 5. [网络模式详解](#网络模式详解)
24
+ 6. [存储卷管理](#存储卷管理)
25
+ 7. [安全加固](#安全加固)
26
+ 8. [性能优化](#性能优化)
27
+ 9. [生产部署](#生产部署)
28
+ 10. [监控与日志](#监控与日志)
29
+ 11. [常见陷阱与反模式](#常见陷阱与反模式)
30
+ 12. [故障排查 Playbook](#故障排查-playbook)
31
+ 13. [Agent Checklist](#agent-checklist)
32
+
33
+ ---
34
+
35
+ ## 概述
36
+
37
+ Docker 是业界标准的容器化平台,将应用程序及其全部依赖打包为轻量级、可移植的容器镜像。与虚拟机不同,容器共享宿主机内核,启动时间以毫秒计,资源开销极低。
38
+
39
+ **核心价值**:
40
+ - **环境一致性**:开发、测试、生产使用同一镜像,消除 "在我机器上能跑" 问题
41
+ - **资源效率**:容器比 VM 轻量 10-100 倍,单台主机可运行数百个容器
42
+ - **快速交付**:镜像构建秒级完成,CI/CD 流水线提速显著
43
+ - **微服务基石**:每个服务独立容器化,独立部署、独立扩缩
44
+
45
+ **适用场景**:
46
+ - Web 应用 / API 服务的标准化部署
47
+ - 微服务架构下的服务编排
48
+ - CI/CD 流水线中的构建与测试隔离
49
+ - 本地开发环境的快速搭建
50
+
51
+ **不适用场景**:
52
+ - 需要不同内核版本的工作负载(应使用 VM)
53
+ - GUI 密集型桌面应用
54
+ - 对实时性有极高要求的嵌入式系统
55
+
56
+ ---
57
+
58
+ ## 核心概念
59
+
60
+ ### 镜像 (Image)
61
+
62
+ 镜像是只读的分层文件系统模板,包含运行应用所需的一切:代码、运行时、库、环境变量、配置文件。
63
+
64
+ ```bash
65
+ # 拉取官方镜像
66
+ docker pull python:3.12-slim
67
+
68
+ # 查看本地镜像
69
+ docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
70
+
71
+ # 查看镜像层结构
72
+ docker history python:3.12-slim
73
+
74
+ # 导出/导入镜像(离线环境迁移)
75
+ docker save -o python-slim.tar python:3.12-slim
76
+ docker load -i python-slim.tar
77
+
78
+ # 删除悬空镜像(无标签的中间层)
79
+ docker image prune -f
80
+
81
+ # 删除所有未使用的镜像
82
+ docker image prune -a -f
83
+ ```
84
+
85
+ **镜像命名规范**:
86
+ ```
87
+ <registry>/<namespace>/<repository>:<tag>
88
+ 例如: harbor.company.com/backend/user-service:v2.1.5-amd64
89
+ ```
90
+
91
+ 标签规则:
92
+ - 永远不要在生产环境使用 `latest` 标签
93
+ - 使用语义化版本: `v1.2.3`
94
+ - 附加构建元信息: `v1.2.3-abc1234`(git short hash)
95
+ - 多架构时附加平台: `v1.2.3-arm64`
96
+
97
+ ### 容器 (Container)
98
+
99
+ 容器是镜像的运行实例,拥有可写层、网络配置、挂载卷等运行时状态。
100
+
101
+ ```bash
102
+ # 运行容器(前台)
103
+ docker run --rm -it python:3.12-slim python
104
+
105
+ # 运行容器(后台)
106
+ docker run -d --name my-api \
107
+ -p 8080:8000 \
108
+ --restart unless-stopped \
109
+ --memory 512m \
110
+ --cpus 1.0 \
111
+ my-api:v1.0.0
112
+
113
+ # 查看运行中的容器
114
+ docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
115
+
116
+ # 进入运行中的容器
117
+ docker exec -it my-api /bin/sh
118
+
119
+ # 查看容器日志(最近 100 行 + 实时跟踪)
120
+ docker logs --tail 100 -f my-api
121
+
122
+ # 查看容器资源使用
123
+ docker stats --no-stream
124
+
125
+ # 优雅停止容器(先 SIGTERM,10 秒后 SIGKILL)
126
+ docker stop --time 10 my-api
127
+
128
+ # 复制文件进出容器
129
+ docker cp ./config.yaml my-api:/app/config.yaml
130
+ docker cp my-api:/app/logs/error.log ./error.log
131
+ ```
132
+
133
+ ### 网络 (Network)
134
+
135
+ Docker 提供多种网络驱动,容器间通过网络进行通信。
136
+
137
+ ```bash
138
+ # 创建自定义桥接网络
139
+ docker network create --driver bridge app-net
140
+
141
+ # 查看网络详情
142
+ docker network inspect app-net
143
+
144
+ # 将容器连接到网络
145
+ docker network connect app-net my-api
146
+
147
+ # 容器间通过容器名通信(自定义桥接网络自动 DNS)
148
+ docker run -d --name db --network app-net postgres:16
149
+ docker run -d --name api --network app-net my-api:v1.0.0
150
+ # api 容器中可直接使用 "db" 作为主机名连接数据库
151
+ ```
152
+
153
+ ### 存储 (Volume)
154
+
155
+ Docker 卷是持久化数据的推荐方式,生命周期独立于容器。
156
+
157
+ ```bash
158
+ # 创建命名卷
159
+ docker volume create pg-data
160
+
161
+ # 挂载命名卷
162
+ docker run -d --name db \
163
+ -v pg-data:/var/lib/postgresql/data \
164
+ postgres:16
165
+
166
+ # 挂载主机目录(bind mount,开发用)
167
+ docker run -d --name api \
168
+ -v $(pwd)/src:/app/src:ro \
169
+ my-api:v1.0.0
170
+
171
+ # 查看卷信息
172
+ docker volume inspect pg-data
173
+
174
+ # 清理未使用的卷
175
+ docker volume prune -f
176
+ ```
177
+
178
+ ---
179
+
180
+ ## Dockerfile 最佳实践
181
+
182
+ ### 多阶段构建
183
+
184
+ 多阶段构建是减小最终镜像体积的最有效手段,将构建依赖与运行时依赖分离。
185
+
186
+ ```dockerfile
187
+ # ============ 阶段 1: 构建 ============
188
+ FROM node:20-alpine AS builder
189
+
190
+ WORKDIR /build
191
+
192
+ # 先复制依赖清单,利用缓存
193
+ COPY package.json package-lock.json ./
194
+ RUN npm ci --prefer-offline
195
+
196
+ # 再复制源码
197
+ COPY . .
198
+ RUN npm run build
199
+
200
+ # ============ 阶段 2: 运行 ============
201
+ FROM node:20-alpine AS runner
202
+
203
+ # 安全:创建非 root 用户
204
+ RUN addgroup -g 1001 appgroup && \
205
+ adduser -u 1001 -G appgroup -D appuser
206
+
207
+ WORKDIR /app
208
+
209
+ # 只复制运行时需要的产物
210
+ COPY --from=builder --chown=appuser:appgroup /build/dist ./dist
211
+ COPY --from=builder --chown=appuser:appgroup /build/node_modules ./node_modules
212
+ COPY --from=builder --chown=appuser:appgroup /build/package.json ./
213
+
214
+ # 健康检查
215
+ HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
216
+ CMD wget -qO- http://localhost:3000/health || exit 1
217
+
218
+ USER appuser
219
+ EXPOSE 3000
220
+
221
+ CMD ["node", "dist/server.js"]
222
+ ```
223
+
224
+ **Python 多阶段构建**:
225
+
226
+ ```dockerfile
227
+ # ============ 阶段 1: 构建 wheel ============
228
+ FROM python:3.12-slim AS builder
229
+
230
+ RUN apt-get update && apt-get install -y --no-install-recommends \
231
+ build-essential gcc libpq-dev \
232
+ && rm -rf /var/lib/apt/lists/*
233
+
234
+ WORKDIR /build
235
+ COPY requirements.txt .
236
+ RUN pip wheel --no-cache-dir --wheel-dir /build/wheels -r requirements.txt
237
+
238
+ # ============ 阶段 2: 运行 ============
239
+ FROM python:3.12-slim AS runner
240
+
241
+ RUN apt-get update && apt-get install -y --no-install-recommends \
242
+ libpq5 curl \
243
+ && rm -rf /var/lib/apt/lists/*
244
+
245
+ RUN groupadd -g 1001 appgroup && \
246
+ useradd -u 1001 -g appgroup -m appuser
247
+
248
+ WORKDIR /app
249
+
250
+ COPY --from=builder /build/wheels /tmp/wheels
251
+ RUN pip install --no-cache-dir --no-index --find-links=/tmp/wheels /tmp/wheels/* \
252
+ && rm -rf /tmp/wheels
253
+
254
+ COPY --chown=appuser:appgroup . .
255
+
256
+ HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
257
+ CMD curl -f http://localhost:8000/health || exit 1
258
+
259
+ USER appuser
260
+ EXPOSE 8000
261
+
262
+ CMD ["gunicorn", "app:create_app()", "-b", "0.0.0.0:8000", "-w", "4", "-k", "uvicorn.workers.UvicornWorker"]
263
+ ```
264
+
265
+ ### 层优化
266
+
267
+ Docker 每条指令创建一层,层越少、变更越靠后,构建缓存命中率越高。
268
+
269
+ **原则**:
270
+ 1. 把变动频率低的指令放前面(系统包安装、依赖安装)
271
+ 2. 把变动频率高的指令放后面(源码复制)
272
+ 3. 合并多条 RUN 减少层数
273
+ 4. 在同一层中清理临时文件
274
+
275
+ ```dockerfile
276
+ # 反模式:每条 RUN 单独一层,且未清理缓存
277
+ RUN apt-get update
278
+ RUN apt-get install -y curl
279
+ RUN apt-get install -y git
280
+ RUN rm -rf /var/lib/apt/lists/*
281
+
282
+ # 正确做法:合并为一层并在同一层清理
283
+ RUN apt-get update && \
284
+ apt-get install -y --no-install-recommends \
285
+ curl \
286
+ git \
287
+ && rm -rf /var/lib/apt/lists/*
288
+ ```
289
+
290
+ ### .dockerignore
291
+
292
+ 始终配置 `.dockerignore` 文件减小构建上下文,加快构建速度并防止敏感文件泄露。
293
+
294
+ ```gitignore
295
+ # 版本控制
296
+ .git
297
+ .gitignore
298
+
299
+ # 依赖目录
300
+ node_modules
301
+ __pycache__
302
+ *.pyc
303
+ .venv
304
+ venv
305
+
306
+ # 构建产物
307
+ dist
308
+ build
309
+ *.egg-info
310
+
311
+ # IDE
312
+ .vscode
313
+ .idea
314
+ *.swp
315
+
316
+ # 环境与密钥
317
+ .env
318
+ .env.*
319
+ *.pem
320
+ *.key
321
+ credentials.json
322
+
323
+ # Docker
324
+ Dockerfile*
325
+ docker-compose*.yml
326
+ .dockerignore
327
+
328
+ # 文档与测试(生产镜像不需要)
329
+ docs/
330
+ tests/
331
+ *.md
332
+ LICENSE
333
+ ```
334
+
335
+ ### 安全原则
336
+
337
+ ```dockerfile
338
+ # 1. 使用确定性基础镜像标签(带 digest 更佳)
339
+ FROM python:3.12.3-slim@sha256:abc123...
340
+
341
+ # 2. 非 root 用户运行(永远不要以 root 运行应用)
342
+ RUN groupadd -r appuser && useradd -r -g appuser appuser
343
+ USER appuser
344
+
345
+ # 3. 只读文件系统(需要写入时单独挂载 tmpfs)
346
+ # docker run --read-only --tmpfs /tmp my-app:v1
347
+
348
+ # 4. 不在镜像中存储密钥
349
+ # 反模式:
350
+ # COPY .env /app/.env
351
+ # ENV DB_PASSWORD=secret123
352
+ # 正确做法:运行时通过 secret/env 注入
353
+
354
+ # 5. 最小化安装包,不安装推荐包
355
+ RUN apt-get install -y --no-install-recommends <package>
356
+
357
+ # 6. 扫描镜像漏洞(构建后执行)
358
+ # docker scout cves my-app:v1
359
+ # trivy image my-app:v1
360
+ ```
361
+
362
+ ---
363
+
364
+ ## Docker Compose 实战
365
+
366
+ ### 完整应用栈示例
367
+
368
+ ```yaml
369
+ # docker-compose.yml
370
+ # Compose V2 格式(不需要 version 字段)
371
+
372
+ services:
373
+ # ---------- 前端 ----------
374
+ frontend:
375
+ build:
376
+ context: ./frontend
377
+ dockerfile: Dockerfile
378
+ args:
379
+ VITE_API_URL: http://localhost:3001
380
+ ports:
381
+ - "3000:80"
382
+ depends_on:
383
+ api:
384
+ condition: service_healthy
385
+ networks:
386
+ - frontend-net
387
+ restart: unless-stopped
388
+
389
+ # ---------- API 服务 ----------
390
+ api:
391
+ build:
392
+ context: ./backend
393
+ dockerfile: Dockerfile
394
+ target: runner
395
+ ports:
396
+ - "3001:3001"
397
+ environment:
398
+ NODE_ENV: production
399
+ DATABASE_URL: postgresql://app:${DB_PASSWORD}@postgres:5432/myapp
400
+ REDIS_URL: redis://redis:6379/0
401
+ depends_on:
402
+ postgres:
403
+ condition: service_healthy
404
+ redis:
405
+ condition: service_healthy
406
+ healthcheck:
407
+ test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
408
+ interval: 15s
409
+ timeout: 5s
410
+ retries: 5
411
+ start_period: 30s
412
+ networks:
413
+ - frontend-net
414
+ - backend-net
415
+ restart: unless-stopped
416
+ deploy:
417
+ resources:
418
+ limits:
419
+ memory: 512M
420
+ cpus: "1.0"
421
+ reservations:
422
+ memory: 256M
423
+ cpus: "0.5"
424
+
425
+ # ---------- 数据库 ----------
426
+ postgres:
427
+ image: postgres:16-alpine
428
+ environment:
429
+ POSTGRES_DB: myapp
430
+ POSTGRES_USER: app
431
+ POSTGRES_PASSWORD: ${DB_PASSWORD}
432
+ volumes:
433
+ - pg-data:/var/lib/postgresql/data
434
+ - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init.sql:ro
435
+ healthcheck:
436
+ test: ["CMD-SHELL", "pg_isready -U app -d myapp"]
437
+ interval: 10s
438
+ timeout: 5s
439
+ retries: 5
440
+ networks:
441
+ - backend-net
442
+ restart: unless-stopped
443
+
444
+ # ---------- 缓存 ----------
445
+ redis:
446
+ image: redis:7-alpine
447
+ command: >
448
+ redis-server
449
+ --maxmemory 128mb
450
+ --maxmemory-policy allkeys-lru
451
+ --appendonly yes
452
+ volumes:
453
+ - redis-data:/data
454
+ healthcheck:
455
+ test: ["CMD", "redis-cli", "ping"]
456
+ interval: 10s
457
+ timeout: 5s
458
+ retries: 5
459
+ networks:
460
+ - backend-net
461
+ restart: unless-stopped
462
+
463
+ # ---------- 反向代理 ----------
464
+ nginx:
465
+ image: nginx:1.27-alpine
466
+ ports:
467
+ - "80:80"
468
+ - "443:443"
469
+ volumes:
470
+ - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
471
+ - ./nginx/certs:/etc/nginx/certs:ro
472
+ depends_on:
473
+ frontend:
474
+ condition: service_started
475
+ api:
476
+ condition: service_healthy
477
+ networks:
478
+ - frontend-net
479
+ restart: unless-stopped
480
+
481
+ volumes:
482
+ pg-data:
483
+ driver: local
484
+ redis-data:
485
+ driver: local
486
+
487
+ networks:
488
+ frontend-net:
489
+ driver: bridge
490
+ backend-net:
491
+ driver: bridge
492
+ internal: true # 后端网络不暴露到宿主机
493
+ ```
494
+
495
+ ### 开发环境覆盖
496
+
497
+ ```yaml
498
+ # docker-compose.override.yml(开发环境自动加载)
499
+ services:
500
+ api:
501
+ build:
502
+ target: builder # 使用包含 devDependencies 的阶段
503
+ environment:
504
+ NODE_ENV: development
505
+ LOG_LEVEL: debug
506
+ volumes:
507
+ - ./backend/src:/app/src:delegated # 热重载
508
+ - /app/node_modules # 防止覆盖容器内的 node_modules
509
+ command: npm run dev
510
+
511
+ frontend:
512
+ volumes:
513
+ - ./frontend/src:/app/src:delegated
514
+ command: npm run dev
515
+ ports:
516
+ - "3000:5173" # Vite 开发服务器端口
517
+
518
+ postgres:
519
+ ports:
520
+ - "5432:5432" # 开发时暴露数据库端口方便调试
521
+
522
+ redis:
523
+ ports:
524
+ - "6379:6379"
525
+ ```
526
+
527
+ ### 常用 Compose 命令
528
+
529
+ ```bash
530
+ # 启动所有服务(后台)
531
+ docker compose up -d
532
+
533
+ # 只启动指定服务及其依赖
534
+ docker compose up -d api postgres redis
535
+
536
+ # 重新构建并启动
537
+ docker compose up -d --build
538
+
539
+ # 查看服务状态
540
+ docker compose ps
541
+
542
+ # 查看所有服务日志
543
+ docker compose logs -f --tail 50
544
+
545
+ # 查看单个服务日志
546
+ docker compose logs -f api
547
+
548
+ # 停止并删除容器(保留卷)
549
+ docker compose down
550
+
551
+ # 停止并删除容器 + 卷 + 网络(危险,数据库数据会丢失)
552
+ docker compose down -v
553
+
554
+ # 水平扩缩(无状态服务)
555
+ docker compose up -d --scale api=3
556
+
557
+ # 执行一次性命令
558
+ docker compose run --rm api npm run db:migrate
559
+ docker compose exec postgres psql -U app -d myapp
560
+ ```
561
+
562
+ ---
563
+
564
+ ## 网络模式详解
565
+
566
+ ### Bridge(桥接,默认)
567
+
568
+ 每个容器分配独立 IP,通过 docker0 虚拟网桥通信。自定义桥接网络支持容器名 DNS 解析。
569
+
570
+ ```bash
571
+ # 创建自定义桥接网络,指定子网
572
+ docker network create \
573
+ --driver bridge \
574
+ --subnet 172.20.0.0/16 \
575
+ --gateway 172.20.0.1 \
576
+ app-net
577
+
578
+ # 容器指定固定 IP(少数场景需要)
579
+ docker run -d --name api \
580
+ --network app-net \
581
+ --ip 172.20.0.10 \
582
+ my-api:v1
583
+ ```
584
+
585
+ ### Host(主机)
586
+
587
+ 容器直接使用宿主机网络栈,无网络隔离,性能最佳。适合高吞吐网络密集型应用。
588
+
589
+ ```bash
590
+ # 容器直接使用宿主机网络(Linux only)
591
+ docker run -d --network host my-api:v1
592
+ # 注意:macOS/Windows 上 host 模式行为不同,不推荐使用
593
+ ```
594
+
595
+ ### None(无网络)
596
+
597
+ 容器无网络接口,适合批处理任务、密码学计算等不需要网络的场景。
598
+
599
+ ```bash
600
+ docker run --rm --network none my-batch-job:v1
601
+ ```
602
+
603
+ ### Overlay(跨主机)
604
+
605
+ 用于 Docker Swarm 或 Kubernetes 等集群环境下跨主机容器通信。
606
+
607
+ ```bash
608
+ # 初始化 Swarm(单节点测试)
609
+ docker swarm init
610
+
611
+ # 创建 overlay 网络
612
+ docker network create --driver overlay --attachable app-overlay
613
+ ```
614
+
615
+ ### 网络隔离策略
616
+
617
+ ```yaml
618
+ # 推荐的网络分层策略
619
+ networks:
620
+ # DMZ 层:前端 + 反向代理
621
+ dmz:
622
+ driver: bridge
623
+ # 应用层:API 服务
624
+ app:
625
+ driver: bridge
626
+ # 数据层:数据库 + 缓存(internal 禁止外部访问)
627
+ data:
628
+ driver: bridge
629
+ internal: true
630
+ ```
631
+
632
+ ---
633
+
634
+ ## 存储卷管理
635
+
636
+ ### 卷类型对比
637
+
638
+ | 类型 | 用途 | 性能 | 持久性 | 生产推荐 |
639
+ |------|------|------|--------|----------|
640
+ | Named Volume | 数据库、持久化数据 | 高 | 持久 | 是 |
641
+ | Bind Mount | 开发热重载 | 中 | 持久 | 否 |
642
+ | tmpfs | 临时文件、缓存 | 极高 | 非持久 | 视场景 |
643
+
644
+ ### 数据库卷管理
645
+
646
+ ```bash
647
+ # 创建带标签的卷
648
+ docker volume create --label project=myapp --label env=prod pg-data
649
+
650
+ # 备份数据库卷
651
+ docker run --rm \
652
+ -v pg-data:/source:ro \
653
+ -v $(pwd)/backup:/backup \
654
+ alpine tar czf /backup/pg-data-$(date +%Y%m%d).tar.gz -C /source .
655
+
656
+ # 恢复数据库卷
657
+ docker run --rm \
658
+ -v pg-data:/target \
659
+ -v $(pwd)/backup:/backup:ro \
660
+ alpine sh -c "cd /target && tar xzf /backup/pg-data-20260328.tar.gz"
661
+
662
+ # 使用 pg_dump 逻辑备份(推荐)
663
+ docker compose exec postgres pg_dump -U app -d myapp -Fc > backup.dump
664
+
665
+ # 恢复逻辑备份
666
+ docker compose exec -T postgres pg_restore -U app -d myapp < backup.dump
667
+ ```
668
+
669
+ ### tmpfs 挂载
670
+
671
+ ```bash
672
+ # 临时文件使用 tmpfs(不写入磁盘,容器停止即丢失)
673
+ docker run -d --name api \
674
+ --tmpfs /tmp:rw,size=100m,mode=1777 \
675
+ --tmpfs /app/cache:rw,size=50m \
676
+ --read-only \
677
+ my-api:v1
678
+ ```
679
+
680
+ ### 卷驱动插件
681
+
682
+ ```bash
683
+ # 使用 NFS 卷(多主机共享)
684
+ docker volume create \
685
+ --driver local \
686
+ --opt type=nfs \
687
+ --opt o=addr=nfs-server.example.com,rw,nfsvers=4 \
688
+ --opt device=:/exports/app-data \
689
+ shared-data
690
+ ```
691
+
692
+ ---
693
+
694
+ ## 安全加固
695
+
696
+ ### 非 root 用户运行
697
+
698
+ ```dockerfile
699
+ # Node.js 应用
700
+ FROM node:20-alpine
701
+
702
+ # 创建专用用户和组
703
+ RUN addgroup -g 1001 -S appgroup && \
704
+ adduser -u 1001 -S appuser -G appgroup
705
+
706
+ # 创建必要目录并设置权限
707
+ RUN mkdir -p /app/logs /app/tmp && \
708
+ chown -R appuser:appgroup /app
709
+
710
+ WORKDIR /app
711
+
712
+ COPY --chown=appuser:appgroup package*.json ./
713
+ RUN npm ci --omit=dev
714
+
715
+ COPY --chown=appuser:appgroup . .
716
+
717
+ # 切换到非 root 用户
718
+ USER appuser
719
+
720
+ CMD ["node", "src/server.js"]
721
+ ```
722
+
723
+ ### 镜像扫描
724
+
725
+ ```bash
726
+ # Docker Scout(Docker Desktop 内置)
727
+ docker scout cves my-api:v1
728
+ docker scout recommendations my-api:v1
729
+
730
+ # Trivy(CI/CD 推荐)
731
+ trivy image --severity HIGH,CRITICAL my-api:v1
732
+
733
+ # Trivy 集成到 CI(失败阈值)
734
+ trivy image --exit-code 1 --severity CRITICAL my-api:v1
735
+
736
+ # Grype
737
+ grype my-api:v1
738
+
739
+ # CI 中的扫描示例(GitHub Actions)
740
+ # - name: Scan image
741
+ # uses: aquasecurity/trivy-action@master
742
+ # with:
743
+ # image-ref: my-api:v1
744
+ # severity: CRITICAL,HIGH
745
+ # exit-code: 1
746
+ ```
747
+
748
+ ### Secrets 管理
749
+
750
+ ```yaml
751
+ # docker-compose.yml 中使用 secrets
752
+ services:
753
+ api:
754
+ image: my-api:v1
755
+ secrets:
756
+ - db_password
757
+ - jwt_secret
758
+ environment:
759
+ # 通过文件路径引用 secret
760
+ DB_PASSWORD_FILE: /run/secrets/db_password
761
+ JWT_SECRET_FILE: /run/secrets/jwt_secret
762
+
763
+ secrets:
764
+ db_password:
765
+ file: ./secrets/db_password.txt # 开发环境
766
+ jwt_secret:
767
+ file: ./secrets/jwt_secret.txt
768
+ ```
769
+
770
+ ```python
771
+ # 应用内读取 secret 文件的通用模式
772
+ import os
773
+
774
+ def get_secret(name: str) -> str:
775
+ """优先从 Docker secret 文件读取,回退到环境变量。"""
776
+ file_path = os.environ.get(f"{name}_FILE")
777
+ if file_path and os.path.exists(file_path):
778
+ with open(file_path, "r") as f:
779
+ return f.read().strip()
780
+ value = os.environ.get(name)
781
+ if value:
782
+ return value
783
+ raise RuntimeError(f"Secret {name} not found in file or env")
784
+ ```
785
+
786
+ ### 运行时安全
787
+
788
+ ```bash
789
+ # 1. 只读文件系统
790
+ docker run --read-only --tmpfs /tmp my-api:v1
791
+
792
+ # 2. 限制 capabilities(移除所有,仅添加必要的)
793
+ docker run --cap-drop ALL --cap-add NET_BIND_SERVICE my-api:v1
794
+
795
+ # 3. 禁止权限提升
796
+ docker run --security-opt no-new-privileges my-api:v1
797
+
798
+ # 4. 限制系统调用(seccomp)
799
+ docker run --security-opt seccomp=./seccomp-profile.json my-api:v1
800
+
801
+ # 5. 禁止容器内挂载 Docker socket
802
+ # 绝对不要: -v /var/run/docker.sock:/var/run/docker.sock
803
+
804
+ # 6. 综合安全运行示例
805
+ docker run -d --name api \
806
+ --read-only \
807
+ --tmpfs /tmp:rw,noexec,nosuid,size=100m \
808
+ --cap-drop ALL \
809
+ --cap-add NET_BIND_SERVICE \
810
+ --security-opt no-new-privileges:true \
811
+ --memory 512m \
812
+ --cpus 1.0 \
813
+ --pids-limit 100 \
814
+ --user 1001:1001 \
815
+ my-api:v1
816
+ ```
817
+
818
+ ### Docker Content Trust
819
+
820
+ ```bash
821
+ # 启用镜像签名验证
822
+ export DOCKER_CONTENT_TRUST=1
823
+
824
+ # 签名并推送镜像
825
+ docker push harbor.company.com/backend/api:v1.0.0
826
+
827
+ # 启用后,未签名镜像无法拉取或运行
828
+ ```
829
+
830
+ ---
831
+
832
+ ## 性能优化
833
+
834
+ ### 构建性能
835
+
836
+ ```bash
837
+ # 1. 使用 BuildKit(Docker 23+ 默认启用)
838
+ export DOCKER_BUILDKIT=1
839
+
840
+ # 2. 并行多阶段构建(BuildKit 自动并行无依赖的阶段)
841
+ docker build --progress=plain -t my-app:v1 .
842
+
843
+ # 3. 缓存挂载(避免重复下载依赖)
844
+ # Dockerfile 中使用 --mount=type=cache
845
+ ```
846
+
847
+ ```dockerfile
848
+ # 利用 BuildKit 缓存挂载加速依赖安装
849
+ FROM python:3.12-slim
850
+
851
+ WORKDIR /app
852
+
853
+ COPY requirements.txt .
854
+
855
+ # pip 缓存挂载 — 不同构建间共享下载缓存
856
+ RUN --mount=type=cache,target=/root/.cache/pip \
857
+ pip install -r requirements.txt
858
+
859
+ COPY . .
860
+
861
+ CMD ["python", "-m", "uvicorn", "app:app", "--host", "0.0.0.0"]
862
+ ```
863
+
864
+ ```dockerfile
865
+ # Go 应用的缓存挂载示例
866
+ FROM golang:1.22 AS builder
867
+
868
+ WORKDIR /build
869
+
870
+ COPY go.mod go.sum ./
871
+ RUN --mount=type=cache,target=/go/pkg/mod \
872
+ go mod download
873
+
874
+ COPY . .
875
+ RUN --mount=type=cache,target=/go/pkg/mod \
876
+ --mount=type=cache,target=/root/.cache/go-build \
877
+ CGO_ENABLED=0 go build -o /app/server ./cmd/server
878
+
879
+ FROM gcr.io/distroless/static-debian12
880
+ COPY --from=builder /app/server /server
881
+ CMD ["/server"]
882
+ ```
883
+
884
+ ### 运行时性能
885
+
886
+ ```bash
887
+ # 资源限制配置
888
+ docker run -d --name api \
889
+ --memory 1g \ # 内存硬限制
890
+ --memory-swap 1g \ # 禁止 swap(等于 --memory)
891
+ --cpus 2.0 \ # CPU 限制(2 核)
892
+ --cpu-shares 1024 \ # CPU 权重(相对优先级)
893
+ --pids-limit 200 \ # 进程数限制
894
+ --ulimit nofile=65535:65535 \ # 文件描述符限制
895
+ my-api:v1
896
+ ```
897
+
898
+ ### 镜像体积优化
899
+
900
+ ```bash
901
+ # 体积对比:选择合适的基础镜像
902
+ # python:3.12 -> ~1.0 GB
903
+ # python:3.12-slim -> ~150 MB
904
+ # python:3.12-alpine -> ~50 MB(注意 musl 兼容性)
905
+
906
+ # 分析镜像体积
907
+ docker images my-api --format "{{.Size}}"
908
+
909
+ # 使用 dive 分析每一层
910
+ dive my-api:v1
911
+ ```
912
+
913
+ **Alpine vs Slim 选择指南**:
914
+ - **选 slim**:需要 glibc、有 C 扩展依赖(pandas, numpy, psycopg2)、时区数据
915
+ - **选 alpine**:纯静态二进制(Go)、极简 Node.js 应用、对镜像体积有极端要求
916
+ - **选 distroless**:生产环境最终镜像、不需要 shell 的场景、安全优先
917
+
918
+ ---
919
+
920
+ ## 生产部署
921
+
922
+ ### 健康检查
923
+
924
+ ```dockerfile
925
+ # HTTP 健康检查
926
+ HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
927
+ CMD curl -f http://localhost:8000/health || exit 1
928
+
929
+ # TCP 健康检查(无 curl 的镜像)
930
+ HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
931
+ CMD wget -qO- http://localhost:8000/health || exit 1
932
+
933
+ # 数据库健康检查
934
+ HEALTHCHECK --interval=10s --timeout=5s --retries=5 \
935
+ CMD pg_isready -U app -d myapp || exit 1
936
+ ```
937
+
938
+ ### 优雅停机
939
+
940
+ ```javascript
941
+ // Node.js 优雅停机示例
942
+ const server = app.listen(3000);
943
+
944
+ const shutdown = async (signal) => {
945
+ console.log(`Received ${signal}, shutting down gracefully...`);
946
+
947
+ // 停止接收新请求
948
+ server.close(() => {
949
+ console.log('HTTP server closed');
950
+ });
951
+
952
+ // 关闭数据库连接
953
+ await db.disconnect();
954
+
955
+ // 关闭 Redis 连接
956
+ await redis.quit();
957
+
958
+ process.exit(0);
959
+ };
960
+
961
+ process.on('SIGTERM', () => shutdown('SIGTERM'));
962
+ process.on('SIGINT', () => shutdown('SIGINT'));
963
+ ```
964
+
965
+ ```python
966
+ # Python (FastAPI + Uvicorn) 优雅停机
967
+ import signal
968
+ import asyncio
969
+ from contextlib import asynccontextmanager
970
+
971
+ @asynccontextmanager
972
+ async def lifespan(app):
973
+ # 启动时:初始化连接池
974
+ await db_pool.connect()
975
+ await redis_pool.connect()
976
+ yield
977
+ # 关闭时:释放资源
978
+ await db_pool.disconnect()
979
+ await redis_pool.disconnect()
980
+
981
+ app = FastAPI(lifespan=lifespan)
982
+ ```
983
+
984
+ ### 零停机部署
985
+
986
+ ```yaml
987
+ # docker-compose 滚动更新(蓝绿部署简易版)
988
+ services:
989
+ api:
990
+ image: my-api:${VERSION:-latest}
991
+ deploy:
992
+ replicas: 2
993
+ update_config:
994
+ parallelism: 1 # 每次更新 1 个副本
995
+ delay: 30s # 更新间隔
996
+ failure_action: rollback
997
+ order: start-first # 先启动新副本再停旧副本
998
+ rollback_config:
999
+ parallelism: 0
1000
+ order: stop-first
1001
+ ```
1002
+
1003
+ ```bash
1004
+ # 手动蓝绿部署脚本
1005
+ #!/bin/bash
1006
+ set -euo pipefail
1007
+
1008
+ NEW_VERSION=$1
1009
+ OLD_CONTAINER="api-blue"
1010
+ NEW_CONTAINER="api-green"
1011
+
1012
+ # 启动新版本
1013
+ docker run -d --name $NEW_CONTAINER \
1014
+ --network app-net \
1015
+ my-api:$NEW_VERSION
1016
+
1017
+ # 等待健康检查通过
1018
+ echo "Waiting for health check..."
1019
+ for i in $(seq 1 30); do
1020
+ if docker exec $NEW_CONTAINER curl -sf http://localhost:8000/health > /dev/null 2>&1; then
1021
+ echo "New container is healthy"
1022
+ break
1023
+ fi
1024
+ if [ $i -eq 30 ]; then
1025
+ echo "Health check failed, rolling back"
1026
+ docker rm -f $NEW_CONTAINER
1027
+ exit 1
1028
+ fi
1029
+ sleep 2
1030
+ done
1031
+
1032
+ # 切换流量(更新 nginx upstream)
1033
+ docker exec nginx nginx -s reload
1034
+
1035
+ # 优雅停止旧容器
1036
+ docker stop --time 30 $OLD_CONTAINER
1037
+ docker rm $OLD_CONTAINER
1038
+ ```
1039
+
1040
+ ### 多架构构建
1041
+
1042
+ ```bash
1043
+ # 创建多架构构建器
1044
+ docker buildx create --name multiarch --use
1045
+
1046
+ # 构建并推送多架构镜像
1047
+ docker buildx build \
1048
+ --platform linux/amd64,linux/arm64 \
1049
+ -t harbor.company.com/backend/api:v1.0.0 \
1050
+ --push .
1051
+
1052
+ # 查看多架构信息
1053
+ docker manifest inspect harbor.company.com/backend/api:v1.0.0
1054
+ ```
1055
+
1056
+ ---
1057
+
1058
+ ## 监控与日志
1059
+
1060
+ ### 日志驱动配置
1061
+
1062
+ ```json
1063
+ // /etc/docker/daemon.json
1064
+ {
1065
+ "log-driver": "json-file",
1066
+ "log-opts": {
1067
+ "max-size": "50m",
1068
+ "max-file": "5",
1069
+ "compress": "true"
1070
+ }
1071
+ }
1072
+ ```
1073
+
1074
+ ```yaml
1075
+ # docker-compose.yml 中配置日志
1076
+ services:
1077
+ api:
1078
+ image: my-api:v1
1079
+ logging:
1080
+ driver: json-file
1081
+ options:
1082
+ max-size: "50m"
1083
+ max-file: "5"
1084
+ tag: "{{.Name}}/{{.ID}}"
1085
+ ```
1086
+
1087
+ ### 结构化日志
1088
+
1089
+ ```python
1090
+ # Python 结构化日志输出到 stdout(Docker 日志采集推荐方式)
1091
+ import json
1092
+ import logging
1093
+ import sys
1094
+
1095
+ class JSONFormatter(logging.Formatter):
1096
+ def format(self, record):
1097
+ log_entry = {
1098
+ "timestamp": self.formatTime(record),
1099
+ "level": record.levelname,
1100
+ "message": record.getMessage(),
1101
+ "module": record.module,
1102
+ "function": record.funcName,
1103
+ }
1104
+ if record.exc_info:
1105
+ log_entry["exception"] = self.formatException(record.exc_info)
1106
+ return json.dumps(log_entry, ensure_ascii=False)
1107
+
1108
+ handler = logging.StreamHandler(sys.stdout)
1109
+ handler.setFormatter(JSONFormatter())
1110
+ logger = logging.getLogger("app")
1111
+ logger.addHandler(handler)
1112
+ logger.setLevel(logging.INFO)
1113
+ ```
1114
+
1115
+ ### Prometheus 指标采集
1116
+
1117
+ ```yaml
1118
+ # docker-compose.yml 监控栈
1119
+ services:
1120
+ prometheus:
1121
+ image: prom/prometheus:v2.51.0
1122
+ ports:
1123
+ - "9090:9090"
1124
+ volumes:
1125
+ - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
1126
+ - prometheus-data:/prometheus
1127
+ command:
1128
+ - '--config.file=/etc/prometheus/prometheus.yml'
1129
+ - '--storage.tsdb.retention.time=30d'
1130
+ networks:
1131
+ - monitoring
1132
+
1133
+ grafana:
1134
+ image: grafana/grafana:10.4.0
1135
+ ports:
1136
+ - "3100:3000"
1137
+ environment:
1138
+ GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
1139
+ volumes:
1140
+ - grafana-data:/var/lib/grafana
1141
+ - ./monitoring/dashboards:/etc/grafana/provisioning/dashboards:ro
1142
+ networks:
1143
+ - monitoring
1144
+
1145
+ cadvisor:
1146
+ image: gcr.io/cadvisor/cadvisor:v0.49.1
1147
+ ports:
1148
+ - "8080:8080"
1149
+ volumes:
1150
+ - /:/rootfs:ro
1151
+ - /var/run:/var/run:ro
1152
+ - /sys:/sys:ro
1153
+ - /var/lib/docker/:/var/lib/docker:ro
1154
+ networks:
1155
+ - monitoring
1156
+
1157
+ volumes:
1158
+ prometheus-data:
1159
+ grafana-data:
1160
+
1161
+ networks:
1162
+ monitoring:
1163
+ driver: bridge
1164
+ ```
1165
+
1166
+ ```yaml
1167
+ # monitoring/prometheus.yml
1168
+ global:
1169
+ scrape_interval: 15s
1170
+
1171
+ scrape_configs:
1172
+ - job_name: 'cadvisor'
1173
+ static_configs:
1174
+ - targets: ['cadvisor:8080']
1175
+
1176
+ - job_name: 'api'
1177
+ static_configs:
1178
+ - targets: ['api:8000']
1179
+ metrics_path: /metrics
1180
+ ```
1181
+
1182
+ ### Docker 事件监控
1183
+
1184
+ ```bash
1185
+ # 实时监控 Docker 事件
1186
+ docker events --filter type=container --format '{{.Time}} {{.Action}} {{.Actor.Attributes.name}}'
1187
+
1188
+ # 监控 OOM Kill 事件
1189
+ docker events --filter event=oom
1190
+
1191
+ # 查看容器资源使用(单次快照)
1192
+ docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"
1193
+ ```
1194
+
1195
+ ---
1196
+
1197
+ ## 常见陷阱与反模式
1198
+
1199
+ ### 1. 使用 latest 标签
1200
+
1201
+ ```bash
1202
+ # 反模式:不可复现的部署
1203
+ docker pull myapp:latest
1204
+ # latest 随时可能变化,今天和明天拉到的可能是不同版本
1205
+
1206
+ # 正确做法:固定版本标签
1207
+ docker pull myapp:v2.1.5
1208
+ # 或更严格:使用 digest
1209
+ docker pull myapp@sha256:abc123...
1210
+ ```
1211
+
1212
+ ### 2. 在镜像中嵌入 secrets
1213
+
1214
+ ```dockerfile
1215
+ # 反模式:密钥硬编码或复制进镜像
1216
+ ENV DATABASE_URL=postgresql://user:password@host/db
1217
+ COPY .env /app/.env
1218
+ # 任何人 docker history 都能看到
1219
+
1220
+ # 正确做法:运行时注入
1221
+ # docker run -e DATABASE_URL_FILE=/run/secrets/db_url ...
1222
+ # 或使用 Docker secrets / 外部密钥管理
1223
+ ```
1224
+
1225
+ ### 3. 以 root 运行容器
1226
+
1227
+ ```dockerfile
1228
+ # 反模式:默认 root 运行(不写 USER 指令)
1229
+ FROM node:20
1230
+ COPY . /app
1231
+ CMD ["node", "server.js"]
1232
+ # 容器内进程以 root 运行,逃逸后直接获得宿主 root 权限
1233
+
1234
+ # 正确做法:显式非 root 用户
1235
+ FROM node:20
1236
+ RUN addgroup --system app && adduser --system --ingroup app app
1237
+ USER app
1238
+ COPY --chown=app:app . /app
1239
+ CMD ["node", "server.js"]
1240
+ ```
1241
+
1242
+ ### 4. 构建上下文过大
1243
+
1244
+ ```bash
1245
+ # 反模式:无 .dockerignore,发送整个项目目录(含 node_modules、.git 等)
1246
+ # 构建时 "Sending build context to Docker daemon" 显示 500MB+
1247
+
1248
+ # 正确做法:配置 .dockerignore 精准排除
1249
+ # 构建上下文应控制在 10MB 以内
1250
+ ```
1251
+
1252
+ ### 5. 单层安装依赖不清理
1253
+
1254
+ ```dockerfile
1255
+ # 反模式:临时文件残留在镜像层中
1256
+ RUN apt-get update
1257
+ RUN apt-get install -y build-essential
1258
+ RUN make install
1259
+ RUN apt-get remove -y build-essential # 虽然删了,但前面的层仍然占空间
1260
+
1261
+ # 正确做法:同一 RUN 指令中清理
1262
+ RUN apt-get update && \
1263
+ apt-get install -y --no-install-recommends build-essential && \
1264
+ make install && \
1265
+ apt-get purge -y build-essential && \
1266
+ apt-get autoremove -y && \
1267
+ rm -rf /var/lib/apt/lists/*
1268
+ ```
1269
+
1270
+ ### 6. 容器内数据不持久化
1271
+
1272
+ ```bash
1273
+ # 反模式:数据库数据存在容器可写层
1274
+ docker run -d postgres:16
1275
+ # 容器删除后数据全部丢失
1276
+
1277
+ # 正确做法:使用命名卷
1278
+ docker run -d -v pg-data:/var/lib/postgresql/data postgres:16
1279
+ ```
1280
+
1281
+ ### 7. 忽略健康检查
1282
+
1283
+ ```yaml
1284
+ # 反模式:Compose 中不配置 healthcheck 和 depends_on condition
1285
+ services:
1286
+ api:
1287
+ depends_on:
1288
+ - postgres # 仅等待容器启动,不等数据库就绪
1289
+ # API 启动时数据库可能尚未初始化完成,导致连接失败
1290
+
1291
+ # 正确做法:见上方 Compose 示例中的 healthcheck + condition
1292
+ ```
1293
+
1294
+ ### 8. 直接挂载 Docker Socket
1295
+
1296
+ ```bash
1297
+ # 反模式:给应用容器挂载 Docker socket
1298
+ docker run -v /var/run/docker.sock:/var/run/docker.sock myapp
1299
+ # 等同于给容器 root 权限控制整个 Docker daemon
1300
+
1301
+ # 正确做法:如果必须管理容器,使用受限的 Docker API 代理
1302
+ # 例如 Tecnativa/docker-socket-proxy
1303
+ ```
1304
+
1305
+ ---
1306
+
1307
+ ## 故障排查 Playbook
1308
+
1309
+ ### 容器无法启动
1310
+
1311
+ ```bash
1312
+ # 1. 查看容器退出码和状态
1313
+ docker ps -a --filter name=my-api
1314
+ # Exit code 0: 正常退出
1315
+ # Exit code 1: 应用错误
1316
+ # Exit code 137: OOM Killed (128 + 9 = SIGKILL)
1317
+ # Exit code 139: Segfault
1318
+ # Exit code 143: SIGTERM
1319
+
1320
+ # 2. 查看容器日志
1321
+ docker logs my-api 2>&1 | tail -50
1322
+
1323
+ # 3. 检查容器详细信息
1324
+ docker inspect my-api --format '{{.State.ExitCode}} {{.State.Error}}'
1325
+
1326
+ # 4. 以交互模式调试(覆盖入口点)
1327
+ docker run --rm -it --entrypoint /bin/sh my-api:v1
1328
+ ```
1329
+
1330
+ ### OOM Killed
1331
+
1332
+ ```bash
1333
+ # 1. 确认是否 OOM
1334
+ docker inspect my-api --format '{{.State.OOMKilled}}'
1335
+
1336
+ # 2. 查看内存限制
1337
+ docker stats --no-stream my-api
1338
+
1339
+ # 3. 查看内核 OOM 日志
1340
+ dmesg | grep -i "oom\|killed"
1341
+
1342
+ # 4. 解决方案
1343
+ # a. 增加内存限制
1344
+ docker update --memory 1g --memory-swap 1g my-api
1345
+ # b. 优化应用内存使用(检查内存泄漏)
1346
+ # c. 设置合理的 JVM/Node.js 堆大小
1347
+ # Node.js: --max-old-space-size=768
1348
+ # JVM: -Xmx768m
1349
+ ```
1350
+
1351
+ ### 网络问题
1352
+
1353
+ ```bash
1354
+ # 1. 检查容器网络配置
1355
+ docker inspect my-api --format '{{json .NetworkSettings.Networks}}' | jq .
1356
+
1357
+ # 2. 容器间连通性测试
1358
+ docker exec my-api ping -c 3 postgres
1359
+ docker exec my-api nslookup postgres
1360
+
1361
+ # 3. 检查端口映射
1362
+ docker port my-api
1363
+ netstat -tlnp | grep docker # 或 ss -tlnp
1364
+
1365
+ # 4. 抓包分析
1366
+ docker exec my-api tcpdump -i eth0 -nn port 5432 -c 20
1367
+
1368
+ # 5. 检查 DNS 解析
1369
+ docker exec my-api cat /etc/resolv.conf
1370
+
1371
+ # 6. 检查防火墙/iptables 规则
1372
+ iptables -L -n -t nat | grep DOCKER
1373
+ ```
1374
+
1375
+ ### 磁盘空间不足
1376
+
1377
+ ```bash
1378
+ # 1. 查看 Docker 磁盘使用
1379
+ docker system df
1380
+ docker system df -v
1381
+
1382
+ # 2. 分步清理(从安全到激进)
1383
+ # a. 清理已停止的容器
1384
+ docker container prune -f
1385
+ # b. 清理悬空镜像
1386
+ docker image prune -f
1387
+ # c. 清理未使用的网络
1388
+ docker network prune -f
1389
+ # d. 清理未使用的卷(危险!确认无重要数据)
1390
+ docker volume prune -f
1391
+
1392
+ # 3. 一键清理(不清理卷)
1393
+ docker system prune -f
1394
+
1395
+ # 4. 一键清理(含卷,危险)
1396
+ docker system prune -a --volumes -f
1397
+
1398
+ # 5. 查看 Docker 数据目录大小
1399
+ du -sh /var/lib/docker/
1400
+ du -sh /var/lib/docker/overlay2/
1401
+ ```
1402
+
1403
+ ### 构建缓存失效
1404
+
1405
+ ```bash
1406
+ # 1. 查看构建缓存
1407
+ docker builder prune --dry-run
1408
+
1409
+ # 2. 检查 .dockerignore 是否正确
1410
+ # 确保频繁变动的文件被排除
1411
+
1412
+ # 3. 重新组织 Dockerfile 层顺序
1413
+ # 把 COPY package.json 和 RUN npm ci 放在 COPY . . 之前
1414
+
1415
+ # 4. 使用外部缓存源(CI 环境)
1416
+ docker buildx build \
1417
+ --cache-from type=registry,ref=harbor.company.com/cache/my-api \
1418
+ --cache-to type=registry,ref=harbor.company.com/cache/my-api,mode=max \
1419
+ -t my-api:v1 .
1420
+ ```
1421
+
1422
+ ### 容器进程僵死
1423
+
1424
+ ```bash
1425
+ # 1. 检查进程状态
1426
+ docker top my-api
1427
+
1428
+ # 2. 检查是否有僵尸进程
1429
+ docker exec my-api ps aux | grep Z
1430
+
1431
+ # 3. 使用 tini 作为 init 进程(推荐)
1432
+ # Dockerfile 中:
1433
+ # RUN apk add --no-cache tini
1434
+ # ENTRYPOINT ["/sbin/tini", "--"]
1435
+ # CMD ["node", "server.js"]
1436
+
1437
+ # 4. 或使用 Docker 内置 init
1438
+ docker run --init my-api:v1
1439
+ ```
1440
+
1441
+ ---
1442
+
1443
+ ## Agent Checklist
1444
+
1445
+ 以下是 Agent 在项目中使用 Docker 时必须检查的要点。每次涉及 Docker 相关的文件变更时,逐项验证。
1446
+
1447
+ ### Dockerfile 检查
1448
+
1449
+ - [ ] 基础镜像使用固定版本标签(禁止 `latest`)
1450
+ - [ ] 使用多阶段构建分离构建依赖与运行时
1451
+ - [ ] 最终阶段使用非 root 用户运行(`USER` 指令存在且在末尾)
1452
+ - [ ] `HEALTHCHECK` 指令已配置
1453
+ - [ ] `.dockerignore` 存在且覆盖 `.git`、`node_modules`、`__pycache__`、`.env`、`*.key`
1454
+ - [ ] 依赖安装指令在源码复制之前(缓存优化)
1455
+ - [ ] 同一 `RUN` 层中清理 apt 缓存和临时文件
1456
+ - [ ] 未在 Dockerfile 中硬编码任何密钥、密码、Token
1457
+ - [ ] 使用 `--no-install-recommends` 减小体积
1458
+ - [ ] `COPY` 指令使用 `--chown` 设置正确所有者
1459
+
1460
+ ### Docker Compose 检查
1461
+
1462
+ - [ ] 所有有状态服务(数据库、缓存)使用命名卷
1463
+ - [ ] 关键服务配置 `healthcheck` 和 `depends_on.condition`
1464
+ - [ ] 网络按层划分(前端/后端/数据层),数据层使用 `internal: true`
1465
+ - [ ] 资源限制已配置(`deploy.resources.limits`)
1466
+ - [ ] 密钥通过 `secrets` 或环境变量文件注入,不硬编码在 compose 文件
1467
+ - [ ] 日志驱动配置了 `max-size` 和 `max-file` 防止磁盘爆满
1468
+ - [ ] `restart: unless-stopped` 或 `restart: always` 已设置
1469
+
1470
+ ### 安全检查
1471
+
1472
+ - [ ] 镜像构建后执行漏洞扫描(trivy/scout)
1473
+ - [ ] 未挂载 Docker socket 到应用容器
1474
+ - [ ] 生产容器使用 `--cap-drop ALL`,仅 `--cap-add` 必要权限
1475
+ - [ ] 生产容器设置 `--security-opt no-new-privileges`
1476
+ - [ ] 未在镜像层中包含 `.env`、`*.pem`、`*.key` 等敏感文件
1477
+
1478
+ ### 生产部署检查
1479
+
1480
+ - [ ] CI/CD 流水线集成镜像扫描步骤
1481
+ - [ ] 健康检查端点返回依赖状态(数据库、缓存连接)
1482
+ - [ ] 应用实现优雅停机(处理 SIGTERM)
1483
+ - [ ] 日志输出到 stdout/stderr(不写文件)
1484
+ - [ ] 内存限制与应用堆大小一致(避免 OOM)
1485
+ - [ ] 数据卷有定期备份策略
1486
+ - [ ] 使用 `--init` 或 tini 防止僵尸进程
1487
+
1488
+ ---
1489
+
1490
+ > 文档版本: v1.0 | 最后更新: 2026-03-28