When Prometheus runs in a Docker container, why must its metric data and its configuration live on persistent volumes, and where should the data volume sit?
Because the normal way to update a container is to stop the old one and start a new one from the newer image; without a volume, the metric history and the config would disappear with the old container. The data volume belongs on an SSD.
* Containers come and go; the volumes, and with them the history and the config, stay. *
A container's own file system lives and dies with that container. That is fine for the program, which comes fresh from the image, but not for the time series database, which has to outlive every update. A Docker volume is storage managed outside the container: the new container mounts the same volume and finds the data where the old one left it.
The same applies to prometheus.yml. It goes onto its own persistent volume, so the configuration also survives an update.
docker volume create prometheus-data
docker run -d -p 9090:9090 \
-v prometheus-data:/prometheus \
-v /srv/prometheus-config:/etc/prometheus \
prom/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus
-v prometheus-data:/prometheusmounts the volume where the image keeps its data by default.--config.fileand--storage.tsdb.pathtell Prometheus where to look. Arguments after the image name replace the image's defaults, so both have to be given.- Older guides written for Prometheus 1.x use
--storage.local.path; since Prometheus 2.0 the flag is--storage.tsdb.path, the same one a binary install puts in its systemd unit.
Where: queries read straight from that volume, so it should be on an SSD. The way to get there is to configure Docker to create its volumes under the SSD's mount point.
Go deeper:
Docker Docs — Volumes — named volumes, how they outlive containers and where Docker stores them.
The Prometheus Dockerfile on GitHub — the image's VOLUME /prometheus and the default flags the arguments replace.
Docker (software) — Wikipedia — images, containers and volumes in context.