Docker postgres keep ignoring bind directive, creating new volume instead

Good day to you guys, i have some problem with docker compose / behaviour.
I already set up the directory in host to be bind type on docker volume, so the persistent data would be written on host.

Thing is, regardless of container rm or volume rm and write the compose file with explicit type/source/targets, once the docker is build, it continue to read/writeto volumes instead, ignoring the explicitly bind host directory.

The build cmd was run as root, so the host directory also owned by root:root

Any help or direction would be appreciated. Thanks you for your time reading this, best regards

Ubuntu 24.04 (host name is 20.04 though)
Docker ce version 5:29.8.0-1~ubuntu.24.04~noble

Docker compose yml

services:
  metabase:
    image: metabase/metabase:v0.62.3.x
    container_name: metabase
    hostname: metabase
    dns:
      - ip
      - ip
    volumes:
      - /dev/urandom:/dev/random:ro
    ports:
      - 3000:3000
    environment:
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabasedb
      MB_DB_PORT: 5432
      MB_DB_USER: /run/secrets/db_user
      MB_DB_PASS: /run/secrets/db_password
      MB_DB_HOST: postgres
      MB_ENCRYPTION_SECRET_KEY: /run/secrets/mb_key
    networks:
      - metanet1
    depends_on:
      - postgres
    secrets:
      - db_password
      - db_user
      - mb_key
    restart: always
  postgres:
    image: postgres:18
    container_name: postgres
    hostname: postgres
    dns:
      - ip
      - ip
    environment:
      POSTGRES_DB: metabasedb
      POSTGRES_USER: /run/secrets/db_user
      POSTGRES_PASSWORD: /run/secrets/db_password
    volumes:
      - ./pg_data:/var/lib/postgresql/docker
    networks:
      - metanet1
    secrets:
      - db_password
      - db_user
    restart: always

networks:
  metanet1:
    driver: bridge

secrets:
   db_password:
     file: db_password.txt
   db_user:
     file: db_user.txt
   mb_key:
     file: mb_key.txt

volumes
Notes : the first volume is leftover after container was rm ed.

This can’t be correct. The PGDATA variable is used to configure the data folder. Since you don’t set it in your environments, your container is using the default.

I just checked:

% docker run --rm --entrypoint env postgres:18
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/18/bin
HOSTNAME=73e3776da0ce
GOSU_VERSION=1.19
LANG=en_US.utf8
PG_MAJOR=18
PG_VERSION=18.6-1.pgdg13+2
PGDATA=/var/lib/postgresql/18/docker
HOME=/root

So either, you need to use the path /var/lib/postgresql/18/docker or add the environment variable PGDATA: /var/lib/postgresql/docker. The first is recommended, as your setup will match the official docs.

I didn’t look at the volume inspect output yesterday, because I usually don’t look at screenshots of text.
Now I did. The Dockerfile for the image declares /var/lib/postgresql as volume.

Thus, even though either suggestion from my last post will work and write the data into the bind mounted host path, but the unused anonymous volume will be still generated. If you want to prevent that you would need to use /var/lib/postgresql as container path.