Keep a database's data when its container is replaced
Not started.
What you need
Windows 11 with Docker Desktop running, and either PowerShell or a WSL terminal. The verifier runs inside a throwaway Node container, so nothing else needs installing.
Steps
- Save
compose.yamlandverify.mjsinto an empty folder, and create a.envfile next to them with a line such asDB_PASSWORD=practice-only-1234. Use a throwaway password: this is practice. - Start it with
docker compose -p skilltree-persist up -dand run the verifier. It fails, and says why. - Fix the Compose file: keep the data in a named volume, publish the port on 127.0.0.1 only, and add a health check. Start it again with
up -d --wait. - Run the verifier. When every check passes it writes one row into the database and remembers it.
- Replace the container:
docker compose -p skilltree-persist down, thenup -d --wait. Do not add-v, which deletes volumes. - Run the verifier with
--afterat the end. It checks the container is new and the row is still there. Paste its last line below.
Why it matters
A container's own filesystem goes with the container. Data that must outlive it belongs in a volume. A named volume, declared under volumes: in the Compose file, keeps its name across down and up; an anonymous one gets a random name and is easy to lose.
Publishing "5432:5432" opens the database on every network interface of your machine, including Wi-Fi. "127.0.0.1:5432:5432" keeps it to this machine; containers on the same Compose network reach it without any published port at all.
A health check with pg_isready lets up --wait, and anything that depends on the database, wait until it accepts connections instead of failing on the first try.
When you are done
docker compose -p skilltree-persist down -vDelete the folder, including .env and .skilltree-persist.json
Files
Show compose.yaml
# Starting point for the compose-persist task. It runs, but its data dies with the container, the port is open on
# every network interface, and nothing tells Docker when the database is ready. Fix all three.
services:
db:
image: postgres:18
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?Set DB_PASSWORD in a .env file next to this one}
ports:
- "55440:5432"
It asks Docker for what it needs and changes nothing else; read it before you run it. Mounting the Docker socket gives it the same control over Docker that you have.
Run the verifier
In the folder with your files, in PowerShell or in WSL:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "${PWD}:/work" -w /work node:22-alpine node verify.mjs compose-persistThe last step adds --after to the end.
The verifier runs on your machine, so this page cannot see the result for itself: what you paste is what it records.
Checks
First run
- The db service is runningnot yet checked
- It runs PostgreSQLnot yet checked
- Its data is in a named volumenot yet checked
- Its port is reachable from this machine onlynot yet checked
- A container from it becomes healthynot yet checked
- A check row was writtennot yet checked
The final run (--after)
- The db service is runningnot yet checked
- It runs PostgreSQLnot yet checked
- Its data is in a named volumenot yet checked
- Its port is reachable from this machine onlynot yet checked
- A container from it becomes healthynot yet checked
- The first run was recordednot yet checked
- The container was replacednot yet checked
- The check row survivednot yet checked