docker compose up and docker compose down work like a charm: creating everything from scratch and wiping without a trace.
That puts data in the volumes at the mercy of “fat fingers” typing down instead of stop.
Is there a setting in the docker-compose.yml to tell: don’t nuke this volume?
2
Answers
You have two options.
Option A:
Create the volume manually:
And mark it as external in your docker-compose.yaml file:
Option B:
Mount a host directory as the volume:
As I known,
docker compose down
won’t delete volume, but if you didn’t specify the volume name, it will create a new volume as default to bind.And every volume docker create won’t delete until you type
docker volume prune
, you can check bydocker volume ls
to see if there is a lot of none-name volumes.You can create a volume by
docker create volume example
, and define to use it indocker-compose.yml
, then it will use same volume on nextdocker compose up
.For example what I did:
Create volume by
docker volume create mongodb
.Edit
docker-compose.yml
.Docker document also said removing the service doesn’t remove any volumes created by the service. Volume removal is a separate step, more details see here.