How to check for unuse images for your docker containers? - docker

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?

Above answers help us find and remove the dangling images,, but not unused.
So to fetch all the unused docker images on the machine
Fetch all the images belonging to the running containers(which are not stopped or exited)
Fetch all the images on the machine
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.

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)

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.

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.

Related

Unable to remove python and ubuntu docker images

I am new to using dockers,so i found these python and ubuntu images when executing docker images and i am unable to remove it using docker rmi {image_id}.
Error response from daemon: conflict: unable to delete ba6acccedd29 (cannot be forced) - image has dependent child images
It was using up significant space,hence i was a bit concerned.
REPOSITORY TAG IMAGE ID CREATED SIZE
docker_jupyter latest 90708b9040df 28 hours ago 1.94GB
ubuntu 20.04 ba6acccedd29 7 days ago 72.8MB
testapi latest 82e970c819ff 6 weeks ago 1.33GB
python 3.9 a5210955ee89 6 weeks ago 911MB
python latest a5210955ee89 6 weeks ago 911MB
you have to delete the other images that depend on them first
I think "test_api" and "jupyter" images depends on python so u need to delete them first

Docker Image Deleted

I made a goof while trying to rename an image by following the steps on this page that say to create a tag then delete the original
Docker how to change repository name or rename image?
Now when I list the images it doesn't show up anymore. However, when I list the containers the image still shows up.
PS C:\Users\Grail> docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 157be28c0fe3 7 days ago 668MB
fedora latest a368cbcfa678 2 months ago 183MB
PS C:\Users\Grail> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
1ea5ffd50852 157be28c0fe3 "/bin/bash" 7 hours ago Exited (0) 7 hours ago
fb81990e756c 0d120b6ccaa8 "/bin/bash" 10 hours ago Exited (0) 24 minutes ago
081641b3e600 a368cbcfa678 "/bin/bash" 11 hours ago Exited (0) 31 minutes ago
Not only that, the image (0d120b6ccaa8) still shows up in my Docker Dashboard (running on Windows) and I can start/stop it without any problems.
Clearly the image still exists. Can I restore it such that I can see it when I list the images?
Can it be restored from the container?
If it's in a weird state/unrecoverable, how do I actually delete it so it's not taking up space?
Update:
Thanks to #prashanna I went down a path where I exported the container and imported to get the image:
docker export -o mycontainer.tar fb81990e756c
docker import mycontainer.tar
**docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]**
is for creating a new image from a container, meaning when you update or add new config or install new software, thus creating a new template images.
ref:docker commit

How to link container with a new image?

I am new to Docker and tried to check how to make changes in a docker container and commit them to reflect in the results. I have AWX running on Docker-CE and had created made changes in awx_web container to update the UI logo as mentioned in the Ansible Tower documentation:
docker cp custom_console_logo.png aea2551951d5:/var/lib/awx/public/static/assets
and created the local_settings.json in /var/lib/awx/public/static/ one of the running awx_web container using the following and then using the bash command to create and save file:
docker exec -it aea2551951d5 bash
Post this, i have commited the changes so that the changes are reflected permanently in the image file and in UI using:
docker commit aea2551951d5 ansible/awx_web
I have verified the changes in the container files and it appears to be fine however, the changes are not getting reflected when i verified them on browser. It appears like the existing container is pointing to the old image file:
CONTAINER ID IMAGE
aea2551951d5 b7c261b76010
I am not sure what was missed. Could anyone suggest if i have missed anything and how do i fix this?
Following are the containers:
[root#localhost~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80ab6bf562a9 ansible/awx_task:latest "/tini -- /bin/sh -c…" 13 days ago Up 3 days 8052/tcp awx_task
aea2551951d5 b7c261b76010 "/tini -- /bin/sh -c…" 13 days ago Up 3 hours 0.0.0.0:80->8052/tcp awx_web
e789a4a82a9e memcached:alpine "docker-entrypoint.s…" 13 days ago Up 3 days 11211/tcp memcached
a8c74584255c ansible/awx_rabbitmq:3.7.4 "docker-entrypoint.s…" 13 days ago Up 3 days 4369/tcp, 5671-5672/tcp, 15671-15672/tcp, 25672/tcp rabbitmq
25f6f6ca7766 postgres:9.6 "docker-entrypoint.s…" 13 days ago Up 3 days 5432/tcp postgres
Following are the Images:
[root#localhost~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ansible/awx_web latest 1accc59cbdee 2 hours ago 1.06GB
b7c261b76010 latest 998ec3e77fd8 3 hours ago 1.06GB
postgres 9.6 106bdfb062df 6 weeks ago 235MB
ansible/awx_task latest 535bb2b8e1f3 6 weeks ago 1.07GB
ansible/awx_web <none> b7c261b76010 6 weeks ago 1.04GB
hello-world latest 2cb0d9787c4d 2 months ago 1.85kB
memcached alpine b40e8fa7e3e5 2 months ago 8.69MB
ansible/awx_rabbitmq 3.7.4 e08fe791079e 5 months ago 85.6MB
[root#clab2726vw0001 ~]#
Environment Information:
Docker Version: 18.06.0-ce, build 0ffa825
AWX Version: 1.0.6.41
Ansible Version: 2.6.1
RHEL Version: Red Hat Enterprise Linux Server release 7.5 (Maipo)
Containers are the running instances of images and the container id is unique for the particular image.
We cannot use the same 12 digit container id as a reference for the other images. When we use "commit" it create a new fresh container for the new image.
This new container will contain all the changes needed.
You can try with naming the container.
Give a name to container (using docker rename command) .
Make your changes in container.
Commit it.
Give same name to new container generated for the new image.
Rename old container with some other name.
Now you have the same container name (as you were using before) but with new Image.

How can I tell which Docker image(s) are safely to remove?

When I run docker image ls I see this
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> c012c1e2eb45 4 hours ago 2.59GB
<none> <none> a2013debf751 5 hours ago 2.59GB
<none> <none> 0bbb2f67083c 5 hours ago 2.59GB
<none> <none> af18b070061b 29 hours ago 182MB
<none> <none> 186a8fac122e 35 hours ago 1.67GB
<none> <none> 0f90c1bb12a3 35 hours ago 182MB
<none> <none> b94009e70416 13 days ago 631MB
<none> <none> 880d8e6713cf 2 weeks ago 631MB
b/2018-external latest 128d208a6c83 2 weeks ago 207MB
2018-external latest 128d208a6c83 2 weeks ago 207MB
b/2018-web <none> 128d208a6c83 2 weeks ago 207MB
nginx latest 9e7424e5dbae 2 weeks ago 108MB
node 8-alpine 4db2697ce114 4 weeks ago 67.7MB
b_web latest d5a0ea011c0a 5 weeks ago 182MB
<none> <none> 957c22ababec 5 weeks ago 182MB
docker_web latest 70b443ed0495 5 weeks ago 182MB
docker_app latest 509d58a68224 5 weeks ago 756MB
b_app latest 509d58a68224 5 weeks ago 756MB
mysql 5.6 96dc914914f5 5 weeks ago 299MB
mysql latest 5fac85ee2c68 8 weeks ago 408MB
redis latest 1fb7b6c8c0d0 2 months ago 107MB
alpine 3.6 76da55c8019d 2 months ago 3.97MB
nginx 1.13.3-alpine ba60b24dbad5 5 months ago 15.5MB
keymetrics/pm2-docker-alpine 6 4a09bfc067d6 5 months ago 75.3MB
dockercloud/cli latest 051238cd0a37 6 months ago 64.2MB
andrewmclagan/nginx-hhvm latest ec6cc741eb0e 7 months ago 580MB
nginx 1.10 0346349a1a64 8 months ago 182MB
php 7.0.8-fpm 75b880f3a420 17 months ago 375MB
tutum/haproxy latest 33bc771bec1e 18 months ago 232MB
php 7.0.4-fpm 81d7a2fdc6dc 21 months ago 494MB
How do I know if which image are safely to remove ?
My attention is to remove all of them, but I am not sure if I should be more carful before doing that.
You can use docker image prune to remove "dangling" images; those that are not tagged (eg <none>) and are not referenced by any running container.
I find the docker image prune -a more useful. It will remove any image that is not used by a running container. In that sense prune -a is a cleanup step that you can take after your environment is running correctly.
Images that are referred to as dangling are safe to remove. Those are the images that don't have a tag. They result when a new build of an image appears and the new image takes the tag leaving the dangling images with <none>:<none>
tag.
Dangling images can be listed using docker images --filter "dangling=true" and can be removed by running docker image prune.
This command also work for me.
docker rmi $(docker images -f "dangling=true" -q)
Purging All Unused or Dangling Images, Containers, Volumes, and Networks:
Clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):
docker system prune
Remove any stopped containers and all unused images (not just dangling images):
docker system prune -a
Remove one or more specific images:docker rmi Image Image
Remove dangling images:docker images purge
Removing images according to a pattern:docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
Remove all images:docker rmi $(docker images -a -q)
Remove one or more specific containers: docker rm ID_or_Name ID_or_Name
Remove a container upon exit(run and remove): docker run --rm image_name
Remove all exited containers: docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter: docker rm $(docker ps -a -f status=exited -f status=created -q)
Stop and remove all containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Origin article: digitalocean

Docker images/containers taking up space despite clean up

I noticed that Docker appears to be using a large amount of disk space. I can see the directory /Users/me/.docker/machine/machines/default is 27.4GB
I recently cleaned up all of the images and containers I wasn't using. Now, when I run docker ps I see that no images are running.
~ $ docker ps
I can also see that I have 2 containers available.
~ $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42332s42d3 cds-abm "/bin/bash" 2 weeks ago Exited (130) 2 weeks ago evil_shockley
9ssd64ee41 tes-abm "/bin/bash" 2 weeks ago Exited (130) 2 weeks ago lonely_brattain
I can then see I have 3 images.
~ $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ghr/get latest 6708fffdd4dfa 2 weeks ago 2.428 GB
<none> <none> 96c5974ddse18 2 weeks ago 2.428 GB
fdbh/ere latest bf1da53766b1 2 weeks ago 2.225 GB
How can these be taking up nearly 30GB?
It is because you are not removing the volumes created by the containers when you stop the container. In the future, use -v when you remove the containers.
docker rm -v <container-id>
Regrading cleaning up the space, you have to ssh into docker-machine and remove all volumes created. To do so,
docker-machine ssh default
sudo -i # You may not get permission to enter in to volumes directory
cd /var/lib/docker/volumes
rm -rf *
Make sure none of your containers are currently running. Also, make sure that you don't need any of these volumes for later use (like DB containers).

Resources