Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for unuse images for your docker containers?

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?

like image 424
code-8 Avatar asked Nov 21 '17 13:11

code-8


Video Answer


3 Answers

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)
like image 91
nickgryg Avatar answered Sep 21 '22 20:09

nickgryg


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.

like image 41
GPuri Avatar answered Sep 20 '22 20:09

GPuri


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.

like image 45
odk Avatar answered Sep 19 '22 20:09

odk