Thiago Moraes
Thiago Moraes

Reputation: 617

How to persist data on a volume when using docker swarm mode?

New to Docker and I'm trying to set Postgres and pgadmin4 to run as a single service on docker for Mac inside a virtual machine. Everything works but as soon as I stop the service my data is gone. I'm using a named volume to persist data but probably doing something wrong. What is it?

Here's my setup:

# create my VM
docker-machine create dbvm
# set the right environment
eval $(docker-machine env dbvm)

Here's my docker-compose.yaml file:

version: '3'

services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=my_db
    volumes:
      - pgdata:/pgdata
    ports:
      - 5432:5432
  pgadmin:
    image: fenglc/pgadmin4
    ports:
      - 5050:5050
    volumes:
      - pgadmindata:/pgadmindata
volumes:
  pgdata:
  pgadmindata:

With docker-compose.yaml, I run:

docker stack deploy -c docker-compose.yaml dbstack

I can do everything on this setup, but if I run docker stack rm dbstack the data is gone after this, but the volumes still exist.

$ docker volume ls
DRIVER              VOLUME NAME
local               0c15b0b22c6b850e8768c14045da166253424dda4df8d2e13df75fd54d833412
local               22bab81d9d1de0e07de97363596b096f944752eba617ff574a0ab525239227f5
local               6da6e29fb98ad0f66d7da6a75dc76066ce014b26ea43567c55ed318fda707105
local               dbstack_pgadmindata
local               dbstack_pgdata

What am I missing?

Upvotes: 2

Views: 1425

Answers (2)

Bret Fisher
Bret Fisher

Reputation: 8596

@Idg is partially correct. postgres data lives at /var/lib/postgresql/data per the Docker Hub readme.

But for it to work in your named volume, you can't use a path on the left side, so correct value would be:

volumes:
  - pgdata:/var/lib/postgresql/data

Then the postgres data will stay in that named volume, on the node it was created on.

Upvotes: 1

ldg
ldg

Reputation: 9402

Unless you have it in some config not shown, I believe you need to map to the default data location inside the container e.g., pgdata:/var/lib/postgresql/data

Upvotes: 2

Related Questions