DEV Community

Diego Carrasco Gubernatis
Diego Carrasco Gubernatis

Posted on • Originally published at diegocarrasco.com on

2

How to Optimize and Free Up Disk Space on Debian/Ubuntu Servers with Docker Containers

TLDR

Manage disk space on Debian/Ubuntu servers and Docker containers by removing unnecessary packages, cleaning up caches, and pruning Docker objects.

Context

I needed to free up space as I had a small VPS with full storage, and my notebook and desktop computers had also a really high disk usage, although I did not have that many files, but I do use a lot of docker.

After researching I did not find a guide with everything I needed (explanations included), thus here it is.

Steps

Package Manager (apt)

Remove packages that are no longer required

sudo apt-get autoremove

Enter fullscreen mode Exit fullscreen mode

Clean Up APT Cache

Check the space used by the APT cache:

sudo du -sh /var/cache/apt

Enter fullscreen mode Exit fullscreen mode

Clean up the APT cache:

sudo apt-get autoclean
sudo apt autoclean

Enter fullscreen mode Exit fullscreen mode

Delete cache files:

sudo apt-get clean
sudo apt clean

Enter fullscreen mode Exit fullscreen mode

Clear Systemd Journal Logs

Check the disk usage of systemd journal logs:

journalctl --disk-usage

Enter fullscreen mode Exit fullscreen mode

Clean logs older than 3 days:

sudo journalctl --vacuum-time=3d

Enter fullscreen mode Exit fullscreen mode

Docker

Docker takes a lot of space compared to vanilla servers. Check link:/slug/change-docker-data-directory-vps-optimization for a related post on the overlay2 and how to move docker data root to another volume/ drive.

Check system usage

Check overall system usage:

docker system df

Enter fullscreen mode Exit fullscreen mode

For more detailed information:

docker system df -v

Enter fullscreen mode Exit fullscreen mode

Use docker system prune

(from documentation)

WARNING! This will remove:

  • all stopped containers
  • all networks not used by at least one container
  • all dangling images
  • all build cache
docker system prune

Enter fullscreen mode Exit fullscreen mode
Use Docker Container Prune

Warning: This will remove all stopped containers. Refer to the documentation for more details.

docker container prune # Remove all stopped containers

Enter fullscreen mode Exit fullscreen mode
Use Docker Image Prune

Remove unused images (Remove all dangling images. If -a is specified, will also remove all images not referenced by any container.)

Quick note:

What are Docker Dangling Images?

  • Images that have no tag and are not referenced by any container source
  • Untagged layers that serve no purpose but still consume disk space.
  • Not automatically removed by Docker and need to be cleaned up manually. source
docker image prune

Enter fullscreen mode Exit fullscreen mode

Use docker volume prune

Remove all unused local volumes. Unused local volumes are those which are not referenced by any containers. By default, it only removes anonymous volumes.

docker volume prune # remove only anonymous (unnamed) volumes

Enter fullscreen mode Exit fullscreen mode

This command removes only anonymous (unnamed) volumes by default.

To remove all unused volumes:

docker volume prune -a # remove all unused volumes

Enter fullscreen mode Exit fullscreen mode

References

Related tools I found interesting during my search

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay