How to fix docker error no space left on device?

Sometimes you must clean up your disk so that you can download the docker image and run it. But if you do not have sufficient space available on your disk then you might end up with the error no space left on device.

In this blog, we are going to see a number of ways to fix this error because I had this issue many times while working with docker.

Table of Content

  1. Using docker system prune (docker system prune, docker system prune --volume, docker system prune --all)
  2. Delete orphaned volumes
  3. Delete unused images
  4. Default docker storage .i.e. - /var/lib/docker
  5. Docker space issue for Mac

1. docker system prune

The first command which comes to my mind for freeing space is to run the following command is docker system prune

(Note: - docker system prune can be run with different parameter, so please read through all the option before running the command)

1docker system prune 

But always run this command with caution because it will just wipe everything for you and if you want to retain some of your other docker containers then you might lose those.

Once you will run the above command it is going to ask for the confirmation -

1WARNING! This will remove:
2  - all stopped containers
3  - all networks not used by at least one container
4  - all dangling images
5  - all dangling build cache
7Are you sure you want to continue? [y/N]

You can press Y and it will free up some space for you.

docker system prune --volumes

The docker system prune prunes images, containers, and networks but it never prunes/delete the volumes associated with the docker container. If the docker system prune does not work for you then try to delete the volumes by supplying the --volumes -

1docker system prune --volumes

docker system prune --all

If you want to prune images, containers, networks along with volumes then you should supply --all parameter -

1docker system prune --all

2. Delete orphaned volumes

The second approach I would take to remove all the orphaned volumes associated with the docker. But before you remove all of the orphaned volumes, I would recommend you to list out all the orphaned volumes before deleting it -

1docker volume ls -qf dangling=true

Now you can run the following command to remove all the orphaned volumes -

1docker volume rm $(docker volume ls -qf dangling=true)

3. Delete unused images

Before coming to this step I would highly encourage you to check the step-2 where we have deleted the volumes.

Alright now I am assuming you have already tried removing the unused volumes associated with your docker container and now first check how many docker images you have onto your system -

1docker image ls 

The above command will list out all the docker images available. Please take note of the image name which you want to delete.

Now you can run the following command by replacing the <image_name> -

1docker rmi $(docker images | grep `<image_name>` | awk '{print $3}')

Or you can run the following command -

1docker rmi {image-name}

4.Default docker storage .i.e. - /var/lib/docker

If all the previously mentioned 3 steps do not work for you then it's time to check the default docker storage and generally it's around ~6GB. If you are working with docker for quite some time then it is highly possible that you might have consumed all the available default docker storage.

In general speaking the default docker storage location is - /var/lib/docker

Now we need to change the default docker storage location from - /var/lib/docker to something else /app/sample_direction/my_local_storage.

Step 1 : Create storage directory

Run the following mkdir command (Note - Change the directory name as per your requirement)

1mkdir /app/sample_direction/my_local_storage

Step 2 : Change the permission

Make sure to change the directory permission after you have created the directory -

1chmod -R 777 /app/sample_direction/my_local_storage

Step 3 : Update docker.service

Now update the docker.service file which generally present under /usr/lib/systemd/system

1vi /usr/lib/systemd/system/docker.service 

It will open up docker.service in edit mode. Now you need to update your default docker storage path to - /app/sample_direction/my_local_storage

1ExecStart=/usr/bin/dockerd -s devicemapper --storage-opt dm.fs=xfs --storage-opt dm.basesize=40GB -g /app/sample_direction/my_local_storage --exec-opt native.cgroupdriver=cgroupfs

(Note:If the storage device is overlay2)

In case if your storage device is overlay2 then you might need to add add -g /apps/newdocker/docker to your docker.service

Step 4 : Remove /var/lib/docker

Now we need to delete the default docker directory .i.e. - /var/lib/docker

1rm -rf /var/lib/docker 

Step 5 : stop and restart docker service

First, stop the docker service -

1systemctl stop docker 

Next, reload the Docker daemon -

1systemctl daemon-reload 

Now start your docker daemon -

1systemctl start docker

5. Docker space issue for Mac

In case you are working on Mac and then you could try one more approach to free up some space by deleting Docker.raw file.

Mac OS is a little special and it creates a docker.raw file for local docker storage and over time this file piles up. You can locate the file here -

1Preferences > Resources > Advanced > Disk Image Location

You can check the file size and then delete it. It should fix your issue error no space left on device.

Here are some reference for further reading -

  1. Stackoverflow - Docker error : no space left on device
  2. -

Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this series