How to install Docker in Ubuntu 22.04 LTS

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install Docker in Ubuntu 22.04.

Docker allows you to self host multiple apps at the same time in your server, with no issues as all these apps are in isolated containers. So you could use one server, to self host as many apps as you want.

Docker can be easily installed in Ubuntu 22.04, as docker provides its own repository that allows Ubuntu users to install docker from the terminal.

Install Docker in Ubuntu 22.04

In this tutorial, we'll be installing Docker in Ubuntu 22.04, via the official repository.

Step 1 - Install required packages

In order to be able to download and install Docker in your system, you need to install the required packages.

The required packages are ca-certificates, curl and gnupg

ca-certificates - the certificates package that is used to verify the authenticity of the SSL connections.

curl - the package that will be used to download Docker's GPG.

gnupg - the tool that allows you to verify Docker's GPG Key.

To do so, simply run the command below.

sudo apt-get install ca-certificates curl gnupg

Step 2 - Download Docker GPG Key

Next, proceed to Download Docker's GPG key. Downloading the GPG key is quite easy, and it can be done via three easy steps.

1) First set the correct permissions, by running the following.

sudo install -m 0755 -d /etc/apt/keyrings

2) Download Docker's GPG Key.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

3) Grant read permissions to Docker's GPG for all users.

sudo chmod a+r /etc/apt/keyrings/docker.gpg

Step 3 - Setup Docker Repository

Now that you have downloaded Docker GPG Key on your system, you can now proceed to create the repository so that you can download Docker.

Docker's repository can be easily created in your system, all you have to do is run the command below to do so.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4 - Update your system

Docker's repository should be added in your system now. However, in order to use docker repository, you must update the package index.

To do so, simply issue the following command.

sudo apt update

Step 5 - Install Docker

Now you should be able to install Docker, by running the command below.

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

Step 6 - Verify Docker Installation

Docker is now installed in your machine. To verify that Docker is working, you can run the hello-world image, or simply check Docker version.

To run the hello-world image, simply run the command below.

sudo docker run hello-world

To check Docker version, run the following.

docker --version

Uninstall Docker in Ubuntu 22.04

If you no longer need Docker, then you can easily remove it from your system.

1) To uninstall Docker, simply run the command below.

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

2) Once docker and docker related packages are removed, proceed to remove Docker GPG key.

You can use rm to delete the Docker GPG key that you downloaded.

sudo rm /etc/apt/keyrings/docker.gpg

3) Finally, remove Docker repository.

Docker repository can be removed also via the rm comand.

To do so, simply run the following.

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

Conclusion

In this tutorial, you learned how to install and uninstall docker in Ubuntu 22.04. In this tutorial, we used the official Docker repository and GPG key.

This tutorial should also apply to any Debian based distro.