Skip to content

docker-compose: Fix Postgres volume mount#887

Open
runephilosof-abtion wants to merge 1 commit into
SonarSource:masterfrom
runephilosof-abtion:patch-1
Open

docker-compose: Fix Postgres volume mount#887
runephilosof-abtion wants to merge 1 commit into
SonarSource:masterfrom
runephilosof-abtion:patch-1

Conversation

@runephilosof-abtion
Copy link
Copy Markdown

https://github.com/docker-library/docs/blob/ea45618e3609152fb315e85c2ee8678e3a4a9324/postgres/README.md?plain=1#L216

Important Note: (for PostgreSQL 17 and below) Mount the data volume at /var/lib/postgresql/data and not at /var/lib/postgresql because mounts at the latter path WILL NOT PERSIST database data when the container is re-created. The Dockerfile that builds the image declares a volume at /var/lib/postgresql/data and if no data volume is mounted at that path then the container runtime will automatically create an anonymous volume that is not reused across container re-creations. Data will be written to the anonymous volume rather than your intended data volume and won't persist when the container is deleted and re-created.

If you are frantically searching for your lost postgres data, look in /var/lib/docker/volumes for it:

find /var/lib/docker/volumes/ -type d -name base

Then copy the contents to the empty named volume:

docker compose stop
VOL=<name of the volume>
docker run --rm   -v $VOL:/from   -v sonarqube-self-hosted_postgresql:/to   alpine sh -c "cp -av /from/. /to/"
docker compose up

https://github.com/docker-library/docs/blob/ea45618e3609152fb315e85c2ee8678e3a4a9324/postgres/README.md?plain=1#L216

> **Important Note:** (for PostgreSQL 17 and below) Mount the data volume at `/var/lib/postgresql/data` and not at `/var/lib/postgresql` because mounts at the latter path WILL NOT PERSIST database data when the container is re-created. The Dockerfile that builds the image declares a volume at `/var/lib/postgresql/data` and if no data volume is mounted at that path then the container runtime will automatically create an [anonymous volume](https://docs.docker.com/engine/storage/#volumes) that is not reused across container re-creations. Data will be written to the anonymous volume rather than your intended data volume and won't persist when the container is deleted and re-created.
@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha Bot commented Apr 29, 2026

Summary

Changes the PostgreSQL volume mount path in the docker-compose example from /var/lib/postgresql to /var/lib/postgresql/data.

This is a critical fix for data persistence. When a volume is mounted at /var/lib/postgresql, PostgreSQL's declared volume at /var/lib/postgresql/data takes precedence, causing the database to write to an anonymous volume instead of the named volume. This data is lost when containers are recreated. The corrected path ensures data is persisted to the named volume as intended.

What reviewers should know

What to review:

  • Verify this is the only example compose file with this issue (check other postgres examples if present)
  • Confirm the fix applies to the correct PostgreSQL version range mentioned in the author's description

Context for reviewers:
This fixes a silent data loss scenario that occurs when containers are recreated—no warnings or errors, just missing data. The author's description includes helpful recovery steps for users who have already experienced this issue.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix applied here is correct — mounting at /var/lib/postgresql/data is the right path for the PostgreSQL 17 image used in this compose file. However, the same silent data-loss bug exists in two other example files that this PR did not touch, leaving users of those examples exposed to the same problem.

Incomplete fix — same bug in other compose files:

  • example-compose-files/sq-dce-postgres/docker-compose.yml line 150: postgresql:/var/lib/postgresql — identical to the bug fixed here; data will not persist across container re-creations for anyone using the DCE Postgres example.

  • example-compose-files/sq-dce-custom-zip-postgres/docker-compose.yml lines 200–201: has both mounts — postgresql:/var/lib/postgresql (incorrect) and postgresql_data:/var/lib/postgresql/data (correct). Data does survive here because the /data mount takes effect, but the parent-level mount is confusing dead weight and should be removed. tests/dce-compose-test/docker-compose.yml has the same two-mount pattern.

The PR should be expanded to fix sq-dce-postgres/docker-compose.yml at minimum. The sq-dce-custom-zip-postgres and tests/dce-compose-test files should have the redundant postgresql:/var/lib/postgresql line removed as well.

🗣️ Give feedback

@carminevassallo carminevassallo self-assigned this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants