Docker is a software for automating the deployment and management of applications in an operating system-level virtualization environment. It allows you to package an application with all its environment and dependencies into a container that can be ported to any Linux system with kernel cgroups support and provides a container management environment

file

The Docker installation guide can be found here.

Clearing all unused or unrelated images, containers

Docker has a command responsible for these actions:

docker system prune  
root@kvmde68-19464:~# docker system prune  
WARNING! This will remove:  
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] 

To remove all stopped containers and unused images, you should add the -a flag to this command:

docker system prune -a  
root@kvmde68-19464:~# docker system prune -a  
WARNING! This will remove:  
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y  
Deleted Images:  
untagged: ubuntu:latest  
untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7  
deleted: sha256:1d622ef86b138c7e96d4f797bf5e4baca3249f030c575b9337638594f2b63f01  
deleted: sha256:279e836b58d9996b5715e82a97b024563f2b175e86a53176846684f0717661c3  
deleted: sha256:39865913f677c50ea236b68d81560d8fefe491661ce6e668fd331b4b680b1d47  
deleted: sha256:cac81188485e011e56459f1d9fc9936625a1b62cacdb4fcd3526e5f32e280387  
deleted: sha256:7789f1a3d4e9258fbe5469a8d657deb6aba168d86967063e9b80ac3e1154333f

Removing Docker images

  • Deleting one or more specific images

The command docker images with the flag -a, will find the identifier of the images to be deleted, this command will show you all the images, including the intermediate image layers

docker images -a  
root@kvmde68-19464:~# docker images -a  
REPOSITORY TAG IMAGE ID CREATED SIZE  
debian latest 5971ee6076a0 3 weeks ago 114MB  
ubuntu latest 1d622ef86b13 6 weeks ago 73.9MB  
centos latest 470671670cac 4 months ago 237MB

When you have decided on the images you want to delete, you can pass their identifiers or tags to docker rmi:

docker rmi ubuntu  
root@kvmde68-19464:~# docker rmi ubuntu  
Untagged: ubuntu:latest  
Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7  
Deleted: sha256:1d622ef86b138c7e96d4f797bf5e4baca3249f030c575b9337638594f2b63f01  
Deleted: sha256:279e836b58d9996b5715e82a97b024563f2b175e86a53176846684f0717661c3  
Deleted: sha256:39865913f677c50ea236b68d81560d8fefe491661ce6e668fd331b4b680b1d47  
Deleted: sha256:cac81188485e011e56459f1d9fc9936625a1b62cacdb4fcd3526e5f32e280387  
Deleted: sha256:7789f1a3d4e9258fbe5469a8d657deb6aba168d86967063e9b80ac3e1154333f  
  • Removing images that are not bound to a container

Docker images consist of multiple layers. Unlinked images are layers that are not linked to any tagged images. They have no purpose and just take up disk space. To find them you can use the filter flag -f with the value dangling=true in the command docker images

docker images -f dangling=true  

Deletion:

docker images purge  
  • Deleting images by pattern

You can find all images matching a certain pattern by using a combination of docker images and grep commands

docker images -a | grep "centos"  
root@kvmde68-19464:~# docker images -a | grep "centos"  
centos latest 470671670cac 4 months ago 237MB  

You can remove them by using awk to pass IDs to docker rmi

docker images -a | grep "centos" | awk '{print $3}' | xargs docker rmi  
root@kvmde68-19464:~# docker images -a | grep "centos" | awk '{print $3}' | xargs docker rmi  
Untagged: centos:latest  
Untagged: centos@sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700  
Deleted: sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee  
Deleted: sha256:0683de2821778aa9546bf3d3e6944df779daba1582631b7ea3517bb36f9e4007  
  • Deleting all images

To display all Docker images on the system, you must add the -a flag to the docker images command

docker images -a  
root@kvmde68-19464:~# docker images -a  
REPOSITORY TAG IMAGE ID CREATED SIZE  
debian latest 5971ee6076a0 3 weeks ago 114MB  
ubuntu latest 1d622ef86b13 6 weeks ago 73.9MB  

If you want to remove all images, add the -q flag to pass the image id to docker rmi:

docker rmi $(docker images -a -q)  
root@kvmde68-19464:~# docker rmi $(docker images -a -q)  
Untagged: debian:latest  
Untagged: debian@sha256:4ab3309ba955211d1db92f405be609942b595a720de789286376f030502ffd6f  
Deleted: sha256:5971ee6076a06b695a62d8dbb5e4c977f2db1e45902f5bb8d4b74511d9649dde  
Deleted: sha256:8c02234b86056c009036ff0c31efb9a726412392d9872dacf95103767ac3b101  
Untagged: ubuntu:latest  
Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7  
Deleted: sha256:1d622ef86b138c7e96d4f797bf5e4baca3249f030c575b9337638594f2b63f01  
Deleted: sha256:279e836b58d9996b5715e82a97b024563f2b175e86a53176846684f0717661c3  
Deleted: sha256:39865913f677c50ea236b68d81560d8fefe491661ce6e668fd331b4b680b1d47  
Deleted: sha256:cac81188485e011e56459f1d9fc9936625a1b62cacdb4fcd3526e5f32e280387  
Deleted: sha256:7789f1a3d4e9258fbe5469a8d657deb6aba168d86967063e9b80ac3e1154333f  

Deleting containers

  • Deleting one or more specific containers

Use the docker ps command with the flag -a to search for the names or identifiers of the containers you want to remove:

docker ps -a  
root@kvmde68-19464:~# docker ps -a  
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES  
f0fd79d66827 ubuntu "/bin/bash" About a minute ago Exited (130) 24 seconds ago magical_lamport

Deletion:

docker rm ID_or_Name  
  • Deleting a container on exit

If you create a container that you don't need after you finish using it, you can use the docker run --rm command to automatically delete it on exit.

Run and delete:

docker run --rm image_name  
  • Deleting all containers that have been exited

You can find containers with docker ps -a and filter them by status

  • created;
  • restarting;;
  • running; paused;
  • paused;
  • exited.

To see a list of containers that have been exited, use the -f flag to filter by status

docker ps -a -f status=exited  

Make sure you want to remove these containers, and use the -q flag to pass the identifiers to the docker rm command.

docker rm $(docker ps -a -f status=exited -q)  
  • Removing containers using multiple filters

Docker filters can be combined by repeating the filter flag with an additional value. The result is a list of containers that match any of the specified conditions

docker ps -a -f status=exited -f status=created  

If you want to remove all containers with Created or Exited status, you can use several filters:

docker rm $(docker ps -a -f status=exited -f status=created -q)  
  • Deleting containers by pattern

To find all containers that match a particular template, use a combination of docker ps and grep commands

docker ps -a | grep "pattern"  

When you are satisfied with the list of containers to delete, use awk and xargs to pass the identifiers to the docker rmi command

docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi  
  • Stopping and removing all containers

The command docker ps with the flag -a will list all containers on the system.

docker ps -a  

If you want to remove them, add the flag -q to pass their identifiers to the docker stop and docker rm commands:

docker stop $(docker ps -a -q)  
docker rm $(docker ps -a -q)  
root@kvmde68-19464:~# docker stop $(docker ps -a -q)  
f0fd79d66827  
root@kvmde68-19464:~# docker rm $(docker ps -a -q)  
Error response from daemon: You cannot remove a running container f0fd79d66827b9b9e37b88048b18309eeb450a81823ffaa4c66046c4b93bb299. Stop the container before attempting removal or force removal


If you have difficulties in setting up or have additional questions, you can always contact our support team via ticket system.

Updated June 8, 2020