Table of Contents

Docker - Install Docker

Prerequisites

ALERT: Docker is only compatible with iptables and ip6tables.


Uninstall old versions

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt remove $pkg; done

NOTE: Images, containers, volumes, and networks stored in /var/lib/docker/ are not automatically removed when you uninstall Docker.

To delete all images, containers, and volumes:

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

Remove source list and keyrings

sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.asc

NOTE: Any edited configuration files will need to be deleted manually.


Set up the Docker apt repository

# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
 
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

Install the Docker packages

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

NOTE:

  • docker-ce - The Docker engine itself.
  • docker-ce-cli - A command line tool that lets you talk to the Docker daemon.
  • containerd.io - A container runtime that manages the container’s lifecycle.
  • docker-buildx-plugin - This extension for Docker enhances the capabilities of building images, mainly focusing on multi-platform builds.
  • docker-compose-plugin - A configuration management plugin that helps manage multi-container Docker applications using a single YAML file.

Verify that the installation is successful

sudo docker run hello-world

returns:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete 
Digest: sha256:0b6a027b5cf322f09f6706c754e086a232ec1ddba835c8a15c6cb74ef0d43c29
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

NOTE: This downloads a test image and runs it in a container.

  • When the container runs, it prints a confirmation message and exits.
  • If the command fails to run, try to restart the docker service, before rerunning this test:
    sudo systemctl restart docker.service

Uninstall Docker Engine

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

Then, manually remove the following two directories:

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

References

https://docs.docker.com/engine/install/ubuntu/

https://docs.docker.com/engine/install/ubuntu/#uninstall-docker-engine

https://docs.docker.com/engine/network/packet-filtering-firewalls/