Docker之批量删除镜像/容器脚本

    使用一段时间Docker以后,本地会有不少的没用了的镜像,逐条手动删除很费时,因此弄个脚本批量删除,以下,删除以192.168.33.10开头的镜像名称,只要这个镜像没有被使用,会被删除。mysql

    List-1sql

docker images|awk '{print $1":"$2}'|grep 192.168.33.10|xargs -t docker rmi

    以下如果,除了mysql、postgresql、kibana、elastic、mongo除外的没有在运行的容器会被删除,xargs的-t参数会打印出执行的命令docker

    List-2bash

docker ps -a|egrep -v 'mysql|post|kiban|elas|mongo'|awk '{print $1}'|xargs -t docker rm

    有些时候会有些<none>的镜像,以下List-3,多是临时产生的,删除这种镜像,直接用List-1中的是不行的post

    List-33d

mjduan@mjduan:/opt/tmp/images$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
<none>                                 <none>              fb522ae76d1c        3 days ago          993MB
<none>                                 <none>              4966b6e23631        3 days ago          662MB
<none>                                 <none>              3d30ff829e3d        3 days ago          738MB
<none>                                 <none>              82d66f605ccd        3 days ago          738MB
<none>                                 <none>              53df78f6d849        3 days ago          683MB
<none>                                 <none>              dd914a092541        3 days ago          738MB

    List-4postgresql

#这种,注意awk中要用\t隔开,后面的awk才能获得咱们想要的$2
docker images|awk '{print  $1"\t"$3}'|grep "<none"|awk '{print $2}'| xargs -t docker rmi
#或者下面这种
docker images|grep "<none"|awk '{print $3}'|xargs -t docker rmi
相关文章
相关标签/搜索