Skip to content

Instantly share code, notes, and snippets.

@wowkin2
Created May 7, 2025 19:28
Show Gist options
  • Save wowkin2/55ac6226e46ddbab92f35bcc094df069 to your computer and use it in GitHub Desktop.
Save wowkin2/55ac6226e46ddbab92f35bcc094df069 to your computer and use it in GitHub Desktop.

How to clean Docker.raw image

Many times I stuck in situation when Docker consumes whole 70Gb, then I remove all Containers and Images in Docker, and only Volumes remaining which should consume ~7Gb. But storage is not free anymore. Hard to say what is using it. It can probably be just reserved by and not released. Found the way to backup volumes, drop virtual machine, create new and restore volumes.

Steps

  • To create backups - create file from example below and run that:
chmod +x backup_volumes.sh
./backup_volumes.sh
  • Quit Docker Desktop
  • Delete the Docker.raw file
rm ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw
  • Restart Docker Desktop.

    • It will create a fresh Docker.raw (~1–2 GB).
    • All images, volumes, and containers will be gone.
  • To restore from backups - create file from example below and run that:

chmod +x restore_volumes.sh
./restore_volumes.sh
#!/bin/bash
set -e
BACKUP_DIR="./docker_volume_backups"
mkdir -p "$BACKUP_DIR"
echo "πŸ“¦ Backing up Docker volumes to $BACKUP_DIR"
for volume in $(docker volume ls -q); do
echo "πŸ”„ Backing up volume: $volume"
docker run --rm -v ${volume}:/volume -v "$BACKUP_DIR":/backup alpine \
sh -c "tar czf /backup/${volume}.tar.gz -C /volume ."
done
echo "βœ… All volumes backed up!"
#!/bin/bash
set -e
BACKUP_DIR="./docker_volume_backups"
echo "♻️ Restoring Docker volumes from $BACKUP_DIR"
for archive in "$BACKUP_DIR"/*.tar.gz; do
volume=$(basename "$archive" .tar.gz)
echo "πŸ” Restoring volume: $volume"
docker volume create "$volume"
docker run --rm -v "$volume":/volume -v "$BACKUP_DIR":/backup alpine \
sh -c "tar xzf /backup/${volume}.tar.gz -C /volume"
done
echo "βœ… All volumes restored!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment