Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see: https://github.com/chadoe/docker-cleanup-volumes

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

delete networks

$ docker network ls  
$ docker network ls | grep "bridge"   
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

Resize disk space for docker vm

$ docker-machine create --driver virtualbox --virtualbox-disk-size "40000" default
@t-hofmann

This comment has been minimized.

Copy link

@t-hofmann t-hofmann commented Apr 3, 2017

regarding bridge networks I find the following modification more robust:

docker network ls | awk '$3 == "bridge" && $2 != "bridge" { print $1 }'

In this example I do not want to delete the network named ($2) "bridge", if I am not mistaken that network is predefined by docker.

@blankdots

This comment has been minimized.

Copy link

@blankdots blankdots commented Apr 7, 2017

What about pruning the networks https://docs.docker.com/engine/reference/commandline/network_prune/:
$ docker network prune
And docker system prune https://docs.docker.com/engine/reference/commandline/system_prune/:
$ docker system prune

They seem less distructive.

@ibussieres

This comment has been minimized.

Copy link

@ibussieres ibussieres commented May 3, 2017

@blankdots those are great, but are only available as of 1.13.

@professordennis

This comment has been minimized.

Copy link

@professordennis professordennis commented Sep 28, 2017

To delete volumes, use: docker volume rm $(docker volume ls -f 'dangling=true')
The "-f" parameter is necessary for describe the filter ('dangling=true'). [[]]'s

@thebaconguy

This comment has been minimized.

Copy link

@thebaconguy thebaconguy commented Oct 6, 2017

$ docker system prune -a

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 images without at least one container associated to them
Are you sure you want to continue? [y/N] y

----> from documentation example

I was looking for a clean everything (almost, see EDIT below) not running or in use command to do a clean reset before installing Rancher to manage the entire system (highly recommend watching the demo and trying it out btw!). Hope this comes in handy for someone else looking for a fresh start! You can still use the filtering arguments to prune only resources matching a certain label or before a certain timestamp.

Thanks to @blankdots for the suggestion above to look into the docker system prune commands!

EDIT: This does not seem to include removal of unused/dangling volumes, so @professordennis's suggestion is still best to use for that case:

$ docker volume rm $(docker volume ls -f dangling=true -q)

----> also found from this documentation

@zenglian

This comment has been minimized.

Copy link

@zenglian zenglian commented Nov 2, 2017

how to clean it before starting docker? docker cannot be started due to no space.

@demorphica

This comment has been minimized.

Copy link

@demorphica demorphica commented Dec 18, 2017

just faced an inode exhaustion issue on ext4 & freed up ~18.5 million inodes out of 19 million total inodes using the image cleanup command.
Thank you!

@tindvik

This comment has been minimized.

Copy link

@tindvik tindvik commented Jan 4, 2018

@thebaconguy If you want to remove volumes with the prune command you can run this command:
docker system prune -a --volumes

@ZerooCool

This comment has been minimized.

Copy link

@ZerooCool ZerooCool commented Feb 1, 2018

I use tutum/lamp image docker. ( 500 Mo )
Then, i update upgrade in the shell
Then, i add php curl
I save the image, i have a file with 750Mo.

Then, i import this file.
update upgrade and clean
I save this, and, the new image is fat ( 950 Mo )

How i can clean my file and have a good size ( 500 / 600 mo ? )

More Docker command in French : https://www.visionduweb.eu/wiki/index.php?title=Ajouter_Docker_sur_GNU_Linux

@cies

This comment has been minimized.

Copy link

@cies cies commented Feb 23, 2018

Small comment: first remove the containters, then remove the images. Just a small re-order, but it saves a potential error (as images that are bound to contains cannot be deleted).

@Genovo

This comment has been minimized.

Copy link

@Genovo Genovo commented May 2, 2018

docker system info
will give the lay of the land

@kurtwheeler

This comment has been minimized.

Copy link

@kurtwheeler kurtwheeler commented Jun 27, 2018

I came here to add the same comment as @cies. Running the commands in the order listed always results in me going back to the images command after I run the containers command.

@MartinThoma

This comment has been minimized.

Copy link

@MartinThoma MartinThoma commented Jul 12, 2018

Stack Overflow: How to clean up Docker is what I was searching when I came here.

@rbucker

This comment has been minimized.

Copy link

@rbucker rbucker commented Jul 12, 2018

Nice work... what is the best way to clean up a swarm cluster?

@americofreitasjr

This comment has been minimized.

Copy link

@americofreitasjr americofreitasjr commented Sep 25, 2018

Muito bom!

@strarsis

This comment has been minimized.

@SebastianPozoga

This comment has been minimized.

Copy link

@SebastianPozoga SebastianPozoga commented Oct 26, 2018

I prepared docker image to automate it. The image delete unused images and conatainers only.
https://github.com/SebastianPozoga/docker-cleaner

Docker Hub

@kolyabres

This comment has been minimized.

Copy link

@kolyabres kolyabres commented Nov 6, 2018

resize disk for users that use dinghy

docker-machine rm dinghy                                          //delete the current virtual machine
dinghy create --provider virtualbox --disk=100000  // create the new one
@pabloleone

This comment has been minimized.

Copy link

@pabloleone pabloleone commented Nov 17, 2018

don't you feel Docker has way too many flags and options... wouldn't be easier if all efforts were focused on docker-composer and restrict the yml configuration leaving the freedom for the dockerfiles?
I feel docker is overengineered and misplanned. The learning curve isn't because of the complexity of the system, it's because the bad UX.

@irizzant

This comment has been minimized.

Copy link

@irizzant irizzant commented Mar 6, 2019

docker system prune -fa --volumes will do the trick without prompting for confirmation.
Remove -f if you want to be prompted for confirmation

@reg2005

This comment has been minimized.

Copy link

@reg2005 reg2005 commented Mar 18, 2019

For docker swarm just create stack:

version: '3.3'
services:
  system-prune:
    image: docker:latest
    command: docker system prune --all --force
    volumes:
     - /var/run/docker.sock:/var/run/docker.sock
    networks:
     - default
    logging:
      driver: json-file
    deploy:
      mode: global
      restart_policy:
        delay: 86400s
networks:
  default:
    driver: overlay

Thanks for this @Monokai, original comment

@dylanh724

This comment has been minimized.

Copy link

@dylanh724 dylanh724 commented Apr 4, 2019

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

"docker rmi" requires at least 1 argument."

But I have tons of old images dating back years ago?

image

EDIT: This worked,

docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' \
    | grep ' months' | awk '{ print $1 }' \
    | xargs --no-run-if-empty docker rmi
@vojtech-cerveny

This comment has been minimized.

Copy link

@vojtech-cerveny vojtech-cerveny commented Apr 29, 2019

Not sure, if docker-swarm works properly. @reg2005 - it removes it after start, but it doesn't do it periodically. Can you please describe how you restart this service? Do you have cron for recreate service.

I think that

      mode: global
      restart_policy:
        delay: 86400s

doesn't work properly.

@huiyonghkw

This comment has been minimized.

Copy link

@huiyonghkw huiyonghkw commented Aug 12, 2019

//clear all

$docker kill $(docker ps -q) ; docker rm $(docker ps -a -q) ; docker rmi $(docker images -q -a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.