How to remove old and unused Docker images - docker

When running Docker for a long time, there are a lot of images in system. How can I remove all unused Docker images at once safety to free up the storage?
In addition, I also want to remove images pulled months ago, which have the correct TAG.
So, I'm not asking for removing untagged images only. I'm searching for a way to remove general unused images, which includes both untagged and other images such as pulled months ago with correct TAG.

(original answer see below)
Update Sept. 2016: Docker 1.13: PR 26108 and commit 86de7c0 introduce a few new commands to help facilitate visualizing how much space the docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune will delete ALL dangling data (i.e. In order: containers stopped, volumes without containers and images with no containers). Even unused data, with -a option.
You also have:
docker container prune
docker image prune
docker network prune
docker volume prune
For unused images, use docker image prune -a (for removing dangling and ununsed images).
Warning: 'unused' means "images not referenced by any container": be careful before using -a.
As illustrated in A L's answer, docker system prune --all will remove all unused images not just dangling ones... which can be a bit too much.
Combining docker xxx prune with the --filter option can be a great way to limit the pruning (docker SDK API 1.28 minimum, so docker 17.04+)
The currently supported filters are:
until (<timestamp>) - only remove containers, images, and networks created before given timestamp
label (label=<key>, label=<key>=<value>, label!=<key>, or label!=<key>=<value>) - only remove containers, images, networks, and volumes with (or without, in case label!=... is used) the specified labels.
See "Prune images" for an example.
Warning: there is no "preview" or "--dry-run" option for those docker xxx prune commands.
This is requested with moby/moby issue 30623 since 2017, but seems tricky to be implemented (Aug. 2022)
Having a more representative overview of what will be pruned will be quite complicated, for various reasons;
race conditions (can be resolved by documenting the limitations);
A container/image/volume/network may not be in use at the time that "dry run" is used, but may be in use the moment the actual prune is executed (or vice-versa), so dry run will always be an "approximation" of what will be pruned.
the more difficult part is due to how objects (containers, images, networks etc.) depend on each other.
For example, an image can be deleted if it no longer has references to it (no more tags, no more containers using it); this is the reason that docker system prune deletes objects in a specific order (first remove all unused containers, then remove unused images).
In order to replicate the same flow for "dry-run", it will be needed to temporarily construct representation of all objects and where they're referenced based on that (basically; duplicate all reference-counters, and then remove references from that "shadow" representation).
Finally; with the work being done on integrating the containerd snapshotter (image and layer store), things may change more;
For example, images can now be multi-arch, and (to be discussed), "pruning" could remove unused variants (architectures) from an image to clean up space, which brings another dimension to calculating "what can be removed".
Original answer (Sep. 2016)
I usually do:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
I have an [alias for removing those dangling images: drmi]13
The dangling=true filter finds unused images
That way, any intermediate image no longer referenced by a labelled image is removed.
I do the same first for exited processes (containers)
alias drmae='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'
As haridsv points out in the comments:
Technically, you should first clean up containers before cleaning up images, as this will catch more dangling images and less errors.
Jess Frazelle (jfrazelle) has the bashrc function:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
To remove old images, and not just "unreferenced-dangling" images, you can consider docker-gc:
A simple Docker container and image garbage collection script.
Containers that exited more than an hour ago are removed.
Images that don't belong to any remaining container after that are removed.

Update the second (2017-07-08)
Refer (again) to VonC, using the even more recent system prune. The impatient can skip the prompt with the -f, --force option:
docker system prune -f
The impatient and reckless can additionally remove "unused images not just the dangling ones" with the -a, --all option:
docker system prune -af
https://docs.docker.com/engine/reference/commandline/system_prune/
Update
Refer to VonC's answer which uses the recently added prune commands. Here is the corresponding shell alias convenience:
alias docker-clean=' \
docker container prune -f ; \
docker image prune -f ; \
docker network prune -f ; \
docker volume prune -f '
Old answer
Delete stopped (exited) containers:
$ docker ps --no-trunc -aqf "status=exited" | xargs docker rm
Delete unused (dangling) images:
$ docker images --no-trunc -aqf "dangling=true" | xargs docker rmi
If you have exercised extreme caution with regard to irrevocable data loss, then you can delete unused (dangling) volumes (v1.9 and up):
$ docker volume ls -qf "dangling=true" | xargs docker volume rm
Here they are in a convenient shell alias:
alias docker-clean=' \
docker ps --no-trunc -aqf "status=exited" | xargs docker rm ; \
docker images --no-trunc -aqf "dangling=true" | xargs docker rmi ; \
docker volume ls -qf "dangling=true" | xargs docker volume rm'
References
docker ps -f
docker rm
docker images -f
docker rmi
Docker v1.9.0 release notes
docker volume ls
docker volume rm

The other answers are great, specifically:
docker system prune # doesn't clean out old images
docker system prune --all # cleans out too much
But I needed something in the middle of the two commands so the filter option was what I needed:
docker image prune --all --filter "until=4320h" # delete images older than 6 months ago; 4320h = 24 hour/day * 30 days/month * 6 months
For reference: https://docs.docker.com/config/pruning/#prune-images

To remove old tagged images that are more than a month old:
$ docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' \
| grep ' months' | awk '{ print $1 }' \
| xargs --no-run-if-empty docker rmi
Note that it'll fail to remove images that are used by a container, referenced in a repository, has dependent child images... which is probably what you want. Else just add -f flag.
Example of /etc/cron.daily/docker-gc script:
#!/bin/sh -e
# Delete all stopped containers (including data-only containers).
docker ps -a -q --no-trunc --filter "status=exited" | xargs --no-run-if-empty docker rm -v
# Delete all tagged images more than a month old
# (will fail to remove images still used).
docker images --no-trunc --format '{{.ID}} {{.CreatedSince}}' | grep ' months' | awk '{ print $1 }' | xargs --no-run-if-empty docker rmi || true
# Delete all 'untagged/dangling' (<none>) images
# Those are used for Docker caching mechanism.
docker images -q --no-trunc --filter dangling=true | xargs --no-run-if-empty docker rmi
# Delete all dangling volumes.
docker volume ls -qf dangling=true | xargs --no-run-if-empty docker volume rm

According to the doc, the following command will delete images older than 48 hours.
$ docker image prune --all --filter until=48h

Assuming you have Docker 1.13 or higher you can just use the prune commands. For your question specifically for removing old images, you want the first one.
# Remove unused images
docker image prune
# Remove stopped containers.
docker container prune
# Remove unused volumes
docker volume prune
# Remove unused networks
docker network prune
# Command to run all prunes:
docker system prune
I would recommend not getting used to using the docker system prune command. I reckon users will accidentally remove things they don't mean to. Personally, I'm going to mainly be using the docker image prune and docker container prune commands.

Until now (Docker version 1.12) we are using the following command to delete all the running containers. Also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command.
Delete all Exited Containers
docker rm $(docker ps -q -f status=exited)
Delete all Stopped Containers
docker rm $(docker ps -a -q)
Delete All Running and Stopped Containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove all containers, without any criteria
docker container rm $(docker container ps -aq)
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command:
docker system prune
All unused containers, images, networks and volumes will get deleted. We can also do this using the following commands that clean up the individual components:
docker container prune
docker image prune
docker network prune
docker volume prune

This worked for me:
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

I recently wrote a script to solve this on one of my servers:
#!/bin/bash
# Remove all the dangling images
DANGLING_IMAGES=$(docker images -qf "dangling=true")
if [[ -n $DANGLING_IMAGES ]]; then
docker rmi "$DANGLING_IMAGES"
fi
# Get all the images currently in use
USED_IMAGES=($( \
docker ps -a --format '{{.Image}}' | \
sort -u | \
uniq | \
awk -F ':' '$2{print $1":"$2}!$2{print $1":latest"}' \
))
# Get all the images currently available
ALL_IMAGES=($( \
docker images --format '{{.Repository}}:{{.Tag}}' | \
sort -u \
))
# Remove the unused images
for i in "${ALL_IMAGES[#]}"; do
UNUSED=true
for j in "${USED_IMAGES[#]}"; do
if [[ "$i" == "$j" ]]; then
UNUSED=false
fi
done
if [[ "$UNUSED" == true ]]; then
docker rmi "$i"
fi
done

Here is a script to clean up Docker images and reclaim the space.
#!/bin/bash -x
## Removing stopped container
docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm
## If you do not want to remove all container you can have filter for days and weeks old like below
#docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm
#docker ps -a | grep Exited | grep "weeks ago" | awk '{print $1}' | xargs docker rm
## Removing Dangling images
## There are the layers images which are being created during building a Docker image. This is a great way to recover the spaces used by old and unused layers.
docker rmi $(docker images -f "dangling=true" -q)
## Removing images of perticular pattern For example
## Here I am removing images which has a SNAPSHOT with it.
docker rmi $(docker images | grep SNAPSHOT | awk '{print $3}')
## Removing weeks old images
docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi
## Similarly you can remove days, months old images too.
Original script
https://github.com/vishalvsh1/docker-image-cleanup
Usually Docker keeps all temporary files related to image building and layers at
/var/lib/docker
This path is local to the system, usually at THE root partition, "/".
You can mount a bigger disk space and move the content of /var/lib/docker to the new mount location and make a symbolic link.
This way, even if Docker images occupy space, it will not affect your system as it will be using some other mount location.
Original post: Manage Docker images on local disk

I'm using this command:
export BEFORE_DATETIME=$(date --date='10 weeks ago' +"%Y-%m-%dT%H:%M:%S.%NZ")
docker images -q | while read IMAGE_ID; do
export IMAGE_CTIME=$(docker inspect --format='{{.Created}}' --type=image ${IMAGE_ID})
if [[ "${BEFORE_DATETIME}" > "${IMAGE_CTIME}" ]]; then
echo "Removing ${IMAGE_ID}, ${BEFORE_DATETIME} is earlier then ${IMAGE_CTIME}"
docker rmi -f ${IMAGE_ID};
fi;
done
This will remove all images whose creation time is greater than 10 weeks ago.

If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago:
three_months_old_images=`docker images | grep -vi "<none>" | tr -s ' ' | cut -d" " -f3,4,5,6 | grep "3 months ago" | cut -d" " -f1`
docker rmi $three_months_old_images

To prune all images and volumes as well
docker system prune -af --volumes

docker system prune -a
(You'll be asked to confirm the command. Use -f to force run, if you know what you're doing.)

#VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some:
#!/bin/bash
imgs=$(docker images | awk '/<none>/ { print $3 }')
if [ "${imgs}" != "" ]; then
echo docker rmi ${imgs}
docker rmi ${imgs}
else
echo "No images to remove"
fi
procs=$(docker ps -a -q --no-trunc)
if [ "${procs}" != "" ]; then
echo docker rm ${procs}
docker rm ${procs}
else
echo "No processes to purge"
fi

To remove tagged images which have not container running, you will have to use a little script:
#!/bin/bash
# remove not running containers
docker rm $(docker ps -f "status=exited" -q)
declare -A used_images
# collect images which has running container
for image in $(docker ps | awk 'NR>1 {print $2;}'); do
id=$(docker inspect --format="{{.Id}}" $image);
used_images[$id]=$image;
done
# loop over images, delete those without a container
for id in $(docker images --no-trunc -q); do
if [ -z ${used_images[$id]} ]; then
echo "images is NOT in use: $id"
docker rmi $id
else
echo "images is in use: ${used_images[$id]}"
fi
done

Remove old containers weeks ago.
docker rm $(docker ps -a | grep "weeks" | awk '{ print $1; }')
Remove old images weeks ago. Be careful. This will remove base images which was created weeks ago but which your new images might be using.
docker rmi $(docker images | grep 'weeks' | awk '{ print $3; }')

How to remove a tagged image
docker rmi the tag first
docker rmi the image.
# that can be done in one docker rmi call e.g.: #
docker rmi <repo:tag> <imageid>
(this works Nov 2016, Docker version 1.12.2)
e.g.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
usrxx/the-application 16112805 011fd5bf45a2 12 hours ago 5.753 GB
usryy/the-application vx.xx.xx 5af809583b9c 3 days ago 5.743 GB
usrzz/the-application vx.xx.xx eef00ce9b81f 10 days ago 5.747 GB
usrAA/the-application vx.xx.xx 422ba91c71bb 3 weeks ago 5.722 GB
usrBB/the-application v1.00.18 a877aec95006 3 months ago 5.589 GB
$ docker rmi usrxx/the-application:16112805 && docker rmi 011fd5bf45a2
$ docker rmi usryy/the-application:vx.xx.xx && docker rmi 5af809583b9c
$ docker rmi usrzz/the-application:vx.xx.xx eef00ce9b81f
$ docker rmi usrAA/the-application:vx.xx.xx 422ba91c71bb
$ docker rmi usrBB/the-application:v1.00.18 a877aec95006
e.g. Scripted remove anything older than 2 weeks.
IMAGESINFO=$(docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}} {{.CreatedSince}}' |grep -E " (weeks|months|years)")
TAGS=$(echo "$IMAGESINFO" | awk '{ print $2 ":" $3 }' )
IDS=$(echo "$IMAGESINFO" | awk '{ print $1 }' )
echo remove old images TAGS=$TAGS IDS=$IDS
for t in $TAGS; do docker rmi $t; done
for i in $IDS; do docker rmi $i; done

docker rm $(docker ps -faq)
docker rmi $(docker ps -faq)
-f force
-a all
-q in the mode

First, run docker images to see list of images and copy IMAGE HASH ID into clipboard.
Run docker rmi -f <Image>
Remember option -f is force deleting.

Occasionally I have run into issues where Docker will allocate and continue to use disk space, even when the space is not allocated to any particular image or existing container. The latest way I generated this issue accidentally was using "docker-engine" centos build instead of "docker" in RHEL 7.1. What seems to happen is sometimes the container clean-ups are not completed successfully and then the space is never reused. When the 80GB drive I allocated as / was filled with /var/lib/docker files I had to come up with a creative way to resolve the issue.
Here is what I came up with. First to resolve the disk full error:
Stop docker: systemctl stop docker
Allocated a new drive mounted as say /mnt/docker .
Move all the files in /var/lib/docker to /mnt/docker . I used the command:
rsync -aPHSx --remove-source-files /var/lib/docker/ /mnt/docker/
Mount the new drive to /var/lib/docker.
At this point I no longer had a disk full error, but I was still wasting a huge amount of space. The next steps are to take care of that.
Start Docker: systemctl start docker
Save the all the images:
docker save $(docker images |sed -e '/^<none>/d' -e '/^REPOSITORY/d' -e 's,[ ][ ]*,:,' -e 's,[ ].*,,') > /root/docker.img
Uninstall docker.
Erase everything in /var/lib/docker:
rm -rf /var/lib/docker/[cdintv]*
Reinstall docker
Enable docker: systemctl enable docker
Start docker: systemctl start docker
Restore images:
docker load < /root/docker.img
Start any persistent containers you need running.
This dropped my disk usage from 67 GB for docker to 6 GB for docker.
I do not recommend this for everyday use. But it is useful to run when it looks like docker has lost track of used disk space do to software errors, or unexpected reboots.

docker rm `docker ps -aq`
or
docker rm $(docker ps -q -f status=exited)

If you wish to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.
Just run:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It runs every 30 minutes by default. You can however set the delay time by using this flag in seconds (DELAY_TIME=1800 option).
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md

If you build these pruned images yourself (from some other, older base images) please be careful with the accepted solutions above based on docker image prune, as the command is blunt and will try to remove also all dependencies required by your latest images (the command should be probably renamed to docker image*s* prune).
The solution I came up for my docker image build pipelines (where there are daily builds and tags=dates are in the YYYYMMDD format) is this:
# carefully narrow down the image to be deleted (to avoid removing useful static stuff like base images)
my_deleted_image=mirekphd/ml-cpu-py37-vsc-cust
# define the monitored image (tested for obsolescence), which will be usually the same as deleted one, unless deleting some very infrequently built image which requires a separate "clock"
monitored_image=mirekphd/ml-cache
# calculate the oldest acceptable tag (date)
date_week_ago=$(date -d "last week" '+%Y%m%d')
# get the IDs of obsolete tags of our deleted image
# note we use monitored_image to test for obsolescence
my_deleted_image_obsolete_tag_ids=$(docker images --filter="before=$monitored_image:$date_week_ago" | grep $my_deleted_image | awk '{print $3}')
# remove the obsolete tags of the deleted image
# (note it typically has to be forced using -f switch)
docker rmi -f $my_deleted_image_obsolete_tag_ids

See the official reference for docker system prune
docker system prune will remove:
all stopped containers
all networks not used by at least one container
all dangling images
all build cache
docker system prune -a will do the same, but in additional to removing all dangling images, it will more broadly remove:
all images without at least one container associated to them
What are dangling images?
Docker images consist of multiple layers that get wrapped inside a parent 'container layer' when the overall container image is generated from a Dockerfile. Dangling images are layers that have no relationship to any other tagged images, and will therefore never have any use within any new containers that are built. They no longer serve a purpose and consume disk space.
For example a dangling image can be created by the following process:
Build a named image my-image from Dockerfile, without specifying any tag:
FROM ubuntu:latest
CMD ["echo", "Hello World"]
docker build -t my-image
docker images
REPOSITORY TAG IMAGE ID
my-image latest 7ed6e7202eca <--- created, not dangling
ubuntu latest 825d55fb6340
Update the Dockerfile:
FROM ubuntu:latest
CMD ["echo", "Hello, World!"]
Rebuild image re-using the previous name, without specifying any tag:
docker build -t my-image
docker images
REPOSITORY TAG IMAGE ID
my-image latest da6e74196f66 <--- replacement layer
<none> <none> 7ed6e7202eca <--- previous layer, now dangling
ubuntu latest 825d55fb6340
The build created a new my-image layer. As we can see, the layer that was originally created is still there, but its name and tag are set to <none>:<none>. It will never be possible for this layer to be associated with any docker container layer, which means it's 'dangling'
What are images without at least one container associated to them?
An unused image means that it has not been assigned or used in a container. For example, docker ps -a will list all of your running and stopped containers. Any image being used by any of these containers is a "used image".
When running docker system prune -a, it will remove both unused and dangling images. Any image with at least one container associated to it will not be affected.

There is sparrow plugin docker-remove-dangling-images you can use to clean up stopped containers and unused (dangling) images:
$ sparrow plg run docker-remove-dangling-images
It works both for Linux and Windows OS.

If you have a lot of them, it can be really tedious to remove them, but lucky for us Docker has a few commands to help us eliminate dangling images. In older versions of Docker (and this still works today), you can delete dangling images on their own by running docker rmi -f $(docker images -f "dangling=true" -q) .

I usually do docker rm -f $(docker ps -a -q) and docker system prune to purge all dangling containers.

The below command will help to remove all unused and old images from local repository
==> docker system prune --all

Related

how do I clean up /dev/mapper/docker and release space?

If I do df within my docker container I find a drive mounted on / is 95% full.
/dev/mapper/docker-202:80-131076-9c4e30b5819b23ba61e87d44b3824b780a9f5b8
What is this drive and how do I clean it up?
With recent versions of Docker you can see the space used with:
docker system df
and prune it with:
docker system prune
The above command combines the prune command that exists for volumes, containers, images and networks:
docker volume prune
docker container prune
docker image prune
docker network prune
Each command has a --help option documenting a -f (--force) option to avoid asking you questions. It must be used with care.
-o-
On older versions of Docker I ran the script:
#!/bin/bash
# Remove dead containers (and their volumes)
docker ps -f status=dead --format '{{ .ID }}' | xargs -r docker rm -v
# Remove dangling volumes
docker volume ls -qf dangling=true | xargs -r docker volume rm
# Remove untagged ("<none>") images
docker images --digests --format '{{.Repository}}:{{.Tag}}#{{.Digest}}' | sed -rne 's/([^>]):<none>#/\1#/p' | xargs -r docker rmi
# Remove dangling images
docker images -qf dangling=true | xargs -r docker rmi
# Remove temporary files
rm -f /var/lib/docker/tmp/*
OK that is the root of the disk space inside the container. This has a default size of only 10gb
To increase the space there are other answers already posted.

How to Force Docker to release storage space after manual delete of file in volumes and containers?

I have few issues with storage spaces. I deleted few big files such as log files (after find unix of big files).
The problem is that delete manually some file of Docker (in /var/lib/docker/...). After deletion of Docker files, I can see that the space left does not change. Docker does not release space.
I restart the service Docker and I the problem persit.
How can I force Docker to release space from (devicemapper, volume, images, ...) ?
With recent versions of Docker you can see the space used with:
docker system df
and prune it with:
docker system prune
The above command combines the prune command that exists for volumes, containers, networks and images:
docker volume prune
docker container prune
docker image prune
docker network prune
All of these have a --help option.
-o-
On older versions of Docker I ran the script:
#!/bin/bash
# Remove dead containers (and their volumes)
docker ps -f status=dead --format '{{ .ID }}' | xargs -r docker rm -v
# Remove dangling volumes
docker volume ls -qf dangling=true | xargs -r docker volume rm
# Remove untagged ("<none>") images
docker images --digests --format '{{.Repository}}:{{.Tag}}#{{.Digest}}' | sed -rne 's/([^>]):<none>#/\1#/p' | xargs -r docker rmi
# Remove dangling images
docker images -qf dangling=true | xargs -r docker rmi
# Remove temporary files
rm -f /var/lib/docker/tmp/*
This depends on what version of docker you are using, If you are using >1.13 then you can use:
docker system df
and
docker system df -v
^^These will show where disk space is being utilized.
You can cleanup using prune commands:
docker system prune -af
^^ This prunes everything & is the most destructive.
Or you can use docker image prune or docker volume prune etc.
Docker cleanup job is rather non-existing and you are basically in charge of doing it yourself. There are ways of doing that as pointed out in this blog-post, yet I rather use third-party scripts, e.g.: docker-clean to clean up some of the mess docker leaves behind.

How to free wasted space used by docker?

I set up docker on my server. I use Jenkins for CI. Those tools are really cool. But, unfortunately, I still can't figure out how to get the wasted space back.
I tried different things proposed on the web and by using them I can only free a part of the wasted space. I need no any history and etc.
I tried different things, like rude docker rm $(docker ps -aq) and docker rmi $(docker images -q) while some of containers are running that make them prevented from deletion.
I also tried this script and similar.
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr
I would like to expect that after first build of all images I have fixed size of the stuff by applying the clean up commands above. In other words I want the subsequent builds with clean up applied doesn't make the wasted space grow.
But anyway every execution of docker build occupies new space that I can't free up.
The directory /var/lib/docker/overlay grows up and doesn't consider my clean ups eventually.
Where am I wrong?
In the latest versions of Docker, you can now use docker system prune command (with --all option for complete cleanup).
https://docs.docker.com/engine/reference/commandline/system_prune/
In order to remove volumes use:
docker volume prune
In order to remove networks use:
docker network prune
In order to remove all stopped containers use:
docker container prune
In order to remove unused images use:
docker image prune

how do I clean up my docker host machine

As I create/debug a docker image/container docker seems to be leaving all sorts of artifacts on my system. (at one point there was a 48 image limit) But the last time I looked there were 20-25 images; docker images.
So the overarching questions are:
how does one properly cleanup?
as I was manually deleting images more started to arrive. huh?
how much disk space should I really allocate to the host?
will running daemons really restart after the next reboot?
and the meta question... what questions have I not asked that need to be?
Here's how I periodically purge my docker host:
Kill running containers:
docker kill $(docker ps -qa)
Delete all containers (and their associated volumes):
docker rm -v $(docker ps -qa)
Remove all images:
docker rmi $(docker images -q)
Update
Delete only the containers that are not running. Parse the "ps" output for the "Exited" string:
docker ps -a | awk '/Exited/ {print $1}' | xargs docker rm -v
Not perfect... Don't give your container the name "Exited" :-)
Update
Docker 1.9 has a new volume command that can be used to purge old volumes
docker volume rm $(docker volume ls -qf dangling=true)
Update
Latest community edition of docker has a new "system prune" command
docker system prune --volumes
This purged unused networks, kill stopped containers, dangling images and any unused volumes.
It can also be helpful to remove "dangling" images
docker rmi $(docker images -f "dangling=true" -q)
I would also like to contribute to this with some commands that were added to version 1.13.0:
$ docker system prune
$ docker container prune
$ docker image prune
$ docker volume prune
$ docker network prune
see changelog: 1.13.0 (2017-01-18)
Add new docker system command with df and prune subcommands for system resource management, as well as docker {container,image,volume,network} prune subcommands #26108 #27525 / #27525
I'm using docker-machine with VirtualBox and after deleting all containers and all images, the docker VirtualBox image is still consuming many gigabytes of disk space.
To also clean up the disk space, it helps to delete and re-create the docker machine. E.g.:
docker-machine rm default
docker-machine create --driver virtualbox default
Update:
There are built-in filters in the docker cli that let one properly display containers that meet certain criteria. These bash functions are taken from one of the core maintainers' dotfiles:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
del_stopped(){
local name=$1
local state=$(docker inspect --format "{{.State.Running}}" $name 2>/dev/null)
if [[ "$state" == "false" ]]; then
docker rm $name
fi
}
Original Answer
A helper script I've created for my own use:
#!/bin/bash
# options:
# remove stopped containers and untagged images
# $ dkcleanup
# remove all stopped|running containers and untagged images
# $ dkcleanup --reset
# remove containers|images|tags matching {repository|image|repository\image|tag|image:tag}
# pattern and untagged images
# $ dkcleanup --purge {image}
# everything
# $ dkcleanup --nuclear
if [ "$1" == "--reset" ]; then
# Remove all containers regardless of state
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
elif [ "$1" == "--purge" ]; then
# Attempt to remove running containers that are using the images we're trying to purge first.
(docker rm -f $(docker ps -a | grep "$2/\|/$2 \| $2 \|:$2\|$2-\|$2:\|$2_" | awk '{print $1}') 2>/dev/null || echo "No containers using the \"$2\" image, continuing purge.") &&\
# Remove all images matching arg given after "--purge"
docker rmi $(docker images | grep "$2/\|/$2 \| $2 \|$2 \|$2-\|$2_" | awk '{print $3}') 2>/dev/null || echo "No images matching \"$2\" to purge."
else
# This alternate only removes "stopped" containers
docker rm -f $(docker ps -a | grep "Exited" | awk '{print $1}') 2>/dev/null || echo "No stopped containers to remove."
fi
if [ "$1" == "--nuclear" ]; then
docker rm -f $(docker ps -a -q) 2>/dev/null || echo "No more containers to remove."
docker rmi $(docker images -q) 2>/dev/null || echo "No more images to remove."
else
# Always remove untagged images
docker rmi $(docker images | grep "<none>" | awk '{print $3}') 2>/dev/null || echo "No untagged images to delete."
fi
exit 0
source
To your questions:
how does one properly cleanup?
no official way yet, just helper scripts and functions like the above.
as I was manually deleting images more started to arrive. huh?
you might have been deleting images that were built on top of others that became "untagged" when you tried to delete them.
how much disk space should I really allocate to the host?
depends on the types of images you plan to use. Know that running a 500 mb image multiple times doesn't use (500mb X number of containers) space. The containers reuse the same image and just add whatever they change when running on top. So think from an image storing perspective, not a container runtime one regarding storage.
will running daemons really restart after the next reboot?
By default, they are stopped when the host reboots. You need to run with docker run --restart=True to automatically start up again when the host reboots.
Sometimes you wont have Status, it'll just be blank.
here is my version:
docker rm -f $(docker ps -a | env -i grep -v Up | tail -n+2 | cut -d ' ' -f 1)

How does one remove a Docker image?

I'm running Docker under Vagrant under OS X 10.8.4 (Mountain Lion), and whenever I try to delete a saved image, I get an error:
$ docker rmi some-image-id
2013/07/15 hh:mm:ss unexpected JSON input
According to the rmi help, the proper syntax is docker rmi IMAGE [IMAGE...], and I'm not sure what to make of that.
How can I delete an image?
$ docker version
Client version: 0.4.8
Server version: 0.4.8
Go version: go1.1
 
$docker info
Containers: 1
Images: 3
Interestingly, when I run docker ps, no containers show up at all. Running docker images shows four (4) base images and one (1) node image.
Try docker rmi node. That should work.
Seeing all created containers is as simple as docker ps -a.
To remove all existing containers (not images!) run docker rm $(docker ps -aq)
The following are some of the ways to remove docker images/containers:
Remove single image
docker rmi image_name:version/image-id
Remove all images
docker rmi $(docker images -qf "dangling=true")
Kill containers and remove them:
docker rm $(docker kill $(docker ps -aq))
Note: Replace kill with stop for graceful shutdown
Remove all images except "my-image"
Use grep to remove all except my-image and ubuntu
docker rmi $(docker images | grep -v 'ubuntu\|my-image' | awk {'print $3'})
Or (without awk)
docker rmi $(docker images --quiet | grep -v $(docker images --quiet ubuntu:my-image))
Delete all docker containers
docker rm $(docker ps -a -q)
Delete all docker images
docker rmi $(docker images -q)
To remove an image from Docker using the image ID:
Get the list of all Images
docker images
Identify the image ID of the image you want to delete, for example:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
kweku360/java latest 08d3a9b8e166 2 weeks ago 5.733 GB`
Finally remove the image using the image ID (only the first three digits are required)
docker rmi 08d
Image:
List images
docker images
Remove one image
docker rmi image_name
Force remove one image
docker rmi -f image_name
Container:
List all containers
docker ps -a
Remove one container
docker rm container_id
Force remove one container
docker rm -f container_id
Update, as commented by VonC in How to remove old Docker containers.
With Docker 1.13 (Q4 2016), you now have:
docker system prune will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).
See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Removing Containers
To remove a specific container
docker rm CONTAINER_ID CONTAINER_ID
For single image
docker rm 70c0e19168cf
For multiple images
docker rm 70c0e19168cf c2ce80b62174
Remove exited containers
docker ps -a -f status=exited
Remove all the containers
docker ps -q -a | xargs docker rm
Removing Images
docker rmi IMAGE_ID
Remove specific images
for single image
docker rmi ubuntu
for multiple images
docker rmi ubuntu alpine
Remove dangling images
Dangling images are layers that have no relationship to any tagged images as the Docker images are constituted of multiple images.
docker rmi -f $(docker images -f dangling=true -q)
Remove all Docker images
docker rmi -f $(docker images -a -q)
Removing Volumes
To list volumes, run docker volume ls
Remove a specific volume
docker volume rm VOLUME_NAME
Remove dangling volumes
docker volume rm $(docker volume ls -f dangling=true -q)
Remove a container and its volumes
docker rm -v CONTAINER_NAME
docker rm container_name
docker rmi image_name
docker help
rm Remove one or more containers
rmi Remove one or more images
docker rmi 91c95931e552
Error response from daemon: Conflict, cannot delete 91c95931e552 because the container 76068d66b290 is using it, use -f to force
FATA[0000] Error: failed to remove one or more images
Find container ID,
# docker ps -a
# docker rm daf644660736
First of all, we have to stop and remove the Docker containers which are attached with the Docker image that we are going to remove.
So for that, first
docker stop container-id - To stop the running container
docker rm container-id - To delete/remove the container
then,
docker rmi image-id - To delete/remove the image
For versions 1.13 and higher:
docker image rm [OPTIONS] IMAGE [IMAGE...]
Comparing:
the documention of docker image rm and
the documentation of docker rmi,
the [OPTIONS] seem to have no difference.
--force , -f Force removal of the image
--no-prune Do not delete untagged parents
From: Introducing Docker 1.13
CLI restructured
In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with. For example list and start of containers are now subcommands of docker container and history is a subcommand of docker image.
These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.
Docker provides some command to remove images.
Show/Remove Images:
docker images
docker images -a # All images
docker images --no-trunc # List the full length image IDs
docker images --filter "dangling=true" // Show unstage images
docker rmi $(docker images -f "dangling=true" -q) # Remove on unstages images
docker rmi <REPOSITORY> or <Image ID> # Remove a single image
docker image prune # Interactively remove dangling images
docker image prune -a # Remove all images
or
docker rmi -f $(sudo docker images -a -q)
Also, you can also use filter parameters to remove set of images at
once:
Example:
$docker images --filter "before=<hello-world>" // It will all images before hello-world
$docker images --filter "since=<hello-world>" // It will all images since hello-world
So you can delete that filter images like this:
docker rmi $(docker images --filter "since=<hello-world>")
docker rmi $(docker images --filter "before=<hello-world>")
Delete all of them using
Step 1: Kill all containers
for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker kill $i ; done
Step 2: RM them first
for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker rm $i ; done
Step 3: Delete the images using force
for i in `sudo docker images | awk '{ print $3}'`; do sudo docker rmi --force $i ; done
Use the step 1 in case you are getting error saying it cant be deleted owing to child dependencies
If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.
I use this on production since we deploy several times a day on multiple servers, and I don't want to go to every server to clean up (that would be a pain).
Just run:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md
Here's a shell script to remove a tagged (named) image and it's containers.
Save as docker-rmi and run using 'docker-rmi my-image-name'
#!/bin/bash
IMAGE=$1
if [ "$IMAGE" == "" ] ; then
echo "Missing image argument"
exit 2
fi
docker ps -qa -f "ancestor=$IMAGE" | xargs docker rm
docker rmi $IMAGE
In my case the probleme is that I have tow images withe same name
the solution is to add the tag after the name or the id
sudo docker rmi <NAME>:<TAG>
ex:
sudo docker rmi php:7.0.4-fpm
Why nobody mentioned docker-compose! I 've just been using it for one week, and I cannot survive without it. All you need is writing a yml which takes only several minutes of studying, and then you are ready to go. It can boot images, containers (which are needed in so-called services) and let you review logs just like you use with docker native commands. Git it a try:
docker-compose up -d
docker-compose down --rmi 'local'
Before I used docker-compose, I wrote my own shell script, then I had to customize the script whenever needed especially when application architecture changed. Now I don't have to do this anymore, thanks to docker-compose.
For me the following worked fine:
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian jessie 86baf4e8cde9 3 weeks ago 123MB
ubuntu yakkety 7d3f705d307c 3 weeks ago 107MB
alpine 3.5 074d602a59d7 7 weeks ago 3.99MB
Then go ahead and remove an image by running some like these:
> docker rmi debian:jessie
> docker rmi ubuntu:yakkety
> docker rmi alipine:3.5
List images:
ahanjura#ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
88282f8eda00 19 seconds ago 308.5 MB
13e5d3d682f4 19 hours ago 663 MB
busybox2 latest 05fe66bb1144 20 hours ago 1.129 MB
ubuntu 16.04 00fd29ccc6f1 5 days ago 110.5 MB
ubuntu 14.04 67759a80360c 5 days ago 221.4 MB
python 2.7 9e92c8430ba0 7 days ago 680.7 MB
busybox latest 6ad733544a63 6 weeks ago 1.129 MB
ubuntu 16.10 7d3f705d307c 5 months ago 106.7 MB
Delete images:
ahanjura#ubuntu:~$ sudo docker rmi 88282f8eda00
Deleted: sha256:88282f8eda0036f85b5652c44d158308c6f86895ef1345dfa788318e6ba31194
Deleted: sha256:4f211a991fb392cd794bc9ad8833149cd9400c5955958c4017b1e2dc415e25e9
Deleted: sha256:8cc6917ac7f0dcb74969ae7958fe80b4a4ea7b3223fc888dfe1aef42f43df6f8
Deleted: sha256:b74a8932cff5e61c3fd2cc39de3c0989bdfd5c2e5f72b8f99f2807595f8ece43
ahanjura#ubuntu:~$ sudo docker rmi 13e5d3d682f4
Error response from daemon: conflict: unable to delete 13e5d3d682f4 (must be forced) - image is being used by stopped container 5593e25eb638
Delete by force:
ahanjura#ubuntu:~$ sudo docker rmi -f 13e5d3d682f4
Deleted: sha256:13e5d3d682f4de973780b35a3393c46eb314ef3db45d3ae83baf2dd9d702747e
Deleted: sha256:3ad9381c7041c03768ccd855ec86caa6bc0244223f10b0465c4898bdb21dc378
Deleted: sha256:5ccb917bce7bc8d3748eccf677d7b60dd101ed3e7fd2aedebd521735276606af
Deleted: sha256:18356d19b91f0abcc04496729c9a4c49e695dbfe3f0bb1c595f30a7d4d264ebf
First list all your images which are present by using :
docker images
For removing single image
Use docker rmi [image-name (or) image-id]
// This will remove only that specific image
For removing All images
Use docker rmi -f $(docker images -a -q)

Resources