Move docker’s default /var/lib/docker and clean up

to Move docker’s default /var/lib/docker to another directory on Linux

Stop theĀ  docker daemon

# systemctl stop docker

edit lib/systemd/system/docker.service and replace the following line where /new/path/docker is a location of your new chosen docker directory:

ExecStart=/usr/bin/docker daemon -H fd://
ExecStart=/usr/bin/docker daemon -g /new/path/docker -H fd:/

reload systemd daemon:

# systemctl daemon-reload

Once this is done create a new directory you specified above and optionally rsync current docker data to a new directory:

# mkdir /new/path/docker
# rsync -aqxP /var/lib/docker/ /new/path/docker

At this stage we can safely start docker daemon:

# systemctl start docker

Confirm that docker runs within a new data directory:

# ps aux | grep -i docker | grep -v grep

commands to clean up

Kill all running containers:
# docker kill $(docker ps -q)

Delete all stopped containers
# docker rm $(docker ps -a -q)

Delete all images
# docker rmi $(docker images -q)

Remove unused data
# docker system prune

And some more
# docker system prune -af

Little script to clean volumes :

for vol in $(docker volume ls | awk '{print $2}' | grep -v VOLUME)
  docker volume rm $vol