Skip to content

Instantly share code, notes, and snippets.

@bastman
Created March 31, 2016 05:55
Show Gist options
  • Save bastman/5b57ddb3c11942094f8d0a97d461b430 to your computer and use it in GitHub Desktop.
Save bastman/5b57ddb3c11942094f8d0a97d461b430 to your computer and use it in GitHub Desktop.
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
@MartinThoma
Copy link

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

@rbucker
Copy link

rbucker commented Jul 12, 2018

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

@americofreitasjr
Copy link

Muito bom!

@SebastianPozoga
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

//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