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