47

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?
Thoran
  • 8,884
  • 7
  • 41
  • 50
  • Yes, that's correct, virtual disks only get bigger; virtual drivers don't know what part of the virtual disk is in use by the OS file system and what part isn't. It's all just bits. – Mark Ransom Apr 22 '16 at 17:18

9 Answers9

33

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
Arnaud Weil
  • 2,324
  • 20
  • 19
29

On windows 10, this was a major issue, space was not freeing up, even after I ran docker system prune I started noticing this when I found that every week; my SSD was filling up every2 or 3 GB of space.

Try opening Docker Desktop, hitting the Troubleshoot icon at the top right of the UI, and then clicking "Clean / Purge data". This reclaimed the space for me.

Also see:

  1. Windows 10: Docker does not release disk space after deleting all images and containers #244
  2. Hyper V ui issue fix
  3. Clear Hyper-V recognize unused sectors
  4. Linux growing docker size issue fix
Shivam Jha
  • 3,160
  • 3
  • 22
  • 36
  • 3
    my SSD went from 37 to 54gb in free space after running that troubleshooter. Had also run `docker system prune` a couple of days ago. Thanks for the tip. – Tiramonium Dec 09 '21 at 12:55
  • 1
    Thanks. after doing everything with docker prune, only this solution works in the end. Claimed back 24GB from Docker. – blaz Jan 10 '22 at 09:39
  • 1
    69gb freed up, docker system prune didn't remove anything – PowerMan2015 May 26 '23 at 15:47
  • 1
    Thanks my WSL2 was at 600GB, the design is extremely poor for Windows. Deleting images doesn't release the wsl space. It's pretty awful it will be the size of all containers and every time you delete a container it never releases and stays forever. if you run 2 images with 5gb it provisions the space and never releases. It was eating my C: drive like crazy 600gb on a 1tb m2 is significant – Byrd Aug 11 '23 at 05:27
12

On windows using wsl2 follow the below steps. Note that you will lose all the docker images and containers if you follow this. If you want to retain data(images and containers) then you can try the prune option mentioned by other people on this thread.

Locate the vhdx file where docker stores the data. This is a virtual hard disk used by docker.

"P:\Users\your-username\AppData\Local\Docker\wsl\data\ext4.vhdx"

In my case docker is using up about 27GB of disk space in this file. Even after deleting all the images and container, docker is not releasing the free disk space back to OS. enter image description here

To reclaim the disk space, you have to try clean/purge data option from the GUI. When prompted for the data set name, select WSL 2. This should clean up and give back all the unused space.

In case, the docker daemon doesn't start up or disk space is not reduced then the last option is to factory restore from docker desktop GUI like show below. enter image description here

ns15
  • 5,604
  • 47
  • 51
6

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

Community
  • 1
  • 1
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
6

After deleting all the unwanted containers and images using the commands below:

  • Docker container rm
  • Docker image rm
  • Docker container prune
  • Docker image prune
  • Docker volume prune

I couldn't see any disk space released. I found out the disk space was released after I restarted my Docker desktop.

Nick Mehrdad Babaki
  • 11,560
  • 15
  • 45
  • 70
5

If anyone is struggling with this problem on Ubuntu:

In my case pruning and removing using the docker command did not help much.

The issue was docker/overlay2 folder.

febrin@laptop:/var/lib$ sudo du -sh docker/overlay2
361G    docker/overlay2

I had to delete it manually.

Febrin
  • 91
  • 1
  • 2
  • I used, `sudo rm -r /var/lib/docker/overlay2`, freed so much space, thank you in GB – Hassan Kanso Jan 17 '22 at 16:45
  • This works (but I guess this will delete *all* volumes, so if only want to remove some volumes, use `docker inspect` to find out where they're stored. But it doesn't explain how these volumes end up in an un-prunable state in the first place... – olejorgenb Mar 26 '23 at 21:12
0

I was facing a similar issue while doing docker-compose up I was getting some error due to dependency issues in my requirements.txt file because of which my images and containers were not getting created but my disk space was getting reduced.

I first had to resolve the dependency issues after which my images and containers were created and then

As mentioned above by other users doing Clean/Purge through the troubleshoot UI freed my disk space.

Arya Stark
  • 205
  • 1
  • 11
0

If you also can't free up space as troubleshoot->Clear/Purge answers with an error message:

wslconfig /unregister docker-desktop
wslconfig /unregister docker-desktop-data

Found it here: https://github.com/docker/for-win/issues/7295#issuecomment-653815064

This unregisters distributions running in your wsl. You can see your distributions with wsl -l -v.

My disk space was released immediately after running the second command (docker-desktop-data)

andymel
  • 4,538
  • 2
  • 23
  • 35
0

Docker cache: Docker uses a cache to improve build times and optimize image layering. The cache stores intermediate layers during the image build process. When you remove an image, Docker might still keep the cached layers, which consume disk space.

To clean up the Docker cache, you can use the docker system prune command. This command removes unused data, including cached layers. Be cautious, as it will also remove other unused resources such as containers and networks.

docker system prune

Additionally, if you want to specifically clean up only the cached image data, you can use the docker builder prune command:

docker builder prune

By using the methods mentioned above, you can ensure that both unused volumes and Docker cache are cleaned up, thus freeing up storage space on your Linux system.

Miladfa7
  • 102
  • 1
  • 7