Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Image Taking up Space After Deletion

I committed an existing Docker container to create a new image but the committed image was way too large (the original image was 3+ GB, and with just a few additional things installed in the container, the committed image was 8+GB).

I'm low on space, so I decided to delete the committed image, but even after deleting the committed image, my disk space hasn't gone up. Yet, when I run docker system df it's showing only the space that should be used original image and the container (I only have one image and one container).

Any way for me to free up the space that I'm sure is somehow still being occupied by that image I deleted? (For reference, I created the image, then deleted it, all within an hour, and was constantly monitoring my free space, so I'm pretty sure it's this and not something else that's taking up space.)

Also, I'm using Docker Desktop for Windows with WSL2 integration so it's not as easy to check Docker's disk use by just going to /var/lib/docker and checking disk usage.

like image 284
anonymous1a Avatar asked Sep 25 '20 16:09

anonymous1a


People also ask

What happens if you delete docker image?

Nothing. It simply ran flawless from the exact state it was before having its image deleted.

Does removing an image from a docker container free up disk space?

Most of the space is occupied by docker volume as you can see from your output: Docker volumes are used to persist data for docker container and to share data between containers, and they are independent of the container’s lifecycle. So removing image/container will not free the disk space they occupied.

What happens if my Docker space is too full?

If your space is full in my experience docker prune will just hang. You need to manually delete the volumes. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.

How much space does Docker reclaim from a container?

Total reclaimed space: 6.749GB [docker]# du -sh . 2.7G . It's a kernel problem with devicemapper, which affects the RedHat family of OS (RedHat, Fedora, CentOS, and Amazon Linux). Deleted containers don't free up mapped disk space. This means that on the affected OSs you'll slowly run out of space as you start and restart containers.

How do I stop Docker from eating up disk space?

A work-around of sorts is to give Docker its own volume to write to ( "When Docker eats up you disk space" ). This doesn't actually stop it from eating space, just from taking down other parts of your system after it does. My solution was to uninstall docker, then delete all its files, then reinstall:


2 Answers

I have used these commands to shrink desktop image from 35GB to 1GB

(in windows 10, docker version 19.03.13)

Remove unused images and other resources

docker system prune -a

Remove more resources

docker volume rm $(docker volume ls -q -f dangling=true)

Stop docker desktop, optimize image

Make sure HyperV turned on

%windir%\System32\mmc.exe "%windir%\System32\virtmgmt.msc" or type Hyper-V in win search

Optimize disk in GUI > Go to VM, and check disk

Optimize-VHD -Path "C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx" -Mode Full

Start docker desktop

Now I have 34+GB free disk space

Cleanup using docker desktop app

You can purge data when you click on bug icon in header and then click clean / purge data..

enter image description here

like image 159
Scholtz Avatar answered Oct 05 '22 08:10

Scholtz


Since you are on windows and running in docker for desktop your actually running docker in a VM. Then it depends what the disk image size is set to, usually virtual disks will expand to fill that space.

you can shrink a disk image though:

  • stop docker desktop

  • open an admin powershell terminal

  • run Optimize-VHD -Path "C:\ProgramData\DockerDesktop\vm-data\DockerDesktop.vhdx" -Mode Full where the path is the path as documented in the docker desktop settings > Resources > Advanced > Disk image location.

  • start docker desktop

This should shrink the docker virtual machine.

like image 31
Damo Avatar answered Oct 05 '22 08:10

Damo