Docker 部署
Prometheus + Grafana compose、9090/3000 端口。
30 分钟
Docker 部署
动手之前需要可复现的 ops lab:本机用 Compose 起 Prometheus + Grafana,端口与生产习惯一致(9090 / 3000)。
学习目标
- 用 Docker Compose 启动 Prometheus 与 Grafana
- 访问 Prometheus UI 与 Grafana 登录页
- 理解数据卷持久化 TSDB 与 Grafana 配置
核心内容
最小 compose
在 ops-lab/(或课程配套目录)创建:
services:
prometheus:
image: prom/prometheus:v2.55.0
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prom-data:/prometheus
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
grafana:
image: grafana/grafana:11.2.0
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
volumes:
prom-data:
grafana-data:
docker compose up -d
# Prometheus → http://localhost:9090
# Grafana → http://localhost:3000 (首次 admin/admin)
与 backend 联调
backend 跑在宿主机 8080 时,Prometheus 容器需能访问宿主机:
# prometheus.yml 片段(Linux 可用 host.docker.internal 或 extra_hosts)
scrape_configs:
- job_name: coderoadmap-backend
static_configs:
- targets: ["host.docker.internal:8080"]
Windows / Mac Docker Desktop 通常自带 host.docker.internal;纯 Linux 可用 network_mode: host 或把 backend 也放进同一 compose。
安全提示
ops lab 默认弱密码仅用于本机;勿将 9090/3000 无认证暴露公网。生产见 kubernetes-essentials 内网 Service + Ingress。
练习
-
docker compose ps确认两容器 healthy - 打开 Prometheus Status → Targets,确认列表(下节配置抓取)
- 在 Grafana 添加 Prometheus 数据源
http://prometheus:9090(同 compose 网络内)
小结
ops lab = Prometheus 9090 + Grafana 3000,TSDB 与看板配置落盘。 下一课写 scrape_configs 让 backend 出现在 Targets 里。