'Share database between local psql and docker compose postgres container

I need some advice on how to share a Postgres database between local psql and docker compose Postgres container.

I'm on a Debian-based Linux distro called Mint; stack is Django 3.2 and Postgresql 12

I have a compose file with a Docker volume and local driver:

version: "3.9"

volumes:
  db_data:
    name: db_data
    driver: local

services:
  db:
    container_name: db
    image: postgis/postgis:12-3.1
    depends_on:
      - server
    volumes:
      - db_data:/var/lib/postgresql/data:rw
    ports:
      - "5432:5432"
    env_file:
      - "./.env"

  server:
    container_name: server
    build: ./server/
    volumes:
      - ./server:/server
    ports:
      - "8000:8000"
    env_file:
      - "./.env"

Both container are running properly but my local psql doesn't see any database I created or modified:

  • when I run docker-compose exec db psql -U postgres I see my database

  • but locally impossible to link with those data

  • when I run docker-compose ps:

        Name           Command        State                    Ports                  
    ---------------------------------------------------------------------
    db       docker-entrypoint.sh postg ...   Up 0.0.0.0:5432->5432/tcp,:::5432->5432/tcp
    server   /bin/sh -c python3 manage. ...   Up      0.0.0.0:8000->8000/tcp,:::8000->8000/tcp
    
  • inside the volume db there is a whole postgres lib and bin package which is not the same that my local as i expect

I went through a lot of threads and tutorial and I worked around:

  • container networks with network_mode :'host but didn't work
  • ip addresses mapping
  • changing dependencies of containers to depends_on: - db
  • ...

I'm not that experienced, and if you could provide a workaround it would be amazing.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source