Ubuntu

Uninstall the Docker Software and All Its Containers on Ubuntu 22.04

Docker is a renowned tool that the developers use to help with the easier deployment and management of software applications. With Docker, you can utilize the containers to create an isolated environment for any application when deploying it to enhance the host system’s efficiency and better resource utilization.

Thanks to Docker, the developers can build, test, and deploy the software applications in any environment. You can think of Docker as a tool that works like a VM, only that it doesn’t stress the host system’s resources. Sometimes, you may wish to uninstall Docker on your Ubuntu 22.04. In such a case, you need a safe method to uninstall Docker and all its containers. We will cover a step-by-step process to cleanly uninstall Docker on Ubuntu 22.04.

Uninstall Docker on Ubuntu 22.04

We will uninstall Docker and its containers in different steps.

1. Confirm the Docker Packages

The first step is to list all the available Docker packages on your Ubuntu 22.04. You can use the dpkg command to list the installed packages or check the Docker version:

$ dpkg -l | grep -I docker

2. Delete the Docker Images

Deleting the Docker packages doesn’t delete the images that you created. Start by checking the available docker images with the following command:

$ docker images

Delete the available images by executing the following command:

$ docker rmi $(docker images -q)

The command that we executed removes all the images to clear the space that is previously occupied by the Docker images on your system.

If we try listing the images again, no Docker image is available on the system. This means that we managed to remove them all.

3. Delete the Docker Containers

When working with Docker, you must create different containers. We also need to delete them before uninstalling the Docker package. List the available containers with the ps -a command.

$ docker ps -a

You can delete each container independently, but that would take time, especially if you have multiple containers. A better approach is to delete all the available containers using the rm command.

$ docker rm $(docker ps -aq)

All the container IDs of the deleted containers are displayed after you execute the rm command. Verify that the containers are deleted by checking the available containers.

4. Delete the Docker Volumes

If you also have the Docker volumes that you created, the rm command will help you delete them. List the available volumes. Then, specify the names of the volumes that you want to delete, as presented in the following:

$ docker volume ls
$ docker volume rm <volume-name>

Your Docker volumes are deleted, and the space is available to the host system.

5. Delete the Docker Networks

Check if you have any networks that you created using the ls command. Here, we have the linuxhint network. To delete the network, use the rm command.

$ docker network ls
$ docker network rm linuxhint

Alternatively, you can delete all the networks using the prune command. Here’s an example where we delete the “neww” network:

$ docker network prune

At this point, you are ready to remove the Docker package from your Ubuntu 22.04.

6. Uninstall the Docker Package

After identifying the Docker packages on your system, delete them by running the following command:

$ sudo apt-get purge -y docker.io

Here, we remove the docker.io package. Note that we opted to delete the Docker packages last since uninstalling the Docker packages won’t remove your images, containers, volumes, etc.

Note that the Docker directory is not removed even after uninstalling the package.

At the last line, you will notice the message that your /etc/docker and /var/lib/docker/ directories are still available on your system.

We can verify that the Docker files still exist on our system.

Remove these directories from your system with the following command:

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

Also, delete the Docker group that you created when installing the Docker.

$ sudo groupdel docker

Lastly, remove the Docker socket from your system.

$ sudo rm -rf /var/lib/docker.sock

Conclusion

Docker is great for deploying and managing applications. However, when you don’t need to have the Docker in your system, you can uninstall it and all the created containers, images, networks, etc. This guide presented all the steps that you should follow to uninstall the Docker software and all its containers on Ubuntu 22.04.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.