Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve database volume identity during Docker repair

Adam Eivyfe79c90db95c
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index 335680561d..781fe784f4 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -380,6 +380,16 @@ dump/import tools are used only for the selected local Docker endpoint; a
host/port override cannot silently fall back to the local container. Maintenance
`--endpoint` transfers remain bound to their explicitly recorded endpoint.
+**Docker repair preserves volume identity.** Before `scripts/db.sh fix` stops
+or removes an existing database container, it compares that container's data
+volume with the named volume resolved by `docker compose config`. A changed
+checkout directory, `COMPOSE_PROJECT_NAME`, or volume override can select a
+different volume for the next startup. Repair refuses a mismatch or an
+unresolved volume without changing the container or its data; restore the
+original Compose project/volume configuration before retrying. With no
+container, repair requires the configured named volume to already exist and
+never creates one.
+
**Endpoint binding (#10757).** The resolved host, port, user and database are
passed as explicit arguments, but libpq also honors inherited variables that
choose where a client really connects: `PGHOSTADDR` overrides the network
diff --git a/scripts/db-repair.test.js b/scripts/db-repair.test.js
index 716d6e5280..b381612774 100644
--- a/scripts/db-repair.test.js
+++ b/scripts/db-repair.test.js
@@ -52,7 +52,11 @@ case "$1 \${2:-}" in
'volume inspect') exit "\${STUB_VOLUME_RC:-0}" ;;
'compose config')
if [ "$3" = --images ]; then echo "$STUB_IMAGE";
- else echo '{"services":{"db":{"volumes":[{"type":"volume","source":"db-data","target":"/var/lib/postgresql/data"}]}},"volumes":{"db-data":{"name":"fixture-configured-volume"}}}'; fi ;;
+ else
+ [ "\${STUB_CONFIG_RC:-0}" = 0 ] || exit "$STUB_CONFIG_RC"
+ if [ -n "\${STUB_CONFIG_PAYLOAD:-}" ]; then printf '%s\\n' "$STUB_CONFIG_PAYLOAD";
+ else printf '{"services":{"db":{"volumes":[{"type":"volume","source":"db-data","target":"/var/lib/postgresql/data"}]}},"volumes":{"db-data":{"name":"%s"}}}\\n' "\${STUB_CONFIG_VOLUME:-fixture-recorded-volume}"; fi
+ fi ;;
'run --rm')
[ "\${STUB_HELPER_RC:-0}" = 0 ] || exit "$STUB_HELPER_RC"
# Execute the actual helper command against the fixture directory. A no-op
@@ -124,7 +128,7 @@ esac
it.each([
['stopped container', {}, 'fixture-recorded-volume'],
- ['absent container', { STUB_ABSENT: '1' }, 'fixture-configured-volume'],
+ ['absent container', { STUB_ABSENT: '1', STUB_CONFIG_VOLUME: 'fixture-configured-volume' }, 'fixture-configured-volume'],
])('repairs a verified %s using its exact volume and cached pinned image', (_, overrides, volume) => {
const result = run(overrides);
expect(result.status, result.stderr).toBe(0);
@@ -134,6 +138,31 @@ esac
if (overrides.STUB_ABSENT) expect(log()).not.toMatch(/^docker (rm|compose stop) /m);
});
+ // Removing a container from a previous Compose project frees its fixed name;
+ // a later `compose up` would create the newly configured volume and boot an
+ // empty database. Keep the old container and its records together instead.
+ it.each([
+ ['a changed project or volume name', { STUB_CONFIG_VOLUME: 'fixture-new-project-volume' }],
+ ['failed configuration inspection', { STUB_CONFIG_RC: '23' }],
+ ['malformed configuration', { STUB_CONFIG_PAYLOAD: '{"password":"fixture-sensitive-config"' }],
+ ['an unsupported bind mount', { STUB_CONFIG_PAYLOAD: JSON.stringify({
+ services: { db: { volumes: [{ type: 'bind', source: '/fixture/database', target: '/var/lib/postgresql/data' }] } },
+ }) }],
+ ['ambiguous data mounts', { STUB_CONFIG_PAYLOAD: JSON.stringify({
+ services: { db: { volumes: Array(2).fill({ type: 'volume', source: 'db-data', target: '/var/lib/postgresql/data' }) } },
+ volumes: { 'db-data': { name: 'fixture-recorded-volume' } },
+ }) }],
+ ])('preserves the existing container and lock after %s', (_, overrides) => {
+ const result = run(overrides);
+
+ expect(result.status, result.stderr).not.toBe(0);
+ expect(existsSync(join(root, 'removed'))).toBe(false);
+ expect(existsSync(lock)).toBe(true);
+ expect(log()).not.toMatch(/^docker (compose stop|rm|run) /m);
+ expect(result.stdout).not.toContain('Stale lock files cleaned');
+ expect(result.stdout + result.stderr).not.toContain('fixture-sensitive-config');
+ });
+
it('automatically repairs once and retries startup successfully', () => {
const result = run({}, 'start');
expect(result.status, result.stderr).toBe(0);
diff --git a/scripts/db.sh b/scripts/db.sh
index ad1049d46e..520aa114cd 100755
--- a/scripts/db.sh
+++ b/scripts/db.sh
@@ -470,9 +470,33 @@ fix_docker() {
cd "$ROOT_DIR"
+ local container_id data_volume configured_volume running
+ # Repair removes the container, so the next `compose up` must select the SAME
+ # volume. A renamed checkout/project or changed volume override can otherwise
+ # free the fixed container name and boot a new, empty database after repair.
+ # Resolve Compose's canonical model before any stop/removal. Its full output
+ # contains credentials: emit only a validated volume name, never parser errors.
+ if ! configured_volume=$(docker compose config --format json 2>/dev/null | node -e '
+ let input = "";
+ process.stdin.on("data", chunk => { input += chunk; });
+ process.stdin.on("end", () => {
+ try {
+ const config = JSON.parse(input);
+ const mounts = config.services?.db?.volumes;
+ if (!Array.isArray(mounts)) process.exit(1);
+ const matching = mounts.filter(v => v?.target === "/var/lib/postgresql/data");
+ if (matching.length !== 1 || matching[0].type !== "volume") process.exit(1);
+ const name = config.volumes?.[matching[0].source]?.name;
+ if (typeof name !== "string" || !/^[a-z0-9][a-z0-9_.-]*$/i.test(name)) process.exit(1);
+ process.stdout.write(name);
+ } catch { process.exit(1); }
+ });
+ '); then
+ err "Could not resolve the configured database volume; repair stopped"
+ return 1
+ fi
# A successful list distinguishes an absent container from an unavailable
# daemon. Never turn a failed inspection into permission to modify its volume.
- local container_id data_volume running
if ! container_id=$(docker ps -aq --filter 'name=^/portos-db$'); then
err "Could not establish the database container state; repair stopped"
return 1
@@ -484,6 +508,11 @@ fix_docker() {
err "Could not resolve the database data volume; repair stopped"
return 1
fi
+ if [ "$data_volume" != "$configured_volume" ]; then
+ err "Database container volume differs from the configured Compose volume; repair stopped"
+ info "Restore the original Compose project/volume configuration before retrying. The container and data are unchanged."
+ return 1
+ fi
if ! docker compose stop db; then
err "Failed to stop Docker PostgreSQL; repair stopped"
return 1
@@ -498,22 +527,9 @@ fix_docker() {
return 1
fi
else
- # No container exists. Ask Compose for the exact configured volume name;
- # never try guessed names, which docker run would silently create.
- if ! data_volume=$(docker compose config --format json | node -e '
- let input = "";
- process.stdin.on("data", chunk => { input += chunk; });
- process.stdin.on("end", () => {
- const config = JSON.parse(input);
- const mount = config.services?.db?.volumes?.find(v => v.type === "volume" && v.target === "/var/lib/postgresql/data");
- const name = config.volumes?.[mount?.source]?.name;
- if (typeof name !== "string" || !name) process.exit(1);
- process.stdout.write(name);
- });
- '); then
- err "Could not resolve the configured database volume; repair stopped"
- return 1
- fi
+ # No container exists. Never try guessed names, which docker run would
+ # silently create; the configured volume must already exist below.
+ data_volume="$configured_volume"
fi
# Verify absence after removal too, including any concurrent restart.
if ! container_id=$(docker ps -aq --filter 'name=^/portos-db$') || [ -n "$container_id" ]; then