Remove docker from ubuntu
The docker command provides several options for managing Docker containers and images. One of these options is the system prune
command, which can be used to remove unused data from the Docker system. This command can be used to “nuke” everything Docker, by removing all unused containers, images, networks, and volumes. Later you can safely remove docker from the system.
To nuke everything Docker, you can use the following script:
1# Stop and remove all running containers
2docker stop $(docker ps -aq)
3docker rm $(docker ps -aq)
4
5# Remove all unused images
6docker rmi $(docker images -q)
7
8# Remove all unused networks
9docker network prune -f
10
11# Remove all unused volumes
12docker volume prune -f
This code uses stops and removes all running containers, removes all unused images, removes all unused networks, and removes all unused volumes. The -f
option is used with the network prune
and volume prune
commands to force the removal of the unused data.
Note that using the system prune
command or the script above will remove all unused data from the Docker system. This can free up a significant amount of disk space, but it can also remove data that you may want to keep. Therefore, it is recommended to use this command with caution and only when you are sure that you want to remove all unused data from the Docker system.
Now to remove docker from ubuntu, you can use the following commands:
1sudo apt-get purge -y docker-engine docker docker.io docker-ce;
2sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce;
3sudo rm -rf /var/lib/docker sudo rm /etc/apparmor.d/docker sudo groupdel docker sudo rm -rf /var/run/docker.sock;
All of these can be written in a simple nice bash script for automation.
1#!/bin/bash
2
3## Stop and remove all containers
4echo "Removing containers :"
5if [ -n "$(docker container ls -aq)" ]; then
6
7 docker container stop $(docker container ls -aq);
8
9 docker container rm $(docker container ls -aq);
10fi;
11
12## Remove all images
13echo "Removing images :"
14if [ -n "$(docker images -aq)" ]; then
15
16 docker rmi -f $(docker images -aq);
17fi;
18
19## Remove all volumes
20echo "Removing volumes :"
21if [ -n "$(docker volume ls -q)" ]; then
22
23 docker volume rm $(docker volume ls -q);
24fi;
25
26## Remove all networks
27echo "Removing networks :"
28# Skip default networks : bridge, host, none
29
30if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}')" ]; then
31
32 docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}');
33fi;
34
35
36## Remove docker engine and clean
37echo "Uninstall and clean docker :"
38
39if [ -n "$(dpkg -l | grep -i docker )" ]; then
40
41 sudo apt-get purge -y docker-engine docker docker.io docker-ce;
42 sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce;
43 sudo rm -rf /var/lib/docker sudo rm /etc/apparmor.d/docker sudo groupdel docker sudo rm -rf /var/run/docker.sock;
44fi;
45
46echo "Done :"