Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

Why docker-compose down deletes my volume? how to avoid this action done by 'down'. (Postgresql)

when I run docker-compose down without -v or --volumes, why my data is lost? Does docker delete my volume?
When I inspect the "docker volume inspect pgdata", it shows up always the current timestamp, looks like it is always recreated each time.

But when I just stopped the postgres container docker stop containerid and re-run the container, the data is still there.

version: '3.7'
services:
app:
container_name: bsf-quiz-app
image: sapkotasuren/bsf-quiz
ports:
- "443:8443"
depends_on:
- postgresqldb
postgresqldb:
image: postgres:latest
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=bsfQuiz

volumes:
pgdata:

https://www.codepile.net/pile/YbkRvKEr

---updated 20/01/2021------------
I tried to do it on bind mount way, i see lot of data being created but still the same issue could not find the record which i have created before on the database, after docker-compose down and up.

I also tried to create named volume using docker command: docker create volume postgres, and mention it on my yaml file and set external to true. still the same issue!

yaml file can be found here: https://www.codepile.net/pile/MmbZkxmK


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's just simple that it's how docker-compose works. From their documentation

Stops containers and removes containers, networks, volumes, and images created by up.

So the named volumes are created by docker-compose up so it will be remove when you down the compose and up it again. So to keep these volumes, there is an option that you could point the volume directly to a folder so that it no longer be created by up command and it is permanent. Like so:

volumes:
- /home/<username>/.pgdata:/var/lib/postgresql/data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...