Skip to main content

๐Ÿ›‘ Getting "No Space" Error After Using Docker for Some Time? Use These Commands to Clean Up ๐Ÿ›‘

docker logo



Docker is a fantastic tool for containerizing applications, allowing developers to package their software along with dependencies into lightweight, portable containers. These containers can run consistently across different environments, making Docker ideal for development, testing, and deployment. However, over time, unused images, containers, volumes, and networks can pile up, consuming significant disk space. If you’re encountering a "no space left on device" error or simply want to free up space, Docker provides several prune commands to help with cleanup. Let’s dive into each of these commands and how to use them effectively. ๐ŸŽ‰


1. docker image prune ๐Ÿ–ผ️

This command removes dangling images. Dangling images are layers that are no longer tagged or associated with a container.

Usage:

docker image prune

To remove unused images (not just dangling ones), use the -a flag:

docker image prune -a
Pro Tip: Combine the -a flag with filters for more control, like:
docker image prune -a --filter "until=1h"

This removes images that were unused for more than 1 hour. ๐Ÿ•’

docker image prune -a --filter "until=3h15m"

Removes images unused for more than 3 hours and 15 minutes. ⏳

docker image prune -a --filter "until=1h30m45s"

Removes images unused for more than 1 hour, 30 minutes, and 45 seconds. ⏱️

docker image prune -a --filter "until=24h"

Removes images unused for more than 24 hours. ๐Ÿ•›

docker image prune -a --filter "until=7d"

Removes images unused for more than 7 days. ๐Ÿ—“️


2. docker container prune ๐Ÿ› ️

This command removes all stopped containers. Stopped containers often take up disk space unnecessarily.

Usage:

docker container prune

You can filter stopped containers to target specific ones. For example:

docker container prune --filter "until=1h"

Removes containers that have been stopped for more than 1 hour. ๐Ÿ”ง

docker container prune --filter "until=3h15m"

Removes containers that have been stopped for more than 3 hours and 15 minutes. ๐Ÿ”

docker container prune --filter "until=1h30m45s"

Removes containers that have been stopped for more than 1 hour, 30 minutes, and 45 seconds. ๐Ÿ•ฐ️

docker container prune --filter "until=12h"

Removes containers that have been stopped for more than 12 hours. ⚙️

docker container prune --filter "until=7d"

Removes containers that have been stopped for more than 7 days. ๐Ÿ—“️


3. docker volume prune ๐Ÿ“ฆ

Volumes store data used by containers. Over time, unused volumes can consume a lot of space. This command removes all unused volumes.

Usage:

docker volume prune

If you want to avoid accidentally deleting critical volumes, double-check which ones are unused using:

docker volume ls -f dangling=true

4. docker network prune ๐ŸŒ

Unused networks can also contribute to clutter. This command removes networks that are not used by any container.

Usage:

docker network prune

As with other prune commands, you can use filters to target specific networks:

docker network prune --filter "until=1h"

Removes networks unused for more than 1 hour. ๐Ÿ•’

docker network prune --filter "until=3h15m"

Removes networks unused for more than 3 hours and 15 minutes. ⏳

docker network prune --filter "until=1h30m45s"

Removes networks unused for more than 1 hour, 30 minutes, and 45 seconds. ⏱️

docker network prune --filter "until=48h"

Removes networks unused for more than 48 hours. ๐ŸŒŸ

docker network prune --filter "until=7d"

Removes networks unused for more than 7 days. ๐Ÿ—“️


5. docker system prune ๐Ÿงน

This command is your go-to for general cleanup. It removes:

  • Stopped containers
  • Unused networks
  • Dangling images (images not associated with any container)
  • Build cache

Usage:

docker system prune

You’ll be prompted to confirm the action. If you want to bypass the confirmation, use the -f (force) flag:

docker system prune -f

Want to clean up unused volumes as well? Add the --volumes flag:

docker system prune --volumes

Note: Be cautious when using this command with --volumes, as it will remove all unused volumes, which could include data you may want to keep. ⚠️


Combining Commands for Maximum Cleanup ๐Ÿงฝ

To perform a thorough cleanup, you can combine these commands into a single script or execute them sequentially:

docker container prune -f
docker image prune -a -f
docker volume prune -f
docker network prune -f

Or, use the all-encompassing:

docker system prune -a --volumes -f

Warning: Be cautious when using -a and --volumes as they may remove data you still need. ⚠️


Proactive Space Management Tips ๐Ÿ“Š

  1. Use Docker Disk Usage Command: To understand what’s consuming space:

    docker system df
    
  2. Set Up Automatic Cleanup: Use tools like cron jobs or CI/CD pipelines to periodically run prune commands and keep your system tidy. ๐Ÿ•’

  3. Be Mindful with Volume Usage: Explicitly manage volumes to avoid orphaned data. ๐Ÿ“ฆ

  4. Monitor Build Cache: Large builds can leave behind substantial cache files. Regularly prune or optimize build steps to avoid excessive use. ⚙️


By using these prune commands effectively, you can keep your Docker environment clean and avoid the dreaded "no space left on device" error. Regular maintenance not only saves space but also ensures Docker runs smoothly for all your projects. ๐Ÿš€

Comments

Popular posts from this blog

How to install and configure termux on Android

Note: don’t install from the Google Play Store, it is a highly stripped-down version of the original The options present in my own priority order Download from GitHub releases Download using Fdroid download from GitHub releases Just go to GitHub releases , download the APK, and install it. We will see configuration steps later ! The only downside is you need to redownload on each release download using FDroid You can use Fdroid as package manager instead of Google play store Now download completed what’s next setup the nearest mirrors using termux-change-repo setup the storage using termux-setup-storage Do update and upgrade termux configure nearest mirrors Run termux-change-repo And it will scan all the nearest repos and configure the default one which is the nearest setup storage using termux-setup-storage It usually asks for some permissions and do some storage setup don’t remember what is actually do but used it to fix a problem some time ago do some PKG update and upgrade Run com...

Introduction to Protocol Buffers (Protobuf): A Compact and Efficient Data Serialization Format ๐Ÿš€

In the world of software development, communication between systems, especially distributed systems, is crucial. Whether it's for sending data over a network, storing information, or inter-process communication, the format of data exchanged plays a significant role in the efficiency and ease of use. One such data serialization format that has gained widespread popularity is Protocol Buffers (Protobuf) . ๐Ÿ’พ What is Protocol Buffers? ๐Ÿค” Protocol Buffers, commonly known as Protobuf , is a lightweight and language-neutral data serialization format developed by Google. It allows for the efficient and structured representation of data, making it an excellent choice for communication between services, storing data in files, or for persistent data formats. Protobuf works by defining data structures in a language-agnostic way, and then compiling these definitions into source code that can be used across various programming languages such as Java, C++, Python, and more. ๐ŸŒ Why Should Y...