How to Install Docker on Ubuntu 22.04 / Ubuntu 20.04

0

Docker is an open-source containerization platform that helps developers create applications and package them into containers. It provides an efficient way to package applications with their libraries and other dependencies.

As a result, containers are much faster and more efficient than virtual machines because they don’t have to run an entire operating system, just the executable that serves the application.

On the other hand, Docker Engine helps run the pre-packaged containers in an isolated environment.

Here, we will see how to install Docker on Ubuntu 22.04 / Ubuntu 20.04.

Install Docker on Ubuntu 22.04 / 20.04

Remove Old Docker Versions

Before installing Docker, uninstall the older versions of Docker packages called docker or docker-engine along with dependencies from your system. However, Docker data like volumes, images, and networks are preserved even after the uninstallation.

sudo apt remove -y docker docker-engine docker.io containerd runc

Setup Docker Repository

First, install the required packages for Docker installation.

sudo apt update

sudo apt install -y ca-certificates curl gnupg lsb-release

Then, add the Docker’s GPG key to your system.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

And then, add the Docker repository to the system by running the below command.

echo "deb [arch=$(dpkg --print-architecture) 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

Install Docker Engine

After adding the Docker’s repository, update the repository index.

sudo apt update

Then, Install Docker Engine using the apt command.

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
To install a specific version of Docker Engine, list (sudo apt-cache madison docker-ce) the available versions (the second column == <VERSION_STRING>) in the repository and then install with sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin.

After the installation, check the Docker engine version.

docker -v

Output:

Docker version 20.10.14, build a224086

By now, the Docker service should be up and running.

sudo systemctl status docker

Verify Docker Installation

To test the Docker installation, we will run the hello-world container.

sudo docker run hello-world

The below output confirms that the Docker Engine is correctly installed on Ubuntu.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
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/

Allow Non-root users to run Docker Commands

By default, regular users do not have privileges to run Docker commands because the Docker daemon uses a Unix socket owned by the root.

So, to allow regular users to execute Docker commands, add  the user to the docker group.

sudo usermod -aG docker my_user

Then, log out and log in back and run Docker commands without prefixing sudo in the terminal.

docker run hello-world

Docker Basics

1: Top Important Docker Commands – Working with Docker Containers

2: Working with Docker Images – Building Docker Images

3: How to Build Docker Images with DockerFile

Conclusion

That’s All. I hope you have learned how to install Docker on Ubuntu 22.04 / Ubuntu 20.04.

You might also like