Home > Enterprise >  remove a volume if exists
remove a volume if exists

Time:12-28

As part of my CICD deployment, there is a volume my_volume that gets created on docker-compose build/up, that needs deleting every deployment. Therefore the CICID script calls docker volume rm my_volume before docker-compose build/up. But if a build fails, subsequent builds will error out on docker volume rm my_volume, because the volume doesn't exist.

How can I remove this volume only if it exists?

CodePudding user response:

In order to ignore failure while calling "docker volume rm my_volume" use the below order-

set e docker volume rm my_volume docker-compose build/up true

CodePudding user response:

You can ignore the errors:

docker volume rm ${some_volume} || true

Or you can start by making sure the project is completely down:

docker-compose down -v
docker-compose up -d

Or you can start labeling your volumes and containers when testing, and prune those labels:

docker volume prune -f --filter 'label=ci-test'
  • Related