How to Completely Uninstall Docker
In this tutorial, we will show you how to completely uninstall Docker. Docker has revolutionized the way developers build, ship, and run applications through containerization technology. However, there are scenarios where you might need to completely remove Docker from your system. Whether you’re troubleshooting persistent issues, performing system cleanup, or preparing for a fresh installation, knowing how to properly uninstall Docker is crucial for maintaining a clean and efficient system.
This comprehensive guide will walk you through the complete Docker removal process, ensuring no residual files, configurations, or processes remain on your system. We’ll cover various scenarios and provide detailed instructions for different operating systems, with a primary focus on Ubuntu and Linux distributions.
Why Complete Docker Uninstallation Matters
Removing Docker isn’t as simple as running a single uninstall command. Docker creates an extensive footprint across your system, including images, containers, volumes, network configurations, and various system-level integrations. Incomplete removal can lead to several issues including disk space consumption, conflicting installations, and system performance degradation.
Understanding the importance of thorough removal helps prevent future complications and ensures your system remains optimized for other applications or a fresh Docker installation.
Prerequisites and Important Considerations
System Requirements and Permissions
Before beginning the Docker removal process, ensure you have administrative privileges on your system. Most uninstallation commands require sudo
access or root permissions. Additionally, verify that you’re prepared to lose all Docker-related data, as this process is irreversible.
Critical Data Backup
The uninstallation process will permanently delete all Docker images, containers, volumes, and configuration files. If you have important data or custom configurations, create backups before proceeding. Export any critical containers using docker export
or save important images with docker save
commands.
Consider documenting your current Docker setup, including custom networks, volume mappings, and container configurations. This documentation will prove invaluable if you decide to reinstall Docker later.
Preparing for Docker Removal
Assessing Your Current Docker Installation
Start by evaluating your current Docker setup to understand what needs to be removed. Run the following commands to get a comprehensive overview:
docker ps -a
docker images
docker volume ls
docker network ls
These commands will show you all containers, images, volumes, and networks currently on your system. Take note of any critical data that might need backing up.
Stopping All Docker Processes
Before proceeding with uninstallation, properly stop all running Docker processes. This prevents data corruption and ensures a clean removal process.
First, stop all running containers:
sudo docker stop $(sudo docker ps -q)
For containers that refuse to stop gracefully, force removal:
sudo docker rm -f $(docker ps -aq)
Next, use Docker’s built-in cleanup command to remove stopped containers, unused networks, images, and build cache:
sudo docker system prune --volumes
This command provides a comprehensive cleanup that removes most Docker artifacts while maintaining system integrity.
Complete Docker Uninstallation on Ubuntu/Linux
Method 1: Identifying and Removing Docker Packages
The first step in complete Docker removal involves identifying all Docker-related packages installed on your system. Run the following command to see what Docker packages are currently installed:
dpkg -l | grep -i docker
This command will display all packages containing “docker” in their name, helping you identify exactly what needs to be removed.
Once you’ve identified the packages, proceed with removal using the purge command:
sudo apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli
The purge command not only removes the packages but also deletes associated configuration files. Follow this with the autoremove command to clean up dependencies:
sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce
Method 2: Removing Docker Using Wildcard Patterns
For a more comprehensive approach, you can remove all Docker-related packages using wildcard patterns:
sudo apt-get remove 'docker*'
sudo apt-get purge 'docker*'
This method ensures that even packages with unconventional naming are removed from your system.
Stopping Docker Services
Before removing system files, ensure all Docker services are completely stopped:
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl disable docker
These commands stop the Docker daemon, socket, and prevent automatic startup.
Comprehensive System Cleanup
Removing Docker Data Directories
Docker stores significant amounts of data in specific system directories. Complete removal requires manually deleting these directories:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
sudo rm -rf /etc/docker
The /var/lib/docker
directory contains all images, containers, and volumes. The /var/lib/containerd
directory stores containerd runtime data, while /etc/docker
contains configuration files.
Advanced Cleanup Procedures
For thorough system cleanup, remove additional Docker-related files and configurations:
sudo rm /etc/apparmor.d/docker
sudo rm -rf /var/run/docker.sock
sudo rm -rf ~/.docker
These commands remove AppArmor profiles, Docker socket files, and user-specific Docker configurations.
Removing Docker User Group
If you previously added users to the Docker group for sudo-less Docker operations, remove the group entirely:
sudo groupdel docker
This step ensures complete removal of Docker’s system integration.
Cleaning Up APT Sources and GPG Keys
If you installed Docker from the official repository, remove the associated APT sources and GPG keys:
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.asc
This prevents package manager conflicts and ensures clean repository listings.
Advanced Cleanup Techniques
Finding and Removing Residual Files
Even after following standard removal procedures, some Docker files might remain on your system. Use the find command to locate any remaining Docker-related files:
sudo find / -name '*docker*' -type f 2>/dev/null
Review the output carefully before removing files, as some might belong to other applications that simply contain “docker” in their names.
For files that originated from Docker packages, you can identify their source using:
dpkg-query -S $(sudo find / -name '*docker*' -print 2>/dev/null)
Remove any packages that are no longer needed, but exercise caution to avoid removing essential system components.
Handling Docker Compose
If you installed Docker Compose separately, remove it manually:
sudo rm /usr/local/bin/docker-compose
Check for Docker Compose in other common installation locations:
which docker-compose
sudo rm $(which docker-compose)
Configuration File Cleanup
Remove any remaining Docker configuration from user profiles:
rm -rf ~/.docker/config.json
If the config.json file contains important settings for other tools, edit it to remove only Docker-specific entries like "credsStore": "desktop"
and "currentContext": "desktop-linux"
.
Verification and System Check
Confirming Complete Removal
After completing the uninstallation process, verify that Docker has been completely removed from your system:
sudo systemctl status docker
You should see output indicating that the Docker service could not be found.
Check for any remaining Docker processes:
ps aux | grep docker
Verify that Docker commands are no longer available:
docker --version
docker-compose --version
These commands should return “command not found” errors.
Package Verification
Confirm that no Docker packages remain installed:
dpkg -l | grep -E 'docker'
This command should return no results if the removal was successful.
Network and Port Cleanup
Check that Docker’s network modifications have been properly cleaned up:
sudo iptables -L
Look for any Docker-related rules that might need manual removal.
Troubleshooting Common Issues
Handling Permission Denied Errors
If you encounter permission denied errors during removal, ensure you’re running commands with appropriate privileges. Some stubborn files might require additional steps:
sudo chmod -R 755 /var/lib/docker
sudo rm -rf /var/lib/docker
Dealing with Locked Files
If files appear to be locked or in use, identify processes that might be holding them:
sudo lsof | grep docker
Kill any remaining processes before attempting removal.
Incomplete Package Removal
If packages fail to remove completely, try forcing the removal:
sudo dpkg --remove --force-remove-reinstreq docker-ce
sudo dpkg --purge --force-depends docker-ce
Use these commands with extreme caution, as they can potentially damage system integrity.
Handling Corrupted Package Database
If the package database becomes corrupted during removal:
sudo apt-get update
sudo apt-get -f install
sudo dpkg --configure -a
These commands attempt to repair the package database and complete any interrupted installations.
Post-Uninstallation Best Practices
System Optimization
After successfully removing Docker, optimize your system:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get autoremove
sudo apt-get autoclean
These commands update package lists, upgrade remaining packages, and clean up unnecessary files.
Disk Space Recovery
Check how much disk space was recovered:
df -h
Docker can consume significant disk space, so you should see a noticeable increase in available storage.
Documentation and Learning
Document your uninstallation process for future reference. Note any custom configurations or unusual steps required for your specific setup. This documentation will prove valuable if you need to repeat the process or help others with similar setups.
Preparing for Future Docker Installation
Clean Installation Planning
If you plan to reinstall Docker, consider best practices for a clean installation:
- Choose the appropriate Docker edition (CE vs EE) for your needs
- Plan data storage locations and backup strategies
- Set up proper user permissions from the beginning
- Consider using Docker’s official installation methods for better support
Alternative Container Solutions
If Docker doesn’t meet your needs, consider alternative containerization platforms:
- Podman: A daemonless container engine
- LXC/LXD: System containers for full OS virtualization
- containerd: Lower-level container runtime
- CRI-O: Kubernetes-focused container runtime
Security Considerations
Removing Sensitive Data
Ensure that sensitive data stored in Docker containers or volumes has been properly handled. Even after deletion, data recovery might be possible on traditional hard drives. Consider using secure deletion tools if you handled sensitive information:
sudo shred -vfz -n 3 /var/lib/docker
System Hardening
After Docker removal, review your system’s security posture. Docker’s removal might have left network configurations or firewall rules that should be updated to reflect your current security requirements.