Docker Cleanup Commands

I've been working quite a bit with Docker these last few weeks and one thing that I found really annoying was all these unused containers and images taking up precious disk space.

I wish Docker has a 'docker clean' command that would delete stopped containers and untagged images. Perhaps sometime in the near future as the project is very active. But for the time being, these commands should do the job.

Kill all running containers

docker kill $(docker ps -q)

Delete all stopped containers (including data-only containers)

docker rm $(docker ps -a -q)

Delete all 'untagged/dangling' (<none>) images

docker rmi $(docker images -q -f dangling=true)

Delete ALL images

docker rmi $(docker images -q)

It might also be useful to create bash aliases for these commands, for example:

# ~/.bash_aliases

# Kill all running containers.
alias dockerkillall='docker kill $(docker ps -q)'

# Delete all stopped containers.
alias dockercleanc='printf "\n>>> Deleting stopped containers\n\n" && docker rm $(docker ps -a -q)'

# Delete all untagged images.
alias dockercleani='printf "\n>>> Deleting untagged images\n\n" && docker rmi $(docker images -q -f dangling=true)'

# Delete all stopped containers and untagged images.
alias dockerclean='dockercleanc || true && dockercleani'


Update (9/9/2015): 

Also check out this script if your /var/lib/docker directory seems to be getting unusually big as there may be a bunch of orphaned Docker volumes that can be cleaned up. We ran into this issue recently where developers were starting to ran out of space on their hard drive and turned out we had so many orphaned volumes.


转载于:https://my.oschina.net/zx0211/blog/528927

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值