Docker: Eliminar recursos no usados
En un entorno de contenedores, en donde brinda la creación y destrucción de elementos de forma continua, es importante mantener la infraestructura para caer en el caos.
Os voy a explicar varias formas de limpiar diferentes elementos de un sistema de contenedores docker.
Empezamos…
Limpiar infraestructura Docker por completo
Para hacer una purga completa del sistema, de elementos que no se utilizan, da igual lo que sean, contenedores, imágenes, volúmenes o redes (incluso lo que esté en estado pendiente)…usaremos el siguiente comando:
1 |
docker system prune |
Para eliminar, a su vez, los contenedores detenidos y todas las imágenes no utilizadas, usaremos:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
root@p0:~# docker system prune -a WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N] y Deleted Containers: c7eaa0f2453b64fd2dc4cd5d19e26d6b9f7d763a1340fd08e75c6bd0b2fea427 f572003364267e69967cf1ae1882d2c6e855381392a9670c5a370281ff1320f7 170f4cb4bb519256d6cb0c3b1f4022279a96cfae2d34c270275ae5fe39ed1700 35e81f2f40fa98e384e52608829d2e036fac09ff12a3b01e71a7d3a10cf42484 d2c7b4925eb72c7292dd5cc5b1bbc3fe3dbdc34a1217f2d79d0aa1e4491ddd7e b5bb30b9c3958c7cd38c989da9214abff9ced884470fe046dfb76feb880c0944 Deleted Networks: portainer-agent_portainer_agent Deleted Images: untagged: grafana/grafana-enterprise:latest untagged: grafana/grafana-enterprise@sha256:359ceef5835439eb9d64e205e31a37d5aaccad9079c7863c41d28932a6a28271 deleted: sha256:9aea4fdaeca5e826b686f9ef2534d321befb239992283e56acf31de25dc098d6 deleted: sha256:b86e990ce0c6ccb4917570517dc42d2cbce30b3412b47d2f011cd05ebb87fd36 deleted: sha256:320f88a0c5b325173f2b78f31d213ba024dafa6c9206e7c9c43fd859f4b0abc0 deleted: sha256:71653c7effb1d35798a828604525ba4c864d7960c3d04ad6d1f4601ecd5a5179 deleted: sha256:504b8c190a691e2e6f579ae551edf30ab2a67551b0524ee6a6c40d1918586090 deleted: sha256:72cab389dc894544878ee2efca3c76c1fa63b7f4595bf1521269fab99fa20e1f deleted: sha256:f1b2fcc04e26d31e432e98f9d1ae3d3aff351a6f3ae0781526e9e2044b107b93 deleted: sha256:962ca9da50c3a9e5ffb3ce4640d8a524eb21e8f8e2b46feff55de4486d5bbac9 deleted: sha256:0283a779ef31a620efb2994e053b097cf78768d878b553ebcd632031799987d8 deleted: sha256:813c37e1009db02d383f3861a82442133d7a6d0395473b24ec553c12215493fe deleted: sha256:ac8f2b70f27b844bfee334d54d3701a739c37ffe619d8a7dbb54260e88b5fe1d untagged: portainer/agent@sha256:4e57940784bc3981a17c2605652a9ea431eb97ca62d3f61fe80b89404b5ef43b deleted: sha256:132807cd1bfcba20128d6514ed73c8372e541edec9c95c4372b6b5a9968e4a60 deleted: sha256:32c77cef77ef475634700f35afeef0f38ec20929aff3863030e9bc67e9f11fe7 deleted: sha256:c09a9ce5777363be08061ae8e7dace79cc995f398ec0a6466da852b7e5ef8cc3 deleted: sha256:7ace01b76c2c46aa4209bde8dc992637a94fcdc292b889c9e0a5df03daf6f001 deleted: sha256:2fc2682248a501254231811d20f8d98cea1a3e730a3be3e6f20401e90294c498 deleted: sha256:1627894e55f67b82225e47baae59b25240b7e7b6eb5ee2b40530ad325dc03443 untagged: ubuntu:latest untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 deleted: sha256:897590a6c564503ce2bde7a3ed12f470c45da09e97e11c4824990c6adc346246 deleted: sha256:f52ec53703896335ce4c2b2c692536c7ffc7911d32a13ba5ecb0da764c0656d5 untagged: busybox:latest untagged: busybox@sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1 deleted: sha256:486ed9d83cf31d495b07eedd7d4a2154b3a89de79a575e69f7426d05ae2f7c08 deleted: sha256:23d06a6a29cd29ac842acdb662882e6d30753a6fe6d16a5de018e931ec9a6401 untagged: monitoringartist/grafana-xxl:latest untagged: monitoringartist/grafana-xxl@sha256:c8413edfca0955884ee137f71593bd98d3f0e48400e4f13a31c5f62657fe14ef deleted: sha256:088e0747b7735b419e72f703240e98d5a745eb7eb6fbd04e9e11529d97a658be deleted: sha256:e38f9a61fa29c03d526645ee9e7d926ea5e676b236c55fe8a3922e9cdecef36e deleted: sha256:1e8feee1dc485a7843acc368e3cb52554b2c30b3f4b78969a4d82137492eb547 deleted: sha256:2b127b6317d7986ab749990f8995621b2a17e95182c7e4a79a6b1fdeb5fd9e46 deleted: sha256:2f4ee6a2e1b5dfb9236cd262e788f9d39109242ca27a4aacb583c8af66ec3ff7 Total reclaimed space: 3.228GB |
Como veis, nos mostrará la cantidad de espacio ganado.
Eliminar imágenes contenedores docker
Para limpiar imágenes podemos usar un primer comando para revisar las imágenes descargadas:
1 |
docker images -a |
Y luego usar el siguiente comando:
1 |
docker rmi NOMBREIMAGEN |
También nos podemos encontrar con imágenes pendientes, que podemos revisar con:
1 |
docker images -f dangling=true |
Y luego eliminar con:
1 |
docker images purge |
Si por ejemplo tenéis una gran cantidad de imágenes con un patrón concreto como httpd, podéis usar el siguiente comando para eliminarlas:
1 2 3 4 5 6 7 8 9 |
root@p0:~# docker images -a | grep "httpd" | awk '{print $3}' | xargs docker rmi Untagged: httpd:latest Untagged: httpd@sha256:fba8a9f4290180ceee5c74638bb85ff21fd15961e6fdfa4def48e18820512bb1 Deleted: sha256:584d93dba0809af1231a5f11e27c51a6866461992e18553e3c35d07247e7b398 Deleted: sha256:f55c52fd46d8ccc297cc6e976e4b3de130e78e2be1b5e41ec3b45052f31eeec5 Deleted: sha256:c7f160bec20df0f6d2c429e297ab509c0384687ffe763844deca67c4f2e1a4d7 Deleted: sha256:72c76515419b6ade2fae85a6beb2c8dd6d230ce96850823d70311cd423f30fc7 Deleted: sha256:baa7646f7143da7bf6ecf90cd155418cd5cf04bd19ef90b050d129a5a5df6932 Deleted: sha256:8ef3a0fa4b548ec905cf03905c716c13f91982199f7189e2d21530db1885fa52 |
Si queremos algo más radical y eliminar todas las imágenes:
1 |
docker rmi $(docker images -a -q) |
Eliminar contenedores Docker
Para eliminar contenedores usaremos para hacerlo de forma individual:
1 |
docker rm NOMBRE-o-ID |
Para que se ejecute y se elimine podemos usar –rm al generarlo:
1 |
docker run --rm ubuntu |
Eliminar todos los contenedores terminados:
1 2 3 4 5 6 7 |
root@p0:~# docker ps -a -f status=exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 174206e70d5b ubuntu "bash" 17 seconds ago Exited (0) 15 seconds ago funny_ptolemy 8e5ea60d8ea3 ubuntu "bash" 28 seconds ago Exited (0) 25 seconds ago serene_gates root@p0:~# docker rm $(docker ps -a -f status=exited -q) 174206e70d5b 8e5ea60d8ea3 |
Eliminar contenedores según un patrón:
1 |
docker ps -a | grep "ubuntu" | awk '{print $3}' | xargs docker rmi |
Eliminar todos los contenedores. Deberemos pararlos antes:
1 2 |
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) |
Eliminar volumen de contenedores Docker
Para eliminar volúmenes, haríamos lo mismo que con los contenedores:
1 |
docker volume rm VOLUME-NOMBRE |
Eliminar volúmenes en estado pendiente:
1 |
docker volume prune |
Eliminar un volumen y su contenedor asociado:
1 |
docker rm -v ID-NOMBRE-CONTENEDOR |
Te ha gustado la entrada SGUENOS EN TWITTER O INVITANOS A UN CAFE?
Excelente. Salvaste mi maquinita. Gracias!