分页: 1 / 1

2026年 Docker 生产部署实战:从镜像规范到稳定上线

发表于 : 周一 7月 20, 2026 10:13 am
admin
[b]引言[/b]

Docker 已是生产环境标配,但部署不只是 docker run 那么简单。总结几点生产级实践。

[b]1. 镜像构建规范[/b]

- 使用多阶段构建(multi-stage build)减小镜像体积
- 基础镜像选 alpine 或 distroless,减少安全漏洞
- 锁定版本标签,不要用 latest
- 定期扫描镜像漏洞(docker scout / trivy)

[b]2. 资源限制[/b]

[code]services:
web:
image: nginx:alpine
deploy:
resources:
limits:
cpus: '0.5'
memory: 256M
reservations:
cpus: '0.25'
memory: 128M[/code]

[b]3. 数据持久化[/b]

- 数据库类容器必须挂载数据卷
- 日志用日志驱动输出到中央收集
- 不要用 host 网络模式,用 bridge

[b]4. 健康检查[/b]

[code]healthcheck:
test: ['CMD', 'curl', '-f', 'http://localhost/health']
interval: 30s
timeout: 10s
retries: 3[/code]

[b]5. 日志与监控[/b]

- 接入 Prometheus + cAdvisor 监控容器指标
- 日志统一采集到 Loki / ELK
- 设置容器异常退出告警

[b]6. 部署策略[/b]

滚动更新而非全量重启:
[code]deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
order: start-first[/code]

生产环境无小事,每个细节都是潜在的故障点。欢迎交流你们的 Docker 生产踩坑经验。