如何快速的删除docker镜像或者不用的docker容器

avatar 2021年5月1日18:01:02 评论 1,253 次浏览

本文主要针对docker镜像。docker不使用的容器做批量删除操作,我们先说一下docker镜像,镜像拉去时我们只需要找到同一项目中所有拉去的镜像做一下循环即可,不过这个方法也同样可以用到docker容器中,看下面的例子:

[wolf@wulaoer.org 🔥🔥🔥🔥 ]# docker images
REPOSITORY                                                          TAG                 IMAGE ID            CREATED             SIZE
<none>                                                              <none>              e48eec728e32        5 days ago          1.43 GB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage        268                 65f9e5cceaf3        7 days ago          35.3 MB
<none>                                                              <none>              83a5edac254d        7 days ago          35.3 MB
<none>                                                              <none>              eb7e00ca8825        7 days ago          35.3 MB
<none>                                                              <none>              73f8ba6565b1        3 weeks ago         35.2 MB
<none>                                                              <none>              041d25111c69        3 weeks ago         1.18 GB
<none>                                                              <none>              96ad6b77252e        4 weeks ago         359 MB
<none>                                                              <none>              5abee51785b5        4 weeks ago         359 MB
<none>                                                              <none>              6aed582d7a17        6 weeks ago         35 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage        208                 9be866248ab8        6 weeks ago         35 MB
<none>                                                              <none>              8f556a83dbfa        6 weeks ago         35 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage        201                 05c45a78911e        6 weeks ago         35 MB
<none>                                                              <none>              09583d094dc4        6 weeks ago         35 MB
<none>                                                              <none>              e3c018ec2005        7 weeks ago         35 MB
<none>                                                              <none>              2200925f29f4        7 weeks ago         659 MB
<none>                                                              <none>              9c1c8c19ae50        7 weeks ago         659 MB
<none>                                                              <none>              bb7835c3f573        7 weeks ago         659 MB
<none>                                                              <none>              7211604c3bb2        7 weeks ago         659 MB
<none>                                                              <none>              91d2cdc8964a        7 weeks ago         659 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-shaky         115                 0e403e3f2ab5        8 weeks ago         29.2 MB
<none>                                                              <none>              e695e36a2c8b        8 weeks ago         242 MB
<none>                                                              <none>              23bfebdfeafa        8 weeks ago         659 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/etl-base                 latest              7b290fb83619        8 weeks ago         659 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/airflow-base             latest              fda628a59701        8 weeks ago         835 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/create-center-api   19                  50130b0f792c        8 weeks ago         293 MB
<none>                                                              <none>              57e5217acdd8        2 months ago        204 MB
<none>                                                              <none>              6d0c4e9f84af        2 months ago        204 MB
<none>                                                              <none>              818ab2b31f49        2 months ago        1.24 GB
yearning                                                            v3.2                327fcf92df33        3 months ago        32.3 MB
aypi/v1.1.0                                                         latest              763dadd9963c        3 months ago        1.54 GB
docker.io/mysql                                                     5.7                 a70d36bc331a        3 months ago        449 MB
docker.io/mongo                                                     latest              29d9c9b800fa        3 months ago        493 MB
docker.io/python                                                    3.7                 ca194d6afe58        3 months ago        876 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops               v1.1.0              31ad9b0954ba        3 months ago        1.18 GB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops               v1.2.0              65c9610234b5        3 months ago        21.3 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops               v1.3.0              0a1b5ddc634a        3 months ago        204 MB
docker.io/alpine                                                    3.12                389fef711851        4 months ago        5.58 MB
docker.io/centos                                                    7                   8652b9f0cb4c        5 months ago        204 MB
docker.io/jayfong/yapi                                              latest              004ec0d6cdca        10 months ago       251 MB
docker.io/nginx                                                     1.19.0-alpine       7d0cdcc60a96        10 months ago       21.3 MB
docker.io/wangzan18/yearning                                        v2.2.1              c8696d2965eb        11 months ago       33.8 MB
docker.io/nginx                                                     1.17-alpine         89ec9da68213        12 months ago       19.9 MB
docker.io/node                                                      12.13.1-alpine      3fb8a14691d9        17 months ago       80.2 MB
[wolf@wulaoer.org 🔥🔥🔥🔥 ]#  docker images | grep wulaoer-dev | grep -v `docker images | grep wulaoer-dev -m 1 | awk '{print $2}'` | awk '{print $3}' | xargs docker rmi -f
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage:208
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage@sha256:d12298e8d3695a0c1eb97d2f2e181707f573ed3ddc74728ed45bb6bde033409c
Deleted: sha256:9be866248ab8a78a25829a3476b9597cb4ab7b4c68cd2ff55786312bcaf12f8e
Deleted: sha256:65ae797734e88a6552b034e584c6dea9de8f1f2c644e170979c82fa765e8a805
Deleted: sha256:b0e0f7ebf197d7867c425734c2bf58a6ad7047056137b8d0a19a5d84f5c39649
Deleted: sha256:7fe0301c3a4737444563a8fbe08597e0e99d7fff0ddc7f2f59098af4a1a1d0f9
Deleted: sha256:ae3d6264ee588ee1ef9795aa906b5bd8136fde1b2449abc3a933cce604f23cc2
Deleted: sha256:429b65e17a973d284251d3f18b1b1376fa3aaa5f38e91aeff92aefc6dc243df5
Deleted: sha256:d018b6efba3160efc77a081c040706ab819e0fb9cf9a6731a2c8e1bc7106fe12
Deleted: sha256:350ab0c095f11cfa896c8d5dcdaae9ecdc818b8f4d3296370360b2cc57767615
Deleted: sha256:ccb65dfe7402189e2b31d8c79c022c86e6ffecac83126beee85793971312230b
Deleted: sha256:63eba0f03c710bc456373a089ba3f1899aaddab9b5db9c7c3291584dc6861135
Deleted: sha256:05dfd60223af398840693987d2e6e802ff75f6677feca2304d82ff18e8a59b9d
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage:201
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage@sha256:4103012d11a6135676f05760727b7f822f00da2346776f2c6db2519f020a2626
Deleted: sha256:05c45a78911ebd7a0551d077f2d0e001638aa945178dd72750b3c29200ae7c37
Deleted: sha256:35f44c32eb3ba4a6deaa45a4e6000816e22adc815712bd5054da3ef96ff2b4f1
Deleted: sha256:3f0c2dd2fc483eb006cfb62f28bbf4cd98c2f78729062242629874818328df31
Deleted: sha256:ebad52243dc09f213f238c4fc3d61c283e7d369e56188355e7fe2118f34187e3
Deleted: sha256:6273b93e55580f1694a5931ecbdadd3d77186de3f6e0fc8355fd3698b79219cd
Deleted: sha256:ad6b50991a2e8ca3184ef84041a424c6a538085eb67bdfb73b6fc45cdade5942
Deleted: sha256:81375f86e8e6c8c4f0b3e193de2712256332726ee9eddaa0607444b378160e85
Deleted: sha256:81860b2955af43629252750ab045e4aee80d3806941f2b261060e8adcad7395b
Deleted: sha256:1fc63b4a9d6ebd413487c9351a5be8811158214c5653287602a627c9947f2bb4
Deleted: sha256:3d4f3c20ce14712e2b0d68fd0b8937fd7fe6f69fb404874d698e84dbe169a446
Deleted: sha256:be013c3908737ae10aeaec2711b10a25916458a9eac02fbe22d9e72bc954be24
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-shaky:115
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-shaky@sha256:d67e272177968fd34d63b13bc2367daf83a05903bb4554a86b0a5c45d624afe8
Deleted: sha256:0e403e3f2ab57ee687400a8d3944fe43dac77efb0fbcafc83b457d977afd6acc
Deleted: sha256:bfd77197d323218fcbdb075564d8e306308e314e109ed914bca8863f67b053d0
Deleted: sha256:f08ab0e725b706ce70a3a5f143bdfb5e8ccf131d4b231e5672c4aab5972f110c
Deleted: sha256:1751b296b90329a437c657c48864a1eb4c9284ad5cadd9593fac2b99d0a83974
Deleted: sha256:5aeacc401ffb7b22220ec5fa5e3689d3f881f622a49b31bfe1bedf36efc25570
Deleted: sha256:c21523cc36c8628d0ba2719f582663d15879a797ad12e5318423436c7d9ada54
Deleted: sha256:c9541a88d913cb9cea847f4fb82539e84470092641b6ef5d393160ad25bbe6dc
Deleted: sha256:5f45ebbffd4f72959d885f45a1304751a7f4a9527077bc1c1bceaf2202d6f11c
Deleted: sha256:79db4e53fa90512019b0b239eb3da6fd86ea14e93770fc94b5613aa1bb30cea5
Deleted: sha256:8dd7681170f3adbd7b9ea9965af85f69172cc93f250f67fea4552865020f88e4
Deleted: sha256:cc41348b20b21a1e1b5714930d69253f4faf62c3ed3b0b6f8c0df9b645f5287f
Deleted: sha256:126f81b7e4edef862928baf79ca033191f2dcba7df3bf90c4fb97507ec0778b4
Deleted: sha256:beefa9d71f658b580093645f0dcfdc241a787f12e418b411fa2bb99a90430035
Deleted: sha256:57bf46cb1f16c979742c475307809eed371d365a51e0be7ed22a4cdddef71a44
Deleted: sha256:c6665fda851ebf66b4c8cee8276aaa5070f34526966ac091eb5b1c3e7e2d8236
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/create-center-api:19
Untagged: registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/create-center-api@sha256:9c649fce39db4a4bf5c68741de85989cb27e8bc9c35b900490913146252013e8
Deleted: sha256:50130b0f792ce979e8ccf2ca032762aa1c72a561502b295dfe2e72d7c81cc2cf
Deleted: sha256:7b6eb0f6c8e125fa4ce3e173ab21d22e4fe27c4bb501d8de628e49a1aab14ea0
Deleted: sha256:c3c2e4b543e1ccdeba7e3bc27c25530be65118ab4b72ee1b021821d76d74b3e6
Deleted: sha256:7b5270aaef1533b0a8bb688e9f50e3709487a97d5bdd0de8380a619ccff207d6
Deleted: sha256:7541a473ac2557ecaca59115394326125fb1f5c4adfc0d03c31feb01f6c72268
Deleted: sha256:fc43cc0739039a641435f253e8064ad66cc1289b85671938e1f3d3398264fa0e
Deleted: sha256:d7c32a309026c98468081a258277bc5acd998485b0f6ab0acdd41e7466c99bb5
Deleted: sha256:7a1f10dbc0ca2c8dc981517f442d0a300d689bc4207566517168884663ce1769
Deleted: sha256:1bd66b270f0086df270bd51f5056fb074cc3a6d743e1e5d614d9caa0e15d5a75
Deleted: sha256:d5cc83435a9bfffb9c8e8c49fb07b60173cdb7f3f9867f1c02c0abba068f6f1e
Deleted: sha256:e77b6867ecb1e93df497461229b09d386977a7a45b0ea5f10f766635bed848d9
Deleted: sha256:c66c8a64d698600f7f1f4c7a7005683985aa1395374252e6a63191916b383224
Deleted: sha256:a3cdaf28cc66e8d93f5c86ea769fca3b2d368c92be81d2739a80705de89279b5

我们通过grep做筛选,筛选后根据版本,找出最近旧的版本,通过dockr的IMAGE ID删除镜像。命令如下:{docker images | grep wulaoer-dev | grep -v `docker images | grep wulaoer-dev -m 1 | awk '{print $2}'` | awk '{print $3}' | xargs docker rmi -f}

[wolf@wulaoer.org 🔥🔥🔥🔥 ]# docker images
REPOSITORY                                                     TAG                 IMAGE ID            CREATED             SIZE
<none>                                                         <none>              e48eec728e32        5 days ago          1.43 GB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer-dev/wulaoer-manage   268                 65f9e5cceaf3        7 days ago          35.3 MB
<none>                                                         <none>              83a5edac254d        7 days ago          35.3 MB
<none>                                                         <none>              eb7e00ca8825        7 days ago          35.3 MB
<none>                                                         <none>              73f8ba6565b1        3 weeks ago         35.2 MB
<none>                                                         <none>              041d25111c69        3 weeks ago         1.18 GB
<none>                                                         <none>              96ad6b77252e        4 weeks ago         359 MB
<none>                                                         <none>              5abee51785b5        4 weeks ago         359 MB
<none>                                                         <none>              6aed582d7a17        6 weeks ago         35 MB
<none>                                                         <none>              8f556a83dbfa        6 weeks ago         35 MB
<none>                                                         <none>              09583d094dc4        6 weeks ago         35 MB
<none>                                                         <none>              e3c018ec2005        7 weeks ago         35 MB
<none>                                                         <none>              2200925f29f4        7 weeks ago         659 MB
<none>                                                         <none>              9c1c8c19ae50        7 weeks ago         659 MB
<none>                                                         <none>              bb7835c3f573        7 weeks ago         659 MB
<none>                                                         <none>              7211604c3bb2        7 weeks ago         659 MB
<none>                                                         <none>              91d2cdc8964a        7 weeks ago         659 MB
<none>                                                         <none>              e695e36a2c8b        8 weeks ago         242 MB
<none>                                                         <none>              23bfebdfeafa        8 weeks ago         659 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/etl-base            latest              7b290fb83619        8 weeks ago         659 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/airflow-base        latest              fda628a59701        8 weeks ago         835 MB
<none>                                                         <none>              57e5217acdd8        2 months ago        204 MB
<none>                                                         <none>              6d0c4e9f84af        2 months ago        204 MB
<none>                                                         <none>              818ab2b31f49        2 months ago        1.24 GB
yearning                                                       v3.2                327fcf92df33        3 months ago        32.3 MB
aypi/v1.1.0                                                    latest              763dadd9963c        3 months ago        1.54 GB
docker.io/mysql                                                5.7                 a70d36bc331a        3 months ago        449 MB
docker.io/mongo                                                latest              29d9c9b800fa        3 months ago        493 MB
docker.io/python                                               3.7                 ca194d6afe58        3 months ago        876 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops          v1.1.0              31ad9b0954ba        3 months ago        1.18 GB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops          v1.2.0              65c9610234b5        3 months ago        21.3 MB
registry-vpc.cn-beijing.aliyuncs.com/wulaoer/wulaoer-ops          v1.3.0              0a1b5ddc634a        3 months ago        204 MB
docker.io/alpine                                               3.12                389fef711851        4 months ago        5.58 MB
docker.io/centos                                               7                   8652b9f0cb4c        5 months ago        204 MB
docker.io/jayfong/yapi                                         latest              004ec0d6cdca        10 months ago       251 MB
docker.io/nginx                                                1.19.0-alpine       7d0cdcc60a96        10 months ago       21.3 MB
docker.io/wangzan18/yearning                                   v2.2.1              c8696d2965eb        11 months ago       33.8 MB
docker.io/nginx                                                1.17-alpine         89ec9da68213        12 months ago       19.9 MB
docker.io/node                                                 12.13.1-alpine      3fb8a14691d9        17 months ago       80.2 MB

下面是针对docker的容器删除,我们这里只删除不用的docker或者很久不用的docker容器,首先我们需要区分哪些是要删除的那些事用的,我们看一下关键词Exited。

[wolf@wulaoer.org 🔥🔥🔥🔥 ]#  docker ps -a | grep Exited
34da98d44b65        5abee51785b5          "/bin/sh -c 'npm r..."   4 weeks ago         Exited (1) 4 weeks ago                                fervent_chandrasekhar
067c43780114        2200925f29f4          "/bin/sh -c 'ln -s..."   7 weeks ago         Exited (1) 7 weeks ago                                zealous_shannon
15db02106d9b        9c1c8c19ae50          "/bin/sh -c 'ln -s..."   7 weeks ago         Exited (1) 7 weeks ago                                ecstatic_torvalds
5d9f834930dc        bb7835c3f573          "/bin/sh -c 'ln -s..."   7 weeks ago         Exited (1) 7 weeks ago                                xenodochial_stallman
dad7197ead20        7211604c3bb2          "/bin/sh -c 'ln -s..."   7 weeks ago         Exited (1) 7 weeks ago                                eager_mcclintock
2ab7504a0fe5        91d2cdc8964a          "/bin/sh -c 'ln -s..."   7 weeks ago         Exited (1) 7 weeks ago                                gracious_sammet
30890bb12b25        23bfebdfeafa          "/bin/sh -c 'pip i..."   8 weeks ago         Exited (1) 8 weeks ago                                gracious_thompson
61bd8fe49c58        23bfebdfeafa          "/bin/sh -c 'pip i..."   8 weeks ago         Exited (1) 8 weeks ago                                elastic_hypatia
57fcb0ae76e2        23bfebdfeafa          "/bin/sh -c 'pip i..."   8 weeks ago         Exited (1) 8 weeks ago                                brave_kilby
682b46d5b33d        57e5217acdd8          "/bin/sh -c 'bash ..."   2 months ago        Exited (127) 2 months ago                             wizardly_aryabhata
381fdf819835        6d0c4e9f84af          "/bin/sh -c 'bash ..."   2 months ago        Exited (2) 2 months ago                               condescending_wing
96f8b612d690        8652b9f0cb4c          "/bin/sh -c 'yum -..."   2 months ago        Exited (1) 2 months ago                               elated_clarke
b3a68d176883        8652b9f0cb4c          "/bin/sh -c 'yum -..."   2 months ago        Exited (1) 2 months ago                               objective_heyrovsky

执行结果,和上面勒事

[wolf@wulaoer.org 🔥🔥🔥🔥 ]#  docker ps -a | grep Exited | awk '{print $15}' | xargs docker rm
fervent_chandrasekhar
zealous_shannon
ecstatic_torvalds
xenodochial_stallman
eager_mcclintock
gracious_sammet
gracious_thompson
elastic_hypatia
brave_kilby
wizardly_aryabhata
condescending_wing
elated_clarke
objective_heyrovsky

验证一下,很成功

[wolf@wulaoer.org 🔥🔥🔥🔥 ]#  docker ps -a | grep Exited | awk '{print $15}' | xargs docker rm
# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
4a47eacb9c6c        jayfong/yapi:latest   "docker-entrypoint..."   3 months ago        Up 2 days           0.0.0.0:40001->3000/tcp   yapi-web
01e23a852d68        mongo:latest          "docker-entrypoint..."   3 months ago        Up 2 days           27017/tcp                 yapi-mongo

没用的都删除了,后面可以看看其他都把,,,,,

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: