Skip to content

Running on docker

Mikhail Grigorev edited this page Nov 25, 2024 · 2 revisions

Running on Docker.

General recommendations:

  • use environment variables instead of YAML configuration.
  • disable systems collectors - it is unnecessary to collect system metrics being inside the container.
  • disable collectors which require access to PostgreSQL data directories - postgres/settings, postgres/storage, postgres/logs.
  • setting up listening to allow network connection from other containers or hosts.

An example below demonstrates a typical use case when a database and pgSCV are running in separate containers.

  1. Create example network.
docker network create example
  1. Run postgres container.
    • Enable pg_stat_statements, it is preferred (but not strict).
    • Use POSTGRES_PASSWORD=secret to define password.
docker run -d --rm --name db -ti \
-e POSTGRES_PASSWORD=secret \
--network example \
postgres:14 -c 'shared_preload_libraries=pg_stat_statements'
docker exec -ti db psql -U postgres -c 'CREATE EXTENSION pg_stat_statements'
  1. Run pgscv container.
    • Specify Postgres connection settings using DATABASE_DSN.
    • Disable some collectors with PGSCV_DISABLE_COLLECTORS.
    • Setup listen address with PGSCV_LISTEN_ADDRESS.
    • Expose port 9890.
    • Don't detach, it's better to see logs if something goes wrong.
docker run -d --rm --name pgscv -ti \
-e PGSCV_LISTEN_ADDRESS=0.0.0.0:9890 \
-e PGSCV_DISABLE_COLLECTORS=system \
-e DATABASE_DSN="postgresql://postgres:secret@db/postgres" \
-p 9890:9890 --network example \
cherts/pgscv:latest
  1. Open the second terminal and fetch metrics using curl.
curl -s http://127.0.0.1:9890/metrics
  1. In the end, stop containers and remove example network.
docker stop db pgscv
docker rm db pgscv
docker network rm example