How To Remove Docker Containers, Images, Volumes in an automated way
How To Remove Docker Containers, Images, Volumes in an automated way |
When working with Docker, you can quickly accumulate a large number of unused objects that consume significant disk space. In this article i'll explain different ways to do the housekeeping and provide you a script which will help you to do this work in a automated fashion.
Docker Images Removal
When you download a Docker image, it is kept on the server until you manually remove it.
Removing an image
To remove one or more Docker images, first, you need to find the IDs of the images:
docker image ls
The output should look something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-apiserver v1.21.3 3d174f00aa39 3 weeks ago 126 MB
k8s.gcr.io/kube-scheduler v1.21.3 6be0dc1302e3 3 weeks ago 50.6 MB
k8s.gcr.io/kube-controller-manager v1.21.3 bc2bb319a703 3 weeks ago 120 MB
k8s.gcr.io/kube-proxy v1.21.3 adb2816ea823 3 weeks ago 103 MB
docker.io/weaveworks/weave-npc 2.8.1 7f92d556d4ff 6 months ago 39.3 MB
docker.io/weaveworks/weave-kube 2.8.1 df29c0a4002c 6 months ago 89 MB
k8s.gcr.io/pause 3.4.1 0f8457a4c2ec 7 months ago 683 kB
k8s.gcr.io/coredns/coredns v1.8.0 296a6d5035e2 9 months ago 42.5 MB
k8s.gcr.io/etcd 3.4.13-0 0369cf4303ff 11 months ago 253 MB
IMAGE ID
and supply it to the docker image rm
command. For example, to remove an Image you would fire this command.docker image rm 3d174f00aa39
docker rmi -f $(docker images | grep -w "<none>" | awk {print $3})
Delete dangling docker images.
docker rmi -f $(docker images -qf dangling=true)
Removing all unused images
To remove all images that are not referenced by any existing containeruse the prune
command with the -a
option:
docker image prune -a
[root@devopszones ~]#docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] ^C [root@devopszones ~]#
Delete docker images older then one month.
docker rmi -f $(docker images | awk '{print $3,$4,$5}' | grep '[5-9]\{1\}\ weeks\|years\|months' | awk '{print $1}')
Docker Containers Removal
Docker containers are not automatically removed when you stop them unless you start the container using the --rm
flag.
Removing one or more containers
To remove one or more Docker containers, use the docker container rm
command, followed by the "CONTAINER ID" of the containers you want to remove.
You can get a list of all containers by firing the docker container ls
command with the -a
option:
docker container ls -a
[root@devopszones ~]#docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fb408b69ffa7 296a6d5035e2 "/coredns -conf /e..." 29 minutes ago Up 29 minutes k8s_coredns_coredns-558bd4d5db-d7995_kube-system_eaf88a3a-bca7-42d9-af28-f7b42b0c5c30_2 ab5540625287 k8s.gcr.io/pause:3.4.1 "/pause" 29 minutes ago Up 29 minutes k8s_POD_coredns-558bd4d5db-d7995_kube-system_eaf88a3a-bca7-42d9-af28-f7b42b0c5c30_3 afb68869ce93 296a6d5035e2 "/coredns -conf /e..." 29 minutes ago Up 29 minutes k8s_coredns_coredns-558bd4d5db-4c6d7_kube-system_80603103-374d-4199-a816-473d4282f2b2_2 126f12ef1003 k8s.gcr.io/pause:3.4.1 "/pause" 29 minutes ago Up 29 minutes k8s_POD_coredns-558bd4d5db-4c6d7_kube-system_80603103-374d-4199-a816-473d4282f2b2_3 8ee926f9e11c 7f92d556d4ff "/usr/bin/launch.sh" 29 minutes ago Up 29 minutes k8s_weave-npc_weave-net-skv44_kube-system_141e11d8-2c38-4478-80ae-bb1e5865cf4a_2 b6a994dfe089 df29c0a4002c "/home/weave/launc..." 29 minutes ago Up 29 minutes k8s_weave_weave-net-skv44_kube-system_141e11d8-2c38-4478-80ae-bb1e5865
CONTAINER ID
and supply it to the docker container rm
command. For example, to remove a container you would fire this command.docker container rm fb408b69ffa7
Removing all stopped containers
To remove all stopped containers, fire the docker container prune
command:
docker container prune
[root@devopszones ~]#docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] ^C [root@devopszones ~]#
How to remove all containers
To stop all running containers, enter the docker container stop
command followed by the containers IDs:
docker container stop $(docker container ls -aq)
Once all containers are stopped, remove them using the docker container rm
command, followed by the containers ID list.
docker container rm $(docker container ls -aq)
Delete "Dead" or "Exited" containers.
docker rm $(docker ps -a | grep "Dead\|Exited" | awk '{print $1}')
Or you use Filters:
docker container ls -a --filter status=exited --filter status=created
[root@devopszones ~]#docker container ls -a --filter status=exited --filter status=created CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14510dea6dff df29c0a4002c "/home/weave/init.sh" 45 minutes ago Exited (0) 45 minutes ago k8s_weave-init_weave-net-skv44_kube-system_141e11d8-2c38-4478-80ae-bb1e5865cf4a_0 e40fdaca5d7f bc2bb319a703 "kube-controller-m..." 7 days ago Exited (2) 7 days ago k8s_kube-controller-manager_kube-controller-manager-kubemaster_kube-system_0b1d22461a35cca3c829b6cd644ac7d9_3 85120c0315be 296a6d5035e2 "/coredns -conf /e..." 7 days ago Exited (0) 7 days ago k8s_coredns_coredns-558bd4d5db-4c6d7_kube-system_80603103-374d-4199-a8
Then invoke the rm command in conjuction with it.
docker rm $(docker container ls -a --filter status=exited --filter status=created)
Docker Volumes Removal
To remove one or more Docker volumes, run the docker volume ls
command to find the ID of the volumes you want to remove.
docker volume ls
[root@devopszones]# docker volume ls
DRIVER VOLUME NAME local 5ad379121efc83f46909e4941240daa346dba16a32c824d013df6f1c0c2b5d7e local 8c53ca9396cad903193e0c3f5d88f2c34b8c1a6894436d1c569bc2fd77709982 local 966c83e3075b303f45b354167ac58a56ff9a38a658adcda40294190a3a6fd979 local 062570a4e323d66fc524bf1c5545e023aa3f8058f8f4f5e80c9b11de6c1263e0
docker volume rm
command. For example, to remove a volume you would fire this command.Removing all unused volumes
To remove all unused volumes, run the docker volume prune
command:
docker volume prune
[root@devopszones ~]#docker volume prune WARNING! This will remove all volumes not used by at least one container. Are you sure you want to continue? [y/N]
If you want to remove all dangling volumes then, you can invoke folowing command:
docker volume rm -f $(docker volume ls -qf dangling=true)
Removing All Unused Docker Resources
The docker system prune
command removes all stopped containers, dangling images, and unused networks:
docker system prune
[root@devopszones ~]#docker system prune WARNING! This will remove: - all stopped containers - all volumes not used by at least one container - all networks not used by at least one container - all dangling images Are you sure you want to continue? [y/N]
Automation
You can make use of this cleanup script to do these jobs for you.
#!/bin/sh -x result=`which docker` if [ -z $result ]; then echo "Error: DOCKER command seems not to be present in this OS." echo "System defaults are missing.Sorry, Quitting from installation" echo "Thank You" exit 1 else DOCKER=$result fi result=`which awk` if [ -z $result ]; then echo "Error: AWK command seems not to be present in this OS." echo "System defaults are missing.Sorry, Quitting from installation" echo "Thank You" exit 1 else AWK=$result fi result=`which grep` if [ -z $result ]; then echo "Error: grep command seems not to be present in this OS." echo "System defaults are missing.Sorry, Quitting from installation" echo "Thank You" exit 1 else GREP=$result fi echo -e "\n\n =========== Starting the Docker Clean Up Script ================== \n\n" echo -e "======= Checking Docker images with TAG as 'None' ======" noneImages=$($DOCKER images | $GREP -w "<none>" | $AWK '{print $3}') if [ "${noneImages}" != "" ];then for nImages in ${noneImages} do echo ${nImages} ${DOCKER} rmi -f ${nImages} >> cleanUpLog if [ $# -eq 0 ]; then echo -n "\n======= Docker image with ImageId: ${nImages} Deleted Successfully =======\n" >> /tmp/cleanUpLog else echo -n "\n======= Error while deleting Docker image with ImageId: ${nImages} =======\n" >> /tmp/cleanUpLog fi done else echo "\n====================== [Image ID with <none>]:No Docker Images to delete============ \n" fi oldContainers=$($DOCKER ps -a | $GREP "Dead\|Exited" | $AWK '{print $1}') if [ "$oldContainers" != "" ]; then echo "" for oContainers in $oldContainers do echo $j $DOCKER rm ${oContainers} >> /tmp/cleanUpLog if [ $# -eq 0 ]; then echo -n "\n ========[Dead|Exited] Docker container with ContainerID: ${oContainers} Deleted Successfully ======= \n" >> /tmp/cleanUpLog else echo -n "\n =======[Dead|Exited] Error while deleting Docker image with COntainedID: ${oContainers} =======\n" >> /tmp/cleanUpLog fi done else echo -e "\n ======= There no Docker containers with status as 'Exited' ====== \n" >> /tmp/cleanUpLog fi echo -e "======= Proceeding to next step, i.e deletion of old images which are two months old ==============" oldImages=$($DOCKER images | $AWK '{print $3,$4,$5}' | $GREP '[9]\{1\}\ weeks\|years\|months' | $AWK '{print $1}') #echo ${oldImages} >> cleanUpLog if [ "$oldImages" != "" ]; then for i in ${oldImages} do ${DOCKER} rmi -f ${i} >> /tmp/cleanUpLog if [ $# -eq 0 ]; then echo -n "\n ======= Docker image with ImageId: ${i} Delted Successfully =======\n" >> /tmp/cleanUpLog else echo -n "\n ======= Error while deleting Docker image with ImageId: ${i} ======= \n" >> /tmp/cleanUpLog fi done else echo -e "\n =================== No Docker Images to delete ================== \n" fi dangalingImages=$($DOCKER images -qf dangling=true) if [ "$dangalingImages" != "" ]; then for dImages in ${dangalingImages} do ${DOCKER} rmi -f ${dImages} >> /tmp/cleanUpLog if [ $# -eq 0 ]; then echo -n "\n ======= Docker image with ImageId: ${dImages} Delted Successfully =======\n" >> /tmp/cleanUpLog else echo -n "\n ======= Error while deleting Docker image with ImageId: ${dImages} ======= \n" >> /tmp/cleanUpLog fi done else echo -e "\n =================== No Docker dangaling Images to delete ================== \n" fi echo -e "\n ============= Clean up unused docker volumes =========================================\n" unUsedVolumes=$($DOCKER volume ls -qf dangling=true) if [ "$unUsedVolumes" != "" ]; then for uVolumes in ${unUsedVolumes} do ${DOCKER} volume rm -f ${uVolumes} >> /tmp/cleanUpLog if [ $# -eq 0 ]; then echo -n "\n ======= Docker image with ImageId: ${uVolumes} Delted Successfully =======\n" >> /tmp/cleanUpLog else echo -n "\n ======= Error while deleting Docker image with ImageId: ${uVolumes} ======= \n" >> /tmp/cleanUpLog fi done else echo -e "\n =================== No Docker dangaling Images to delete ================== \n" fi echo -n "\n\n ============================ END OF SCRIPT ============================= \n\n"
No comments