To completely uninstall Docker from Ubuntu, removing Docker and related packages from the system is not enough, the user needs to remove containers and image space, Docker configurations, and other files as well.

This blog will provide the steps to completely remove Docker from Ubuntu.

Steps to Completely Uninstall Docker From Ubuntu

Ubuntu is a well-known Debian-based Linux distro that offers many free and open-source packages and Docker is one of them. Docker is a popular containerization tool that encapsulates software and programs along with essential dependencies in small runnable packages(Containers).

However, sometimes, users want to uninstall Docker from the system to free up disk space, switch container runtime, or get the latest Docker environment. Another and more frequent reason is sometimes Docker shows errors and is stuck in unrecoverable conditions.

The Docker uninstallation process is quite tricky. To completely uninstall the Docker(container runtime), go through the following steps.

Step 1: Remove All Containers

First, list down the Docker containers to check all stopped and running containers:

docker ps -a

Here, the “-a” option is used to show all Docker containers:

docker ps -a

Next, stop all executing containers using the below command:

docker stop $(docker ps -a -q)

The given command is a combination of two commands, the first part “docker stop” is used to stop the container, and the second command “docker ps” is used to access the Docker containers. Here, the “-q” option is utilized to access the Docker container by ID:

docker stop $(docker ps -a -q)

Now, remove all Docker containers through “docker rm”:

docker rm $(docker ps -a -q)

Again, this command will first access the containers by ID and then remove them using the “docker rm” command:

docker rm $(docker ps -a -q)

Step 2: Remove Docker Images

Next, remove all Docker images. The below command is also a combination of two commands. The first part “docker rmi” will be used to remove the images and the “docker images” command will be used to access all Docker images by id:

docker rmi $(docker images -a -q)
docker rmi $(docker images -a -q)

Note: The user may get an error, if any image is being used by any Docker container. To avoid such errors, first try to remove Docker containers before removing Docker images. The second solution is to utilize the “-f” option with the “docker rmi” command to forcefully remove the images:

docker rmi -f $(docker images -a -q)

Step 3: Completely Prune the Docker

Now, completely clean the Docker system using the “docker system prune” command. This command will remove all unused and dangling containers, images, and networks. To remove volume as well, utilize the “–volumes” option in the command:

docker system prune -a --volumes

This operation will ask you to confirm the removal process, press “Y” and hit Enter:

docker system prune -a --volumes

This will completely remove all components from Docker.

Step 4: Check Docker Installed Packages

Now, check and list down all installed Docker packages through the mentioned command:

dpkg -l | grep -i docker

Here, you can see currently, 5 packages are installed with Docker name:

dpkg -l | grep -i docker

However, the “containerd.io” package is essential and also installed with Docker installation, but this package is not mentioned in the above list.

Step 5: Uninstall Docker and Other Docker Packages

To uninstall the Docker engine along with other installed packages, run the “apt purge <package-name>” command with “sudo” rights:

sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Here, we are removing the “docker-ce”, “docker-ce-cli”, “containerd.io”, “docker-buildx-plugin”, “docker-compose-plugin” and “docker-ce-rootless-extras” packages from Ubuntu 22.04:

sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Step 6: Remove Docker Directories

Now, remove the Docker directories where Docker data is stored. In the below command, we are removing the “/var/lib/docker” directory where Docker volumes are stored:

sudo rm -rf /var/lib/docker /etc/docker
sudo rm -rf /var/lib/docker /etc/docker

Next, remove the Docker socket data from Ubuntu using the below command:

sudo rm -rf /var/run/docker.sock
sudo rm -rf /var/run/docker.sock

Now, if a user is using “docker-compose”, then remove the Docker compose completely by removing the “/usr/local/bin/docker-compose” directory:

sudo rm -rf /usr/local/bin/docker-compose
sudo rm -rf /usr/local/bin/docker-compose

All Docker data, containers, images, and volume are stored in the “/etc/docker” directory. Now, clear the disk space by removing the “/etc/docker” directory from Ubuntu:

sudo rm -rf /etc/docker
sudo rm -rf /etc/docker

Now, remove the Docker cache by removing the “.docker” hidden directory:

sudo rm -rf ~/.docker
sudo rm -rf ~/.docker

Lastly, remove the containerd completely from the system and all container runtime information using the mentioned command:

sudo rm -rf /var/lib/containerd
sudo rm -rf /var/lib/containerd

Step 7: Delete Docker Group

In the next step, completely delete the Docker from the Ubuntu sudo user group using the “groupdel docker” command. This command requires the root privileges:

sudo groupdel docker
sudo groupdel docker

Step 8: Clean Ubuntu 

Lastly, clean up Ubuntu by removing orphan and unused dependencies and packages from the system through the “apt autoremove” command:

sudo apt autoremove
sudo apt autoremove

This is all about uninstalling Docker from Ubuntu.

Conclusion

To completely uninstall Docker from Ubuntu, first, stop and remove Docker containers. Then, remove the Docker images and lastly prune the Docker system using the “docker system prune -a –volume” command. This will remove all containers, images, networks, and volume. After that, uninstall the Docker completely using the “sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras” command. Then, remove the Docker directories from the system manually and delete the Docker group from Ubuntu using the “sudo groupdel docker” command. We have covered the Docker uninstallation steps in detail in this article.