Why removing docker containers and images does not

2020-06-04 00:31发布

问题:

Problem:

As I download and run containers, they keep taking up more and more space. I found suggestions for cleaning up unused containers and images and I did so. Guess what? They eat up even more disk space!

What I found so far:

It has to do with .docker\machine\machines\default\disk.vmdk file. It only gets bigger!

Log of disk.vmdk:

                            size (MB)
1. with 2 images                1,376
2. downloading a new image X    ?
3. running X as Y               2,963
4. removing Y                   2,963
5. removing X                   3,106
6. removing all the images      3,126

The only fix I found so far was running docker-machine rm default which removes the VM. The problem is that I have to download all the images again. There should be a better fix. Can someone explain:

  1. What is going on?
  2. How to fix it?

回答1:

There are maintainance commands you can run on the more recent versions of Docker. They will free up space used by stopped containers, dangling images and dangling volumes:

docker container prune -f
docker image prune -f
docker volume prune -f


回答2:

Maybe the images you are using, use volumes. If they do then deleting the container doesn't do the trick. You must delete the volumes as well. In order to do that you must specify the -v flag when deleting a container

docker rm -v <container name or container id>

Depending on your docker version you will have some more commands available. Check this SO thread for more. You can read more about orphaned volumes in this SO thread