如何让Docker容器中运行的Prometheus实现数据持久化?
确保Prometheus容器重启后保留数据的正确配置
看起来你已经找对了方向——用Docker卷来持久化Prometheus的时序数据,但有个关键细节需要调整,不然容器重启后数据还是会丢失。问题出在Prometheus默认的数据存储路径上,我来帮你梳理正确的配置步骤:
修正后的操作步骤
- 你创建Docker卷的步骤完全没问题,先保留这一步:
$ docker volume create a-new-volume - 启动容器时,要把卷挂载到Prometheus默认的数据存储目录
/prometheus(而不是你之前用的/prometheus-data),因为Prometheus默认会把数据写到这个路径下。如果一定要用自定义路径,就得额外指定启动参数。两种方式都给你列出来:
方式一:挂载到默认路径(推荐,更简洁)
$ docker run \ --publish 9090:9090 \ --volume a-new-volume:/prometheus \ --volume "$(pwd)"/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus
方式二:使用自定义数据路径(需指定启动参数)
如果你坚持要用/prometheus-data作为容器内的数据目录,启动时要加上--storage.tsdb.path参数告诉Prometheus数据存在哪:
$ docker run \ --publish 9090:9090 \ --volume a-new-volume:/prometheus-data \ --volume "$(pwd)"/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus \ --storage.tsdb.path=/prometheus-data
额外验证小技巧
重启容器后,你可以访问http://localhost:9090,在Graph页面查询之前抓取的HTTP端点数据,如果能看到历史记录,就说明持久化已经生效啦。另外你的配置文件挂载方式是对的,主机上的prometheus.yml会被容器正确读取,测试抓取HTTP端点的逻辑没问题。
内容的提问来源于stack exchange,提问作者Matt




