Docker Image using the same volume

Hi Docker Peeps,

I have a curious question.

I created a container using docker compose using the two files.

ztm_postgre.yaml

version: '3.8'
services:
  ztm_postgre:
    image: postgres:14
    ports:
    - 5432:5432
    environment:
      - POSTGRES_USER=ztm_user
      - POSTGRES_PASSWORD=ztm_password
      - POSTGRES_DB=ztm_db
    volumes:
    - postgre_db_data:/var/lib/postgresql/data
volumes:
  postgre_db_data:
    driver: local

ztm_postgre-copy.yaml

version: '3.8'
services:
  ztm_postgre2:
    image: postgres:14
    ports:
    - 5433:5432
    environment:
      - POSTGRES_USER=ztm_user
      - POSTGRES_PASSWORD=ztm_password
      - POSTGRES_DB=ztm_db
    volumes:
    - postgre_db_data:/var/lib/postgresql/data
volumes:
  postgre_db_data:
    driver: local

Both containers are poniting to the “/var/lib/docker/volumes/smartbrain-fe_postgre_db_data/_data” on the running host.
When I connect to both localhost:5432 and localhost:5433, both instance do not see each other changes. Why is that?

The contents on the host directory file do not show any duplicate files that would indicate that the other container is looking at a different file.

lpt-linuxmint01:~$ sudo ls /var/lib/docker/volumes/smartbrain-fe_postgre_db_data/_data
base	pg_commit_ts  pg_hba.conf    pg_logical    pg_notify	pg_serial     pg_stat	   pg_subtrans	pg_twophase  pg_wal   postgresql.auto.conf  postmaster.opts
global	pg_dynshmem   pg_ident.conf  pg_multixact  pg_replslot	pg_snapshots  pg_stat_tmp  pg_tblspc	PG_VERSION   pg_xact  postgresql.conf	    postmaster.pid

Why are both containers not seeing the same database?

I would not even try to use two servers simultaneously running and using the same data folder. If you want a highly available Postgresql database, you need to replicate the data. It would be true for any relational database. So I guess it is not a Docker related issue. I don’t know how postgresql server uses locks on files or objects in the databases. Maybe the second server cannot read what the other created for safety reasons.

Docker lets you more easily run multiple servers on the same machine and use the same data, but it does mean that it is always a good idea.

What @rimelek said :slight_smile:

Also, I am currious how the compose projects are run to avoid that either of those services considered orphaned by one or the other. If they have different project names, then the prefix for the volumes will be different and therefor would actualy not use the same volume. If the project name is identical then there should be the orphaned service problem.

Can you share how you actualy run your compose project?