How to delete unused docker images and docker containers?

Dung Do Tien Apr 12 2021 65

I used Docker Swarm to deploy my project into Ubuntu 18.0.4. After building a new docker image. Docker created some image docker and docker container temporary and unused them. It grew very fast after building a new image.

I want to delete all of them, How can I delete them faster and correct?

Thanks for any suggestions.

Have 2 answer(s) found.
  • Q

    Quyền Thọ Kim Quang Apr 12 2021

    I usually using the command below to delete all docker images and docker ps was unused.

     docker rm -v $(docker ps -a -q -f status=exited) && docker rmi $(docker images -f "dangling=true" -q)

    After run that command you can see result as below:

     [email protected]:~# docker rm -v $(docker ps -a -q -f status=exited) && docker rmi $(docker images -f "dangling=true" -q)
    e601f9ba3096
    b8f5a15579c5
    5ad85ff6d626
    1edac133b26e
    Deleted: sha256:65e9582050ee0dc97a0eb2d35d955a48845b76f7ae7a6c69a35bd261a9cc7e66
    Deleted: sha256:21120bd11e7070791a2754db349c9152acc0a3efa86b18fecac27086c81359c9
    Deleted: sha256:aee1aeda6e9dc724360864c44e42aec5123605bb1acf64a0d589dbddc358d16b
    Deleted: sha256:a7e1b66d00de62c1e64a91051d2284bdb6ba58245de96c2cb83ed54df783319c
    Deleted: sha256:abc14a8a0323073cbf9bb3847af592e6c5342a2c6f81e94a9cc259280a72b23c
    Deleted: sha256:394ab7ff812019ef3c7da71851ae12ce8189ba6e151cfb7758494a3cc6311bb9
    Deleted: sha256:2c7a3b5993925823265faf2572b280e4cd63dda92961d11908ea9a3ac1734ca6
    Deleted: sha256:795c92298c2b6b1dca3e90c3614b19ee5c29deec270e98f1f8e478306a1f3550
    Deleted: sha256:fd1798672596e15ea550ebaf7d016aa78b5e22ad598b5b79a7f20c3eac600385
    Deleted: sha256:93a343eb0785f8b178a9a3c4287ca74cec2ab337da32026f5cb515ce5cde1c6a
    Deleted: sha256:ac0f8320f9115ccfa8bdd9550906d19fe32114a1e95adea3a2bb08b6d0ccb23b
    Deleted: sha256:f130e74af7b79e85939bbebc545a94229819666eafaafa251407fe6142e719a4
    Deleted: sha256:aa6fd271c8ce26d5386c49e61d95ecccccdd16b02dc84cf397f5283bb1a65e9f
    Deleted: sha256:0e1d65870d10e94866e3cf3303e525f6d0355c969093441ac4832a735ccd27d8
    Deleted: sha256:795f5bcdbff4b9aaab20bbba7449378645512f4bac0bf594ee9d2a19f5c88791
    Deleted: sha256:ff7c540ea2e3538d2f5e3dafbbd41f20619f983ff6404e8eb53cfca191323fbd
    Deleted: sha256:691080abd91818ceec85619c19114665375882593820cf1ceb1e448d3cdb83ae
    Deleted: sha256:74adff24b5fd1089f21e548b9b4f7dbae751b0f5af65a77f00e91d5be8aeb942
    Deleted: sha256:65b48f8b0bc9145a22749ff37df76bfe594882633e6ef8504a474dc5c5aa17d7
    Deleted: sha256:a4ffa15d6bda11c932fb45e8757958279bae201621cddac294ec20f38707058d
    Deleted: sha256:6a557fea2c45e011947c845853f23643e083d82dd139e768bb314ea7828d799a
    Deleted: sha256:311b934a33fe55ac95a5ab4fd56b4a47d38e72dadb5e3fdd3bbe54596b042a8e
    Deleted: sha256:4f00507f64c0a041726fb9592d5cfd98fda58271b8cf99cebe4b35dbb8616a67
    Deleted: sha256:a616c80ce1bf7ca84f14dbd9f272e6aee10eb78309ff2d4598abc25a256d4e97
    Deleted: sha256:44f936b73f043a7cc84b66d4eea85b1c860d6c331966de546f1b6d54184571d0
    Deleted: sha256:5883ba8a449304ae3953ee0afe42ca042d4156a7e2be0c580ad81bc6bdd412b9
    Deleted: sha256:26c486493f9911420cdefa0d8fa9853335f3e2d01b01eea715108dc76cfd0ef4
    Deleted: sha256:dbc20a17a0c23a013ab4563ddd6fc4a65494410364f4595ad0ae67ee8a6d2c66
    Deleted: sha256:6c51ea37fb741dc9e0b0143b7c4f1163b6fa8d1a28c6ac5a887021aa8447f961
    Deleted: sha256:c8c12bb785f957f44d603efcb496edcedadc41148b45b72f904ad5a06b13c33c
    Error response from daemon: conflict: unable to delete af9b912bb74c (must be forced) - image is being used by stopped container 73d4803864cb

    I hope it useful for you!!

  • N

    Nhiên Hồng Phan Apr 12 2021

    1. Command docker to delete containers with status exited and unused:

     docker rm -v $(docker ps -a -q -f status=exited)

    2. Command to delete docker image not use.

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

    Hope it works for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close