Search code examples
postgresqldockerdocker-composepersistenceconfiguration-files

Postgres Docker Persistence


I'm getting started with dockers and I'm not understanding something. I'm just using a docker-compose to use the base image of PostgreSQL, but i don't know how to make the data in the db persistent after killing the docker or even removing my local image. Is any of this possible?

This is my actual docker-compose.yml:

version: "3"
services: 
  pintaecolorea_bd:
    image: postgres
    environment:
      POSTGRES_PASSWORD: <PASSWORD>
      POSTGRES_USER: postgres
    ports: 
      - "1234:5432"
    networks: 
      - "service"
networks:
  service:

Maybe I should use volumes? How?


Solution

  • Whenever you use a image. Look at its documentation on http://hub.docker.com/. The image you are using has documentation on http://hub.docker.com/_/postgres

    The documentation mentions that data is persisted in /var/lib/postgresql/data

    version: "3"
    services: 
      pintaecolorea_bd:
        image: postgres
        environment:
          POSTGRES_PASSWORD: <PASSWORD>
          POSTGRES_USER: postgres
        ports: 
          - "1234:5432"
        networks: 
          - "service"
        volumes:
          - ./data:/var/lib/postgresql/
    networks:
      service:
    

    So you map ./data, data folder in the current folder to /var/lib/postgresql/. When the container ends the volume will be persisted