Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Last active December 23, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphtheninja/37d08ecec2942cb49df32c1f386b066c to your computer and use it in GitHub Desktop.
Save ralphtheninja/37d08ecec2942cb49df32c1f386b066c to your computer and use it in GitHub Desktop.
Articles, scripts related to docker and cleaning up images, containers etc

ideas and notes

  • gather a bunch of useful clean up commands from below and put into a daily cron job, which should be provisioned to all servers kkthx
  • pipe to xargs using -r, e.g. $(docker ps -a -q -f status=exited) | xargs -r docker rm -v to avoid getting docker rm errors if empty input
  • be careful about removing exited containers together with data containers, e.g. docker rm -v $(docker ps -a -q -f status=exited)
  • think about creating commands that only remove containers that exited some time ago, e.g. docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm

commands and tools

  • sudo apt-get install python-pip
  • pip install --upgrade pip
  • sudo pip install git+https://github.com/Yelp/docker-custodian.git#egg=docker_custodian
  • dcgc --help
  • remove stopped containers and unused images older than 2 days dcgc --max-image-age 2days
  • you can use --dry-run flag to test it out

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

docker system prune (available at least in docker 1.13)

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

docker system prune -a (d:o)

remove all dangling images

  • docker image prune (available at least in docker 1.13) or
  • docker rmi -f $(docker images -q -f dangling=true)

remove all dangling images and all unused images

docker image prune -a (available at least in docker 1.13-rc4)

remove all images

docker rmi -f $(docker images -q)

remove exited or dead containers

docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v

remove all containers

docker rm -f $(docker ps -aq)

remove orphaned docker volumes

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

links etc

tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment