Docker does not free up space on clean operations - docker

I have a lot of space (partition full) in the /var/lib/docker/aufs folder.
I've cleaned my volumes with :
docker volume rm $(docker volume ls -qf dangling=true)
Cleaned images with :
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
But no effect on the /var/lib/docker/aufs folder.
Any ideas?
Thanks

docker volume ls -qf dangling=true lists volumes that aren't currently assigned to a container. There may be volumes that are still in use, so not all volumes will be removed by this command. I personally avoid doing a cleanup with this command since it may remove volumes that aren't currently in use, but have important data that I want to mount into a container later.
docker images --filter "dangling=true" -q --no-trunc lists images that are not tagged. The most likely cause for this is pulling or building a new version of an old image you already had on the local machine. It doesn't cleanup all the images you've pulled or built, still have tagged, but don't use.
Neither of these commands cleanup containers that may be stopped and no longer needed. For that, you'd need to look at docker ps -af status=exited to see what containers aren't running and can be deleted with a docker rm. Scripted, that looks like:
docker rm $(docker ps -aqf status=exited)
With the 1.13 release, you can now run:
docker system prune
which will cleanup everything, or you can be more specific and clean specific pieces like:
docker container prune
docker image prune
docker volume prune
The docker image prune command can take the option -a to also prune all unused images rather than just the dangling ones.

Related

How do I delete multiple images in docker? [duplicate]

This question already has answers here:
How to remove old and unused Docker images
(29 answers)
Closed 1 year ago.
I have created a lot of images but I don't know how to delete them efficiently.
Does anyone know good command to remove the images smartly?
Thank you very much!
you can try docker rmi -f $(docker images -q)
Explanation:
docker images -q will list all the image id's. Then you pass all the image id's to
docker rmi -f
The following should delete all your unused images and stopped containers:
$ docker system prune -a
Source
At first, you need to delete/stop the running container that is using your image(which one you want to remove).
docker ps -a: To see all the running containers in your machine.
docker stop <container_id>: To stop a running container.
docker rm <container_id>: To remove/delete a docker container(only if it stopped).
docker image ls: To see the list of all the available images with their tag, image id, creation time and size.
docker rmi <image_id>: To delete a specific image.
delete rmi -f <image_id>: To delete a docker image forcefully
docker rm -f (docker ps -a | awk '{print$1}'): To delete all the docker container available in your machine
docker image rm <image_name>: To delete a specific image
To remove the image, you have to remove/stop all the containers which are using it.
docker system prune -a: To clean the docker environment, removing all the containers and images.
N.B: For docker common commands you can see: docker essential commands

Why docker system prune did not remove the dangling images while docker rmi $(docker images --filter "dangling=true" -q --no-trunc) did

This is the first time I hit the situation that docker system prune did not remove the dandling images. After running docker system prune I still have many images has the tag <none>.
docker images --filter "dangling=true" -q --no-trunc removed them, but why ?
BTW, those images were create when I pull the new ones with the same tags. There is another SO why does docker prune not remove my dangling images? talking about the similar problem but I don't think we were the same problem.
Might there still be containers based on those images?
If not: It seems that docker system prune doesn’t consider images without a tag as dangling. More on that can be found here: Moby issue
Try: docker system prune -a

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.

Remove existing image when using `docker commit`?

I have a script which I use to build docker images. The script starts up a docker container, executes some commands on it, and then does docker commit to fixturize the image. When I commit with an image name/tag, and then later commit with the same image name/tag, I would like the previous image to be removed, since at that point it's just taking up disk space. Instead it sticks around (I can see it in docker images, with its repository and tag both listed as <none>). Is there a way to have docker automatically remove these replaced images?
Not automtaically, but Docker does know which layers are not used in images, and you can remove those "dangling" image layers like this:
docker rmi $(docker images -f "dangling=true" -q)
While you're clearing up, you can also remove exited containers:
docker rm $(docker ps -a -q)
And dangling volumes:
docker volume rm $(docker volume ls -qf dangling=true)

How to remove all docker volumes?

If I do a docker volume ls, my list of volumes is like this:
DRIVER VOLUME NAME
local 305eda2bfd9618266093921031e6e341cf3811f2ad2b75dd7af5376d037a566a
local 226197f60c92df08a7a5643f5e94b37947c56bdd4b532d4ee10d4cf21b27b319
...
...
local 209efa69f1679224ab6b2e7dc0d9ec204e3628a1635fa3410c44a4af3056c301
and I want to remove all of my volumes at once. How can I do it?
The official command to remove all unused data (including volumes without containers) will be with docker 1.13
docker system prune
If you want to limit to volumes alone, removing only unused volumes:
docker volume prune
You also have docker image prune, docker container prune, etc:
See more at "Prune unused Docker objects".
See commit 86de7c0 and PR 26108.
You can see it in action in play-with-docker.com:
/ # docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1296a5e47ef3 hello-world "/hello" 7 seconds ago Exited (0) 6 seconds ago prickly_poincare
/ # docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all dangling images
Are you sure you want to continue? [y/N] y
Deleted Containers:
1296a5e47ef3ab021458c92ad711ad03c7f19dc52f0e353f56f062201aa03a35
The current (pre-docker 1.13) way of managing volume was introduced with PR 14242 and the docker volume command, which documents in its comment from July 2015:
docker volume rm $(docker volume ls -q --filter dangling=true)
Edited on 2017:
This answer was given on Apr 16 '16 and now is outdated, and correct only for docker version prior to 1.13
please use the answer from #VonC, now it is marked as correct
To delete unused volumes you can use the built-in docker volume rm command. The rm command also deletes any directory in /var/lib/docker/volumes that is not a volume, so make sure you didn't put anything in there you want to save:
Command to List volumes, little bit right than yours:
$ docker volume ls -qf dangling=true
Cleanup:
$ docker volume rm $(docker volume ls -qf dangling=true)
more details about ls here, about rm here
This is what I've found to be useful: https://github.com/chadoe/docker-cleanup-volumes
Shellscript to delete orphaned docker volumes in /var/lib/docker/volumes and /var/lib/docker/vfs/dir
Docker version 1.4.1 up to 1.11.x
It basically does a cleanup of any orphaned/dangling volumes, but it includes a --dry-run but it makes note of some docker included commands as well (which are referenced in prev comment)
Note about Docker 1.9 and up
To delete orphaned volumes in Docker 1.9 and up you can also use the built-in docker volume commands instead of this docker-cleanup-volumes script. The built-in command also deletes any directory in /var/lib/docker/volumes that is not a volume so make sure you didn't put anything in there you want to save:
List:
$ docker volume ls -qf dangling=true
Cleanup:
$ docker volume rm $(docker volume ls -qf dangling=true)
Or, handling a no-op better but Linux specific:
$ docker volume ls -qf dangling=true | xargs -r docker volume rm
To answer the question and borrowing from Marc, this works:
$ docker volume rm $(docker volume ls -qf dangling=true | xargs)

Resources