Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker rmi --unused #9054

Closed
SamSaffron opened this issue Nov 9, 2014 · 28 comments
Closed

docker rmi --unused #9054

SamSaffron opened this issue Nov 9, 2014 · 28 comments
Labels
area/runtime kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny

Comments

@SamSaffron
Copy link

I raised this discussion here

https://forums.docker.com/t/command-to-remove-all-unused-images/20

To recap we often find ourselves with systems saturated with unused images, it would be super awesome if we had a simple way of cleaning up.

Eg:

docker rmi --unused
# remove all images that are not referenced by active containers

This is tricky to script as you need to walk through dependencies, the workaround is just to ignore the errors from rmi, but it feels unclean.

@SvenDowideit
Copy link
Contributor

Docker rmi with dangling=true unsafe while images are being pulled #8926

@cpuguy83
Copy link
Member

I think dangling is only untagged images as well.
You should be able to just docker rmi $(docker images -a -q). You'll get warnings on ones that are in use, but it'll keep going.

@SamSaffron
Copy link
Author

@cpuguy83 sort of, we do it but it feels risky, hoping for a warning-less solution.

@schmunk42
Copy link
Contributor

I am in a situation where I can't remove a certain layer, because docker claims it's a dependency of some other layers, but refused to remove it, because it's not there.

No running containers...

docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Remove dangling...

docker@boot2docker:~$ docker rmi `docker images -q --filter "dangling=true"`
Error response from daemon: No such id: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
Error response from daemon: Conflict, cannot delete df775d9589ad because the container 96ecbcfa8996 is using it, use -f to force
Error response from daemon: Conflict, cannot delete 805adf3b3684 because the container 78395e180f12 is using it, use -f to force
[....]
Error response from daemon: No such id: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
Error response from daemon: No such id: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
2014/12/02 20:12:45 Error: failed to remove one or more images

Check a failed ID:

docker@boot2docker:~$ docker history 3aabea57d0eb
IMAGE               CREATED             CREATED BY                                      SIZE
3aabea57d0eb        47 hours ago        /bin/sh -c #(nop) CMD [/root/run.sh]            0 B
313a977f724d        47 hours ago        /bin/sh -c #(nop) WORKDIR /app                  0 B
[...]
f6fab3b798be        3 weeks ago         /bin/sh -c #(nop) CMD [/bin/bash]               0 B
f10807909bc5        3 weeks ago         /bin/sh -c #(nop) ADD file:01b419e635eb6bec68   85.1 MB
511136ea3c5a        17 months ago                                                       0 B

Remove that one...

docker@boot2docker:~$ docker rmi 3aabea57d0eb
Error response from daemon: Conflict, cannot delete 3aabea57d0eb because the container eef630e62010 is using it, use -f to force
2014/12/02 20:13:36 Error: failed to remove one or more images

OK failed, check the dependency - it's not there?!

docker@boot2docker:~$ docker history eef630e62010
2014/12/02 20:13:44 Error response from daemon: No such id: eef630e62010

Force remove now...

docker@boot2docker:~$ docker rmi -f 3aabea57d0eb
Error response from daemon: No such id: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
2014/12/02 20:13:54 Error: failed to remove one or more images

Force remove the dependency...

docker@boot2docker:~$ docker rmi -f fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
Error response from daemon: No such image: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
2014/12/02 20:14:26 Error: failed to remove one or more images

Stuck here since the last both forced commands don't work.

@cpuguy83
Copy link
Member

cpuguy83 commented Dec 2, 2014

@schmunk42 docker ps -a

eef630e62010 is a container, and you can't run history on it, since that's for images.

@schmunk42
Copy link
Contributor

Thanks, but I always get stuck on the image mentioned above fe56ae5aadc..., also when trying to remove other images by name or ID. But the image above is not in the history of the image.

Kraftbuch:~ tobias$ docker rmi -f e922b436aef0
Error response from daemon: No such id: fe56ae5aadc7f173427241454d728bdc917b0793ef7b331b02491b22658c6bfe
2014/12/02 23:02:48 Error: failed to remove one or more images
Kraftbuch:~ tobias$ docker history e922b436aef0
IMAGE               CREATED             CREATED BY                                      SIZE
e922b436aef0        5 weeks ago         /bin/sh -c #(nop) ONBUILD RUN /usr/local/bin/   0 B
965b44585eb1        5 weeks ago         /bin/sh -c #(nop) ONBUILD RUN /app/init --env   0 B
fd3d5cbb562f        5 weeks ago         /bin/sh -c #(nop) ONBUILD ADD . /app            0 B
81953b90b861        5 weeks ago         /bin/sh -c #(nop) WORKDIR /app                  0 B
cfec040ab3f7        5 weeks ago         /bin/sh -c /usr/local/bin/composer create-pro   42.52 MB
2b318ece4d05        5 weeks ago         /bin/sh -c /usr/local/bin/composer global req   5.006 MB
a4da701e254a        5 weeks ago         /bin/sh -c #(nop) ADD dir:e27c1d6e025b66c5f7d   219 B
61686e5d7c4a        5 weeks ago         /bin/sh -c curl -sS https://getcomposer.org/i   1.033 MB
ba1ec865d3fc        5 weeks ago         /bin/sh -c apt-get update &&     apt-get inst   228.8 MB
73807de8b9f3        5 weeks ago         /bin/sh -c #(nop) ENV DEBIAN_FRONTEND=noninte   0 B
417f2d590451        5 weeks ago         /bin/sh -c #(nop) MAINTAINER Tobias Munk <tob   0 B
61f7f4f722fb        6 weeks ago         /bin/sh -c #(nop) CMD [/bin/bash]               0 B
638fd9704285        6 weeks ago         /bin/sh -c #(nop) ADD file:c53beaefa2d28eee7f   85.1 MB
511136ea3c5a        17 months ago                                                       0 B

@thaJeztah
Copy link
Member

@schmunk42 this sounds similar to #9056 (which was solve by #9204) and should be in the upcoming 1.4 release

@schmunk42
Copy link
Contributor

Yeah, looks the same.

PS: Is it possible to test master, I looked through the docs, but didn't find instructions on first sight.

@thaJeztah
Copy link
Member

Yes, you can get master builds at https://master.dockerproject.com saves having to build yourself :)

Note that the master builds are not what will be in 1.4. To get those, look here #9345

@meirwah
Copy link

meirwah commented Jan 11, 2015

Hi,
what about the next approach...
Like the approach taken in the docker “compose”(fig) build a side project(not part of docker core)
that will handle the cleaning of unused images, according to the following logic:

  1. Remove all dangling images
  2. Get all containers running/stopped (docker ps -a)
  3. Fetch all images names from result
  4. Delete all used images (result of 3)

I know it is not perfect (I’m ignoring images dependencies) , but does it have some value?

Another thing we can add, is look at the events of past X time, parse from it all image names,
So you will delete only images not used in the past X time..
WDYT?

@jessfraz jessfraz added the kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny label Feb 27, 2015
@luxas
Copy link

luxas commented Jul 30, 2015

I suppose you mean that --unused should remove all images with no container.
That sounds good.

Then a --untagged or --no-repo option is also possible for removing all images with "" values in Repository and Tag.

How does that sound?
And who should work on this?

@meirwah
Copy link

meirwah commented Aug 6, 2015

I was part of an effort to impl it as an external project in the end,
project is here (it's generic, not only for this use-case): https://github.com/CloudSlang/cloud-slang

you can see a short blog on that here : https://www.digitalocean.com/community/tutorials/how-to-clean-up-your-docker-environment-using-cloudslang-on-a-coreos-cluster-2

hope it helps...

@cpuguy83
Copy link
Member

filters could be extended to support unused

@wgerlach
Copy link

This is how I remove unused images, maybe it is of help to others:

#!/bin/bash

# this script requires bash version 4

#delete all stopped containers
docker rm $(docker ps -f status=exited -q)


# get list of (tagged) images that are used by running containers
declare -A usedimages
for i in $(docker ps | awk '{ print $2; }' | grep -v "^ID$") ; do ID=$(docker inspect  --format="{{.Id}}" $i) ; usedimages[$ID]=$i ; done

for i in "${!usedimages[@]}"
do
  echo "key  : $i"
  echo "value: ${usedimages[$i]}"
done


# delete all unused (tagged) images
for i in $(docker images --no-trunc -q | sort -u) 
  do
  if [[ ${usedimages[$i]}x != "x" ]] ; then
    echo "image is used: $i"
    echo "by ${usedimages[$i]}"
  else
    echo "image is not used: $i"
    docker rmi $i
  fi
done

@wombat
Copy link

wombat commented Nov 16, 2015

@wgerlach Thanks for the tip. Don´t you get an "value too great for base" error within the usedimages[$ID]=$i part?

@wgerlach
Copy link

@w0mbat Are you using bash v4 ?

@thaJeztah
Copy link
Member

@quyen91 please don't leave random comments on all issues that are slightly related, please read the discussion, and you'll see that alternatives are already mentioned. Also, this is not related to docker-machine

@quyen91
Copy link

quyen91 commented Dec 31, 2015

@thaJeztah i tried so many solution but not woking, this works for me so i only want share it.

@ake-persson
Copy link

I do it as follows based on the prev. rec. in the thread:

#!/bin/bash

log() {
    logger -s -t $(basename $0) $1
}

log "Started"

containers=$(docker ps -a -q -f "status=exited")
if [ -n "${containers}" ]; then
    log "Cleanup stopped container id(s): ${containers}"
    docker rm $containers
fi

images=$(docker images -q --filter "dangling=true")
if [ -n "${images}" ]; then
    log "Cleanup dangling image id(s): ${images}"
    docker rmi $images
fi

log "Done"

@sammcj
Copy link

sammcj commented Feb 5, 2016

This would be very useful, after several years of running thousands of docker images since 2013 - cleaning up is still a massive pain in the ass, usually we find formatting the whole filesystem and pulling everything again is by far the easiest way but absolutely not ideal.

@schmunk42
Copy link
Contributor

@sammcj Agreed. Especially for swarm, where nodes often pull images they don't use at all.

My two most used docker-docker-cleanup tools:

@jimmycuadra
Copy link
Contributor

Possible inspiration: rkt has a "garbage collection" system that can be called from a cron job and clean stuff that hasn't been used for a while (with a configurable grace period): https://coreos.com/rkt/docs/latest/devel/pod-lifecycle.html

@csainty
Copy link

csainty commented Feb 15, 2016

👍 for better tooling support here.

I use this command currently (in case anyone is interested)
docker rmi $(grep -xvf <(docker ps -a --format '{{.Image}}') <(docker images | tail -n +2 | grep -v '<none>' | awk '{ print $1":"$2 }'))

@sungardas-cloud-devops
Copy link

sungardas-cloud-devops commented Jun 23, 2016

I've gone a little deeper... Here is my 1-liner for removing containers which have not actively been used in more than 13 days:

docker images | grep -v REPOSITORY | awk \'{print $3}\' | xargs -n 1 docker history | grep -v missing | grep -v "^IMAGE" | egrep -v "day|hour|minute|second" | awk \'{print $1}\' | sort | uniq | xargs -n 1 docker inspect | jq -r .[0].RepoTags[] | xargs docker rmi

It DOES have a dependencies on the xargs and jq commands... My ideal solution would be a command something like:

docker rmi --last-used=-14

Meaning delete all images that have not actively been run in more than 14 days. Or perhaps:

docker rmi --last-used=<unix timestamp>

Which would delete all containers which haven't been used since the specified timestamp.

@thaJeztah
Copy link
Member

Relates to #22871

@thaJeztah
Copy link
Member

Data management features are merged in #26108. Note that it's the first incarnation; additional enhancements can be added in future.

@OlivierDupre
Copy link

@Mickep76 I disagree with your solution. The first part of your script will remove data containers, which are basically in an "Exited" status but are needed.
Cf: https://docs.docker.com/engine/tutorials/dockervolumes/

@ake-persson
Copy link

You're right considering if you use data containers in a state that is not running. I believe the correct approach is what is proposed by @sungardas-cloud-devops --last-used=-14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/runtime kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny
Projects
None yet
Development

No branches or pull requests