Docker Cleanup and Optimization

Cleans unused Docker images, containers, volumes and networks to free up disk space.

Published: March 01, 2024

Detailed Information

This script cleans unused resources in your Docker environment to free up disk space. Docker usage can take up a lot of disk space over time, and you can perform regular cleanup with this script.

What Does This Script Do?

This script optimizes Docker disk usage:

  • Removes stopped containers
  • Cleans unused (dangling) images
  • Removes unused volumes
  • Cleans unused networks
  • Clears build cache (optional)

Why Should You Use It?

Docker can use a lot of disk space over time:

  • Disk Savings: You can save gigabytes of space
  • Performance: Less resource usage
  • Organization: Cleans unused resources
  • Cost: Lower costs on cloud servers

How to Use

Step-by-Step Usage Guide

1. Create Script File

nano docker_cleanup.sh

2. Make Executable

chmod +x docker_cleanup.sh

3. Run Script

./docker_cleanup.sh

Automatic Cleanup

# Weekly cleanup with cron job
0 2 * * 0 /path/to/docker_cleanup.sh

Requirements

Requirements

  • Docker: Docker must be installed and running
  • Docker Access: Permission to run Docker commands

Use Cases

Use Cases

1. Regular Maintenance

Clean Docker resources weekly or monthly.

2. Disk Fullness

Quickly clean up when disk space is full.

3. Production Environments

Perform regular cleanup on production servers to maintain performance.

Examples

Usage Examples

Example 1: Basic Cleanup

./docker_cleanup.sh

Example 2: Full Cleanup (Careful!)

docker system prune -a --volumes

Code

#!/bin/bash

# Docker Cleanup Script

echo "======================================"
echo "   DOCKER CLEANUP UTILITY"
echo "======================================"
echo ""

if ! docker info > /dev/null 2>&1; then
    echo "Error: Docker is not running!"
    exit 1
fi

echo "Current Docker disk usage:"
docker system df
echo ""

echo "--- Cleaning stopped containers ---"
STOPPED=$(docker ps -aq -f status=exited | wc -l)
if [ $STOPPED -gt 0 ]; then
    docker rm $(docker ps -aq -f status=exited)
    echo "✓ Removed $STOPPED stopped containers"
else
    echo "No stopped containers to remove"
fi
echo ""

echo "--- Cleaning dangling images ---"
DANGLING=$(docker images -f dangling=true -q | wc -l)
if [ $DANGLING -gt 0 ]; then
    docker rmi $(docker images -f dangling=true -q)
    echo "✓ Removed $DANGLING dangling images"
else
    echo "No dangling images to remove"
fi
echo ""

echo "--- Cleaning unused volumes ---"
docker volume prune -f
echo "✓ Unused volumes cleaned"
echo ""

echo "--- Cleaning unused networks ---"
docker network prune -f
echo "✓ Unused networks cleaned"
echo ""

read -p "Clear build cache? (y/n): " CLEAR_CACHE
if [ "$CLEAR_CACHE" = "y" ]; then
    docker builder prune -f
    echo "✓ Build cache cleared"
fi
echo ""

echo "======================================"
echo "Cleanup completed!"
echo ""
echo "New Docker disk usage:"
docker system df
echo ""

Usage

chmod +x docker_cleanup.sh
./docker_cleanup.sh

# Full cleanup (careful!)
docker system prune -a --volumes

Troubleshooting

Troubleshooting

Problem: "Docker is not running"

Solution: Start Docker service:

sudo systemctl start docker

Tags

docker cleanup disk space container optimization