Compose basic
One RasterizedDB server under Docker Compose, with a named volume, a healthcheck and a restart policy. Start here for a single server on one host, and add to it from the other recipes.
What you get
- The server on the PostgreSQL protocol (5432) and the native protocol (61170), published on
127.0.0.1. - The role
rasterized, a superuser, with the password from.env. - Data on the volume
rasterizeddb_data. restart: unless-stopped, so the server comes back after a crash or a reboot of the Docker host.stop_grace_period: 60s, sodocker compose stopanddowngive the server time to shut down cleanly.- A healthcheck: the container reads
healthyonce the PostgreSQL port accepts connections. Other services can wait for it withdepends_on: condition: service_healthy.
Prerequisites
- Docker Engine with the Compose plugin (
docker compose), or Docker Desktop. - On x86-64, a CPU with AVX2 (x86-64-v3).
psql, or thepostgres:17image, to check the result.
What to edit
- Copy
.env.exampleto.envand setRASTERIZEDDB_PG_PASSWORD. A$in the password has to be written$$in.env. - Optional: in
compose.yaml, change thecommandline to add server flags (they are added to the image's own), and drop127.0.0.1:from a port to accept connections from other machines.
cp .env.example .envThe password is used on the first start only, to create the role. To change it later, run ALTER ROLE rasterized PASSWORD 'the-new-password'; while connected.
Run it
docker compose up -d
docker compose psdocker compose ps shows (healthy) once the server accepts connections. The first check runs ten seconds after the start.
Check that it works
psql "host=127.0.0.1 port=5432 user=rasterized dbname=postgres sslmode=require" \
-c "CREATE TABLE greetings (id INT PRIMARY KEY, message TEXT)" \
-c "INSERT INTO greetings VALUES (1, 'hello')" \
-c "SELECT * FROM greetings"CREATE TABLE
INSERT 0 1
id | message
----+---------
1 | hello
(1 row)Without psql on the host, run it from the postgres:17 image on the project's network, where the server is reachable by its service name:
docker run --rm -it --network rasterizeddb_default postgres:17 \
psql "host=rasterizeddb port=5432 user=rasterized dbname=postgres sslmode=require"Read the server's log:
docker compose logs rasterizeddbThe command line sets --log-level warn. Use info for more, or remove the line for the default (error).
Stop and clean up
Stop and start, keeping everything:
docker compose stop
docker compose startRemove the container and network. The volume, and so the data, stays:
docker compose downRemove the data as well. This deletes the database for good:
docker compose down -vLimits
- The healthcheck proves that the port is open, not that a query succeeds. The image has no SQL client to do more.
- The healthcheck uses
bash, which thealpinetags do not have. With analpinetag, remove thehealthcheckblock. - Back the volume up before an upgrade: see Backing up the volume and Upgrading.
The files
# RasterizedDB with Docker Compose: one server, a named volume, a
# healthcheck and a restart policy. Copy .env.example to .env first.
name: rasterizeddb
services:
rasterizeddb:
image: ${RASTERIZEDDB_IMAGE:-milendenev/rasterizeddb:1.1.0}
environment:
# The password of the role `rasterized`, set on the first start.
RASTERIZEDDB_PG_PASSWORD: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
# Extra server flags, added to the image's own. Remove or extend.
command: ["--log-level", "warn"]
ports:
# 127.0.0.1 keeps the ports on this machine. Drop the address to
# accept connections from other machines.
- "127.0.0.1:5432:5432" # PostgreSQL protocol
- "127.0.0.1:61170:61170" # native protocol (rasterizeddb_client, Studio)
volumes:
- data:/var/lib/rasterizeddb
restart: unless-stopped
stop_grace_period: 60s
healthcheck:
# The image has bash but no SQL client: this checks that the
# PostgreSQL port accepts a connection.
test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/5432"]
interval: 10s
timeout: 5s
retries: 6
start_period: 30s
volumes:
data:# Copy to .env next to compose.yaml and change the password.
# The password of the role `rasterized`, used on the first start only.
RASTERIZEDDB_PG_PASSWORD=change-me-to-a-long-random-password
# The image and tag. Pin a full version.
RASTERIZEDDB_IMAGE=milendenev/rasterizeddb:1.1.0