Stop a docker swarm stack and remove volumes

I often want to restart a stack with clean volumes. In swarm mode, docker stack down stops containers (services) and removes the network, but keeps the volumes, so you need to remove the volumes with docker volume rm.

Volumes cannot be removed until a stack has been completely shutdown. If you remove a volume that is still attached to a container, the following error is thrown:

Error response from daemon: unable to remove volume: remove mystack_myvolume: volume is in use.

Since it is quite annoying to retry this manually, you can use the following threeliner to stop your stack and remove the associated volumes:

docker stack down mystack
sleep 1 | docker volume ls | grep mystack | awk '{print $2}' | xargs docker volume rm -f
while [ $? -ne 0 ]; do !!; done
Advertenties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s