Monday, April 3, 2017

Docker Purge

There comes a time, when you just want to purge all the docker things from a machine. Over time, you can accumulate a bunch of garbage, and not even really realize it. While playing around at work and at home, I came up with this little script I use pretty often to clear out my working Docker environment on a system.

#!/bin/sh
containers=$(docker ps -a -q)
if [ ! -z "$containers" -a "$containers" != " " ]; then
  echo Stopping $containers
  docker stop $containers
  docker rm $containers
else
  echo No containers running
fi

images=$(docker images -a -q)
if [ ! -z "$images" -a "$images" != " " ]; then
  echo Removing Images $images
  docker rmi $images
else
  echo No images installed
fi

docker volume prune -f
docker network prune -f

This can be used on Linux, Bash for Windows, and even CGWyn. Basically, stop all containers, remove them, remove all images, prune all volumes, and prune all networks.

If you have any improvements to this, let me know down below in the comments!

1 comment:

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.