2

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?

Xhark
  • 781
  • 1
  • 9
  • 24

1 Answers1

6

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • I couldn't get this to work, but found another [post](https://stackoverflow.com/questions/41637505/how-to-persist-data-in-a-dockerized-postgres-database-using-volumes) that explains why - the local directory should map to /var/lib/postgresql/data – sxf Apr 21 '21 at 09:42