13

enter image description here

I have 3 containers, but I have a lot of images as you can see in the image.


⚡️ docker images

REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
sscportalsmb_smb-portal        latest              ad0854c799f6        12 days ago         17.6MB
bheng_web                      latest              d5a0ea011c0a        2 weeks ago         182MB
<none>                         <none>              957c22ababec        2 weeks ago         182MB
docker_web                     latest              70b443ed0495        2 weeks ago         182MB
bheng_app                      latest              509d58a68224        2 weeks ago         756MB
docker_app                     latest              509d58a68224        2 weeks ago         756MB
mysql                          5.6                 96dc914914f5        2 weeks ago         299MB
sscportalapi_ssc-portal-api    latest              e8295f9cb5f1        4 weeks ago         160MB
sscportaladmin_admin-portal    latest              fd141ceba4d6        4 weeks ago         17.7MB
mysql                          latest              5fac85ee2c68        5 weeks ago         408MB
redis                          latest              1fb7b6c8c0d0        6 weeks ago         107MB
alpine                         3.6                 76da55c8019d        2 months ago        3.97MB
keymetrics/pm2-docker-alpine   6                   4a09bfc067d6        4 months ago        75.3MB
andrewmclagan/nginx-hhvm       latest              ec6cc741eb0e        7 months ago        580MB
nginx                          1.10                0346349a1a64        7 months ago        182MB
tutum/haproxy                  latest              33bc771bec1e        17 months ago       232MB
php                            7.0.4-fpm           81d7a2fdc6dc        20 months ago       494MB

I am not sure if this is normal.

How do I check for images that I don't use and get rid of them accordingly?

code-8
  • 54,650
  • 106
  • 352
  • 604
  • `images that I don't use` is little vague..Can you run docker ps,this outputs only running containers,so any container not running may be of not usefull – TheGameiswar Nov 21 '17 at 13:35

5 Answers5

13

Above answers help us find and remove the dangling images,, but not unused.

So to fetch all the unused docker images on the machine

  1. Fetch all the images belonging to the running containers(which are not stopped or exited)

  2. Fetch all the images on the machine

  3. Then filter the images in step 1 from step 2

Below is the basic shell script which could help do that

runningImages=$(docker ps --format {{.Image}})
docker images --format {{.Repository}}:{{.Tag}} | grep -v "$runningImages"

Just be sure before removing unused images(not the dangling ones) just list them and then decide which one to delete manually.

GPuri
  • 495
  • 4
  • 11
8

You can find unused images using the command:

docker images -f dangling=true

and just a list of their IDs:

docker images -q -f dangling=true

In case you want to delete them:

docker rmi $(docker images -q -f dangling=true)
nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • 25
    This is wrong. An image can be unused yet not dangling. This will not find images that are unused but tagged. – AndreKR Jul 26 '19 at 20:22
4

Docker added special commands for this not so long ago: docker image prune -> https://docs.docker.com/engine/reference/commandline/image_prune/ for removing unused images and docker container prune -> https://docs.docker.com/engine/reference/commandline/container_prune/ for stopped containers.

odk
  • 2,144
  • 1
  • 16
  • 10
  • 6
    The question is how to list them (and subsequently delete them); before modifying anything, there needs to be a way to verify what that action will do. – Daniel Jul 09 '21 at 17:10
1

Check before using docker image prune

docker image prune only removes dangling images that don't have any containers based on them.

Script to check the affected images:

runningImages=$(docker ps -a --format "< {{.Image}} >" && docker ps -a --format "< {{.Image}}:latest >") # Latter command is to include images without any tags determined
docker images -f dangling=true --format 'table < {{.Repository}}:{{.Tag}} >\t< {{.ID}} >\t' | grep -v "$runningImages"

Check before using docker image prune -a

docker image prune -a removes all images that don't have any containers based on them.

Script to check the affected images:

runningImages=$(docker ps -a --format "< {{.Image}} >" && docker ps -a --format "< {{.Image}}:latest >") # Latter command is to include images without any tags determined
docker images --format 'table < {{.Repository}}:{{.Tag}} >\t< {{.ID}} >\t' | grep -v "$runningImages"

(It has some improvements and fixes to @GPuri's answer, e.g lists images from all containers not only running ones and surrounds by < > to ensure searching the whole phrase and also checks for containers created by an image's ID instead of name:tag.)

0

Comparing the result of docker images and of docker ps you should be able to understand what images are used in a running container. The second column of the output of docker ps is indeed the image used.

Galuoises
  • 2,630
  • 24
  • 30