How to Install Docker on Ubuntu 22.04


Docker is a containerization technology that acts as a complete workspace (having all prerequisites) for the packages/applications. A Docker container contains a package/app along with all its relevant dependencies and configurations in one place. This makes Docker containers more portable and robust than virtual machines. Because of its portability and flexibility, Docker is quite famous among Ubuntu (and other Linux distros) users.

In Ubuntu, developers have to deal with the client/server architectures. For instance, an application having the client/server model may require two containers to work smoothly, one for the server and the other for the client. Docker is also the key player in such a scenario, as it integrates the client and server-side packages/dependencies on different containers. All in all, Docker has a key role in Ubuntu 22.04 for advanced users.

Today, we will enlist the possible methods to install Docker on Ubuntu 22.04, with the following outline:

 

Difference Between docker-ce, docker.io, and containerd

While installing Docker from this guide, you might come across various Docker-named packages. Let’s first explain/define them to avoid any confusion:

  • docker-ce/docker-ce-cli: The “docker-ce” is the Community Edition (CE) of Docker available on the official repository of Docker. While “docker-ce-cli” refers to the command line support of Docker CE.
  • docker.io/docker: The docker on the default repository of Ubuntu 22.04 is named “docker.io”. Similarly, the “docker” package name is used by the default repository of Fedora.
  • containerd/containerd.io: A service that starts/stops/destroys Docker containers. The containerd is also a Docker container which manages/overlooks the life cycle of the container.

 

How to Install Docker on Ubuntu 22.04?

Docker support is available on the default repositories of Ubuntu 22.04 and Docker’s repository. Apart from these, the basic usage of Docker is available on the Snap Store as well. Let’s first explore Docker’s repository method:

Using the Default Repositories of Ubuntu 22.04

Docker is available with the name “docker.io” on the official repositories of Ubuntu. To install it, update the apt packages list first:

sudo apt update

Now, use the following command to install docker.io:

sudo apt install docker.io

To verify the installation, check the version of the newly installed Docker:

docker --version

Important: After the successful installation, the docker services are still to be managed to run the docker commands on Ubuntu 22.04, which are demonstrated in the Following Section.

 

Configure the Docker on Ubuntu 22.04 | To Be Applied After Each Installation

The docker service also needs to be managed before you start using the Docker on Ubuntu 22.04. A root user, the sudo rights user, or the user in a docker group can manage the Docker service. Let’s do all these:

Create Docker Group

sudo groupadd docker

Add the Current User to the Docker Group

sudo usermod -aG docker $USER

You can add any user by specifying the username in the above command.

Status of the Docker Service

sudo systemctl status docker

Docker service is stopped/dead/inactive.

Start the Service

sudo systemctl start docker

Verify the status

sudo systemctl status docker

The Docker services are running in an active state which assures that the Docker is ready to use. Moreover, the docker services are already enabled to keep the service in an active state after each startup. However, if the service is not enabled in your case, do it using the command:

sudo systemctl enable docker

After all the above steps, reboot your system to keep using Docker.

Using the Docker’s Repository

The Docker’s repository method requires a few dependencies to be integrated into the system before the installation is carried out. Let’s install them first using the below command:

Step 1: Install the Prerequisites

The subsequent installation steps make use of a few utilities to install Docker. First, install those prerequisites via the command:

  • apt-transport-https: Necessary to fetch the packages safely and securely over HTTPS protocols.
  • ca-certificates: This certificate is used to authenticate the SSL connections (through which your system is interacting for some package downloading purpose).
  • curl: A tool that communicates with the web/application server to send/receive data through the relevant URL.
  • software-properties-common: This package includes various scripts that ease the way of adding/removing repositories (nullifies the purpose of adding/removing repositories manually).
sudo apt install apt-transport-https ca-certificates curl software-properties-common

 

Step 2: Add the GPG Key and Docker’s Repository

Add the GPG key so that the system will recognize the Docker’s repository communicated through a trusted source:

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

Note: The GPG key is added in the “/etc/apt/keyrings” directory. If the directory is not available on your system, then you need to create it with suitable permissions using the command:

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

Now, add the Docker’s repository to the apt sources list using the command:

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

After adding the repository, update the apt packages list to embed the docker from the newly added repository:

sudo apt update

Step 3: Install the Docker

Once all is done, use the apt install command to install the docker along with other required packages:

sudo apt install docker-ce docker-ce-cli containerd.io

Bonus Tip: Embed the Docker-Compose Support

You can also install the Docker compose plugin to extend the functionality of a simple Docker engine to manage applications having multiple containers:

sudo apt install docker-compose-plugin

To verify the installation, check the version of Docker:

docker --version

The installed version (24.0.7) is newer than that installed (24.0.5) using the default repositories.

Important: Before using docker, configure the docker service(s) by following the Method described above.

Using Snap

Snap store has the CLI and GUI support available on Ubuntu 22.04. Thus, the Docker package can be installed using the snap CLI and GUI. Let’s understand how:

  • Using the Snap CLI

First, enable the snap daemon on Ubuntu 22.04 and get the dependencies/configurations for the available snaps:

sudo apt install snapd && sudo snap install core

Now, use the below command to install the Docker snap:

sudo snap install docker

Important: Configure/set-up Docker by following the method described above.

 

  • Using the Snap GUI

Search for Docker in the Ubuntu Software Center and click on “Docker” from the search results:

Next, the Install button appears. Click on it to proceed with the application process:

The user password will be required to proceed with the installation:

Upon successful authentication, Docker will be installed soon.

Note: Once the installation is completed, configure the Docker by following the Configuration Method.

 

How to Completely Uninstall Docker From Ubuntu 22.04?

As there are multiple methods to install Docker, each installation method follows a specific removal method.

Apt Method | Applicable on Default Repositories and Docker Repository Installations

If you have installed Docker from the default Ubuntu repositories (docker.io), then use the below command to uninstall it:

sudo apt autoremove docker-ce docker-ce-cli containerd.io docker.io

Remove the GPG Key and Docker’s Repository

If you want to remove the docker’s repository too, use the command:

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

Similarly, you can remove the GPG key that was added for the authentication of the Docker’s repository:

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

Snap Method

Use the below command to remove the docker snap:

sudo snap remove docker

 

Docker Commands Cheat Sheet

In Ubuntu 22.04 (or any other system), Docker strongly relies on command-line support. Even the Docker Desktop functionality is also backed up by the docker-cli and thus docker-cli is the primary prerequisite to get the Docker Desktop support. Here, we have listed some useful Docker commands to get good hands on the Docker:

Docker Commands Cheat Sheet
Commands/ Group Commands Purpose
PULL/PUSH Docker Images docker login -u [username] Login into Docker
docker search [image-name] Search for An Image in Docker HUB*
docker pull [image-name] Pull an image from Docker HUB
docker push [username]/[image-name] Push/Upload an image to Docker HUB
Managing the Docker Images docker build -t [image-name] Build the image from the Docker File*
docker images List Local Docker Images
docker rmi [image-name] Removing a Docker Image
docker image prune Remove All Unused Docker Images
Managing the Docker Containers docker run –name [container-name] [image-name] Run a Specific Container of a Specific Docker Image
docker run -d [image-name] Run an Image in the Background
docker start/stop [container-name]/ [container-id] Start or Stop a Docker Container
docker rm [container-name] Remove a Stopped Container
docker exec -it [container-name] sh Opens up a Shell Inside a Container
docker logs -f [container-name] Fetch the Logs of a Container
docker ps List the Currently Running Containers
docker ps –all List All the Containers (Running/Stopped)
  • Docker HUB: A centralized registry where the images are placed and later searched/pulled.
  • Docker File: This is a script that contains the instructions (in the form of lines of code) to load the preliminary image, the application to be run in that image, and how the application will run.

 

Bottom Line

Docker has been a key contributor to containerized development, through its docker-cli and docker desktop support. Both these interfaces are available on Ubuntu 22.04. This post has described the methods to install docker on Ubuntu 22.04 (CLI and Desktop).

Docker is available on the official repositories of Ubuntu, docker’s repository, and the snap support. The docker’s default repository offers the latest version whereas the Ubuntu repositories or the snap, have a bit older versions, but are tested well by the contributors.

Print Friendly, PDF & Email
Categories