Skip to content

Instantly share code, notes, and snippets.

@sycomix
Created May 4, 2025 00:21
Show Gist options
  • Select an option

  • Save sycomix/b4f803f490cabaad14b710ffc9edfd71 to your computer and use it in GitHub Desktop.

Select an option

Save sycomix/b4f803f490cabaad14b710ffc9edfd71 to your computer and use it in GitHub Desktop.
unfuck docker in debian/ubuntu
#!/bin/bash
# --- Docker Purge Script for Ubuntu (Interactive) ---
#
# WARNING: This script uninstalls Docker and can optionally remove containers,
# images, volumes, and data directories, which can lead to data loss.
# Backup any important data (especially in Docker volumes) before proceeding.
#
# Run this script with root privileges: sudo bash purge_docker_interactive.sh
# Function to ask Yes/No questions
ask_yes_no() {
while true; do
read -p "$1 [y/N]: " yn
case $yn in
[Yy]* ) return 0;; # Yes, return success (0)
[Nn]* | "" ) return 1;; # No or Enter, return failure (1)
* ) echo "Please answer yes (y) or no (n).";;
esac
done
}
echo "--- Starting Docker Purge (Interactive) ---"
echo "This script will guide you through uninstalling Docker components."
echo ""
DOCKER_AVAILABLE=false
if command -v docker &> /dev/null && docker info &> /dev/null; then
echo "Docker daemon seems to be running. Proceeding with Docker cleanup options."
DOCKER_AVAILABLE=true
else
echo "Docker command not found or daemon not accessible."
echo "Skipping Docker object cleanup (containers, images, volumes)."
echo "Proceeding directly to service stopping and package removal."
fi
# 1. Optional: Stop running containers
if $DOCKER_AVAILABLE; then
if ask_yes_no "Do you want to STOP all currently running Docker containers?"; then
echo "Stopping all running Docker containers..."
docker ps -aq | xargs -r docker stop || echo "Failed to stop some containers or no running containers found."
echo "Containers stopped."
else
echo "Skipping container stop."
fi
echo ""
fi
# 2. Optional: Prune system (Containers, Images, Networks, Cache)
# Note: `docker system prune -a` removes ALL images (not just dangling) and stopped containers.
if $DOCKER_AVAILABLE; then
if ask_yes_no "Do you want to perform a system prune? (Removes ALL stopped containers, ALL unused networks, ALL images (not just dangling), and build cache)"; then
echo "Running docker system prune -a -f..."
docker system prune -a -f || echo "Docker system prune failed."
echo "System prune completed."
else
echo "Skipping system prune."
fi
echo ""
fi
# 3. Optional: Prune Volumes (Data Loss Risk!)
if $DOCKER_AVAILABLE; then
if ask_yes_no "**DANGER:** Do you want to remove ALL UNUSED Docker volumes? This can lead to DATA LOSS if volumes are not currently attached to containers but still hold data you need."; then
echo "Running docker system prune --volumes -f..."
docker system prune --volumes -f || echo "Docker volume prune failed."
echo "Volume prune completed."
else
echo "Skipping volume prune."
fi
echo ""
fi
# 4. Stop Docker and related services
echo "Stopping Docker services..."
sudo systemctl stop docker.service || echo "Failed to stop docker.service (may not exist)."
sudo systemctl stop docker.socket || echo "Failed to stop docker.socket (may not exist)."
sudo systemctl stop containerd.service || echo "Failed to stop containerd.service (may not exist)."
sudo systemctl disable docker.service || echo "Failed to disable docker.service (may not exist)."
sudo systemctl disable docker.socket || echo "Failed to disable docker.socket (may not exist)."
sudo systemctl disable containerd.service || echo "Failed to disable containerd.service (may not exist)."
echo ""
# 5. Uninstall Docker packages (including dependencies and config files)
echo "Uninstalling Docker packages (using purge to remove config files)..."
sudo apt-get purge -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin \
docker-ce-rootless-extras \
docker-scan-plugin || echo "Failed to purge some Docker CE packages (they might not be installed)."
# Attempt to remove older/alternative Docker packages as well
sudo apt-get purge -y \
docker \
docker-engine \
docker.io \
runc || echo "Failed to purge some older Docker packages (they might not be installed)."
echo "Package removal complete."
echo ""
# 6. Optional: Remove residual Docker configuration directory
if [ -d "/etc/docker" ]; then
if ask_yes_no "Do you want to remove the Docker configuration directory (/etc/docker)? ('apt purge' should have done this, but this is a safeguard)"; then
echo "Removing /etc/docker..."
sudo rm -rf /etc/docker
echo "/etc/docker removed."
else
echo "Skipping removal of /etc/docker."
fi
else
echo "/etc/docker directory not found."
fi
# Also check for containerd config
if [ -d "/etc/containerd" ]; then
if ask_yes_no "Do you want to remove the containerd configuration directory (/etc/containerd)?"; then
echo "Removing /etc/containerd..."
sudo rm -rf /etc/containerd
echo "/etc/containerd removed."
else
echo "Skipping removal of /etc/containerd."
fi
else
echo "/etc/containerd directory not found."
fi
echo ""
# 7. Optional: Remove Docker data directory (Data Loss Risk!)
if [ -d "/var/lib/docker" ]; then
if ask_yes_no "**DANGER:** Do you want to remove the main Docker data directory (/var/lib/docker)? This permanently deletes any remaining images, container filesystems, and volumes NOT removed earlier."; then
echo "Removing /var/lib/docker..."
sudo rm -rf /var/lib/docker
echo "/var/lib/docker removed."
else
echo "Skipping removal of /var/lib/docker. Your Docker data may still reside there."
fi
else
echo "/var/lib/docker directory not found."
fi
echo ""
# 8. Optional: Remove containerd data directory
if [ -d "/var/lib/containerd" ]; then
if ask_yes_no "Do you want to remove the containerd data directory (/var/lib/containerd)?"; then
echo "Removing /var/lib/containerd..."
sudo rm -rf /var/lib/containerd
echo "/var/lib/containerd removed."
else
echo "Skipping removal of /var/lib/containerd."
fi
else
echo "/var/lib/containerd directory not found."
fi
echo ""
# 9. Remove runtime socket (safe)
echo "Removing runtime socket file /var/run/docker.sock..."
sudo rm -f /var/run/docker.sock
echo ""
# 10. Clean up APT cache and remove unused dependencies
echo "Cleaning up APT..."
sudo apt-get autoremove -y
sudo apt-get autoclean -y
echo ""
# 11. Optional: Remove the Docker group
if getent group docker > /dev/null; then
if ask_yes_no "Do you want to remove the 'docker' system group?"; then
echo "Removing 'docker' group..."
# Force removal even if it's a primary group for some user (unlikely for 'docker')
sudo groupdel docker || echo "Failed to remove docker group (maybe users still belong to it?)."
else
echo "Skipping removal of 'docker' group."
fi
else
echo "'docker' group does not exist."
fi
echo ""
# 12. Reload systemd daemon configuration
echo "Reloading systemd daemon configuration..."
sudo systemctl daemon-reload
echo "--- Docker Purge Script Finished ---"
echo "Docker packages have been uninstalled."
echo "Review the output above to see if containers, images, volumes, or data directories were removed based on your choices."
echo "It's recommended to reboot your system to ensure all changes take effect."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment