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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't get this to work, but found another post that explains why - the local directory should map to /var/lib/postgresql/data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.