skip to Main Content

I have a container with postgres ( for dev purposes), on that container i did some manual changes over the database that i wish to share with other team members,

the following yml file is committed to git, i wish to commit the volume it self so all other team members could compose the container using the data i manually added. so each team member will compose the container with the data i pre-created.

is there a clean way to share the volume with other team members ??
we all use macOS

ideally i want to commit the volume to git and define a fixed volume file but not sure how to do that.

version: "3.7"
services:
  postgres-server:
    image: postgres:13
    restart: always
    env_file:
      - .env.dev
    environment:
      PGDATA: /var/lib/postgresql/data
    volumes:
      - postgres-server:/var/lib/postgres-server/data
    ports:
      - "5432:5432"


  pgadmin:
    image: dpage/pgadmin4:6.5
    restart: always
    env_file:
      - .env.dev
    environment:
      PGADMIN_LISTEN_PORT: 80
    ports:
      - "8080:80"
    volumes:
      - pgadmin:/var/lib/pgadmin



volumes:
  postgres-server:
  pgadmin:


2

Answers


  1. it’s not really how volumes work. They are not meant to be source-controlled in git. You could make a DB dump and source control that.

    For example, you could use pgdump but there are other tools that can do this too.

    pg_dump --schema-only mydb > db.sql
    

    You can then configure your compose file to mount this dump file when starting. If there is no volume with data, Postgres will use the dump to set up the DB.

    services:
      postgres-server:
        volumes:
          - ./db.sql:/docker-entrypoint-initdb.d/db.sql
          - postgres-server:/var/lib/postgres-server/data
    

    But be careful that there is no confidential data inside this dump file. That’s why in the above example, I have dumped only the schema. So no actual data is source controlled. Data itself isn’t meant for git either.

    Login or Signup to reply.
  2. You could use tar to backup the volume content. From that tar archive you could then create a new container.

    If you want to do so, have a look at
    https://stackoverflow.com/a/68230268/4222206
    It tars/untars the data, however you may want to have a step inbetween to share the tar ball with your team members.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search