Thoran
Thoran

Reputation: 9470

Why removing docker containers and images does not free up storage space on Windows? How to fix it?

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?

Upvotes: 68

Views: 68998

Answers (12)

PeterF
PeterF

Reputation: 1

When I worked with docker, I had a problem with getting free space on my hard disc less and less. I made docker compose --build, I deleted old images signed as "none", but the space on my disc didn't free. Docker prune with different combinations also didn't help me. So I have found the problem. If you'll check the folder C:\Users<your_username>\AppData\Local\Temp, you may see, that it is enormous big, I had that folder 50 GB big. I think, Windows 10 keeps there cached useless docker images. So, I've deleted almost whole that folder, I've just left data for up to a week.

Upvotes: 0

stefanoz
stefanoz

Reputation: 51

First you need to delete unused build cache:

docker builder prune

Then follow these steps:

wsl --shutdown
wsl.exe --list --verbose
diskpart
select vdisk file="C:\Users\...\AppData\Local\rancher-desktop\distro-data\ext4.vhdx”
compact vdisk

And finally your storage space will free up!

Upvotes: 4

Jimmy Mohamad
Jimmy Mohamad

Reputation: 19

On Windows 10, I deleted the WSL distro data manually from the docker folder in

AppData\Local\Docker\wsl\data 

after that just run the following command

wsl --unregister docker-desktop-data

and restart docker desktop it worked for me!

Upvotes: 1

Miladfa7
Miladfa7

Reputation: 412

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.

Upvotes: 18

andymel
andymel

Reputation: 5706

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)

Upvotes: 1

Arya Stark
Arya Stark

Reputation: 243

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.

Upvotes: 0

ns15
ns15

Reputation: 8804

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

Upvotes: 24

Febrin
Febrin

Reputation: 109

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.

Upvotes: 5

Shivam Jha
Shivam Jha

Reputation: 4522

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

Upvotes: 56

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12545

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.

Upvotes: 6

Arnaud Weil
Arnaud Weil

Reputation: 2492

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

Upvotes: 44

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17755

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

Upvotes: 6

Related Questions