How to Install Docker on Ubuntu 22.04

Docker is a popular tool for creating and running containers, which are isolated environments that can run applications and services without affecting the host system. Docker makes it easy to deploy, manage, and scale your applications across different platforms and environments.

In this tutorial, I will explain how to install Docker on Ubuntu 22.04, which is the latest long-term support (LTS) release of Ubuntu. This tutorial is for absolute beginners who want to learn how to use Docker on their Ubuntu system.

By the end of this tutorial, you will learn:

  • How to set up the official Docker repository on Ubuntu 22.04
  • How to install Docker and its dependencies on Ubuntu 22.04
  • How to add your user to the Docker group for easier access
  • How to verify that Docker is working correctly on your system
  • How to update and uninstall Docker if needed

To learn more, check out the Docker tutorials for beginners page.

Prerequisites

Before you start this tutorial, you need:

  • A 64-bit Ubuntu 22.04 system with a user account that has sudo privileges
  • A terminal window open on your Ubuntu system
  • A stable internet connection

Step 1: Set Up the Official Docker Repository

The first step is to set up the official Docker repository on your Ubuntu system. This will allow you to install the latest version of Docker and receive updates from the source.

To do this, you need to add the official Docker GPG key and the repository URL to your system. You can use the following commands in your terminal:

# Add the official Docker GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add the official Docker repository URL
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

The first command downloads and adds the official Docker GPG key to your system. The second command adds the repository URL to your apt sources list. The $(lsb_release -cs) part automatically detects your Ubuntu version and inserts it into the URL.

Step 2: Install Docker and Its Dependencies

The next step is to install Docker and its dependencies on your Ubuntu system. You need to update your apt package index and install some packages that are required for apt to use a repository over HTTPS. You can use the following commands in your terminal:

# Update your apt package index
sudo apt-get update

# Install packages for apt to use a repository over HTTPS
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y

The first command updates your apt package index with the new repository information. The second command installs some packages that are needed for apt to communicate with the HTTPS repository.

Now you can install Docker and its components on your system. You can use the following command in your terminal:

# Install Docker Engine, containerd, and Docker Compose
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

This command installs the following packages on your system:

  • docker-ce: The main package for Docker Engine, which provides the core functionality of Docker
  • docker-ce-cli: The command-line interface for interacting with Docker Engine
  • containerd.io: A container runtime that manages containers independently of Docker Engine
  • docker-buildx-plugin: A plugin that extends the docker build command with new features and capabilities
  • docker-compose-plugin: A plugin that integrates Docker Compose with Docker Engine

You can also install a specific version of Docker if you want, but this is not recommended for beginners. To see a list of available versions, you can use this command:

# List the available versions of docker-ce
apt-cache madison docker-ce | awk '{ print $3 }'

This command shows you all the versions of docker-ce that are available in the repository. You can then choose one of them and replace it in the previous installation command.

Step 3: Add Your User to the Docker Group

By default, only users with root privileges or sudo access can run Docker commands on Ubuntu. This can be inconvenient and insecure if you want to use Docker frequently.

To avoid this, you can add your user account to the docker group, which grants you permission to run Docker commands without sudo. You can use the following command in your terminal:

# Add your user account to the docker group
sudo usermod -aG docker $USER

This command adds your current user account (represented by $USER) to the docker group. You need to log out and log back in for this change to take effect.

Step 4: Verify That Docker Is Working Correctly

To verify that Docker is working correctly on your system, you can run a simple test container using the hello-world image. You can use the following command in your terminal:

# Run a test container using hello-world image
docker run hello-world

This command pulls the hello-world image from Docker Hub (the official repository of images) and runs it as a container on your system. You should see a message like this in your terminal:

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

This message confirms that your installation is working correctly and that you can run containers on your system.

Step 5: Update and Uninstall Docker If Needed

If you want to update or uninstall Docker on your Ubuntu system, you can use the apt package manager as usual. To update Docker, you can use these commands:

# Update your apt package index
sudo apt-get update

# Update all installed packages including docker-ce and its components
sudo apt-get upgrade -y

These commands update your apt package index and upgrade all installed packages including docker-ce and its components.

To uninstall Docker, you can use these commands:

# Uninstall docker-ce and its components but keep configuration files and data
sudo apt-get remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

# Uninstall docker-ce and its components and remove configuration files and data
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

# Remove any unused packages including dependencies of docker-ce and its components
sudo apt-get autoremove -y

# Remove any images, containers, volumes, or networks stored in /var/lib/docker/
sudo rm -rf /var/lib/docker/

These commands uninstall docker-ce and its components from your system using different options:

  • The remove option uninstalls the packages but keeps their configuration files and data in case you want to reinstall them later.
  • The purge option uninstalls the packages and removes their configuration files and data completely.
  • The autoremove option removes any unused packages including dependencies of docker-ce and its components.
  • The last command removes any images, containers, volumes, or networks stored in /var/lib/docker/, which are not automatically removed when you uninstall docker-ce.

Conclusion

In this tutorial, I have explained how to install Docker on Ubuntu 22.04 using the official repository. I have also shown you how to add your user to the docker group, how to verify that Docker is working correctly, and how to update or uninstall Docker if needed.

Here are some possible key takeaways from this tutorial:

  • You can install Docker on Ubuntu 22.04 using the official repository, which gives you access to the latest version and updates of Docker.
  • You can add your user to the docker group to run Docker commands without sudo, which makes it easier and more secure to use Docker on your system.
  • You can verify that Docker is working correctly by running a test container using the hello-world image, which shows you a message from Docker.
  • You can update or uninstall Docker using the apt package manager, which allows you to manage Docker and its components on your system.

I hope you found this tutorial helpful and informative. If you have any questions or feedback, please feel free to leave a comment below.