How to Install and Use Docker in Ubuntu 22.04

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

It is s an open source containerization platform. It enables developers to package applications into containers - standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

In this guide we are going to explore various options to install docker in OpenSUSE Leap15.

Also check

# Ensure your system packages are up to date

Before proceeding, let us ensure your packages are updated. Use these commands top achieve this:

1
2
sudo apt update
sudo apt upgrade -y

# Uninstall previous versions if any

Packages providing older versions of Docker are docker, docker.io, or docker-engine. Let us use this command to ensure that these packages do not exist in our system:

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

It’s OK if apt-get reports that none of these packages are installed.

# Installing docker

You can install Docker Engine in different ways, depending on your needs:

  • You can set up docker repositories and install docker using apt
  • You can download the DEB packages and install manually
  • You can choose to use automated convenience scripts to install Docker

# Install docker engine using the repository

On a new host, the docker repositories do not exist. For the first time on a new host machine, you need to set up the Docker repository. Afterwards, you can install and update Docker from the repository.

Let us install the following packages to allow apt to use a repository over HTTPS:

1
2
3
4
5
6
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Docker’s official GPG key:

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

Use the following command to set up the stable repository.

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

# Install Docker Engine

Update the apt package index since we added a new repository:

1
sudo apt-get update

Now let’s install the latest version of Docker Engine and containerd usi

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

# Optional: Installing a specific version

If you have requirements that nececitate a specific version, you can do so.

To install a specific version: List the versions available in your repo:

1
2
3
4
5
6
7
$ apt-cache madison docker-ce

 docker-ce | 5:20.10.17~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.16~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.15~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.14~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
 docker-ce | 5:20.10.13~3-0~ubuntu-jammy | https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

Now you can Install a specific version using the version string from the second column, for example, 5:20.10.17~3-0~ubuntu-jammy.

1
sudo apt-get install docker-ce=<VERSION_STRING></VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

Example:

1
sudo apt-get install docker-ce=5:20.10.17~3-0 docker-ce-cli=5:20.10.17~3-0 containerd.io

# Install docker engine from a package

If you cannot use Docker’s repository to install Docker Engine, you can download the .deb file for your release and install it manually. You need to download a new file each time you want to upgrade Docker.

Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version, then browse to pool/stable/, choose amd64, armhf, or arm64, and download the .deb file for the Docker Engine version you want to install.

Install Docker Engine, changing the path below to the path where you downloaded the Docker package.

1
sudo dpkg -i /path/to/package.deb

The Docker daemon starts automatically.

Verify that Docker Engine is installed correctly by running the hello-world image.

1
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

# Install using the convenience script

Docker provides a convenience script at get.docker.com to install Docker into development environments quickly and non-interactively.

This example downloads the script from get.docker.com and runs it to install the latest stable release of Docker on Linux:

1
2
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Verify installation

Upon installation, docker will run as a daemon. To check that docker is running, we use the systemctl status docker command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ sudo systemctl status docker

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-06-09 15:03:27 UTC; 2min 9s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 2916017 (dockerd)
      Tasks: 8
     Memory: 29.5M
        CPU: 299ms
     CGroup: /system.slice/docker.service
            /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 09 15:03:26 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:26.310732608Z" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
Jun 09 15:03:26 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:26.310915568Z" level=info msg="ccResolverWrapper: sending update to cc: {&#91;{unix:///run/containerd/containerd.sock  <nil> 0 <nil>}] <nil> <nil>}" module=grpc
Jun 09 15:03:26 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:26.311177606Z" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Jun 09 15:03:26 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:26.387947667Z" level=info msg="Loading containers: start."
Jun 09 15:03:26 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:26.915560343Z" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Jun 09 15:03:27 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:27.107135355Z" level=info msg="Loading containers: done."
Jun 09 15:03:27 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:27.135968649Z" level=info msg="Docker daemon" commit=a89b842 graphdriver(s)=overlay2 version=20.10.17
Jun 09 15:03:27 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:27.136646239Z" level=info msg="Daemon has completed initialization"
Jun 09 15:03:27 ubuntusrv.citizix.com systemd&#91;1]: Started Docker Application Container Engine.
Jun 09 15:03:27 ubuntusrv.citizix.com dockerd[1]: time="2022-06-09T15:03:27.182889940Z" level=info msg="API listen on /run/docker.sock"

The Active: active (running) shows that docker was started successfully.

We can run a hello-world image to test that docker works as expected:

1
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

I got this output, showing that it works fine:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ sudo docker run hello-world

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

# Executing the Docker Command Without Sudo

By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:

1
2
3
$ docker ps

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

1
sudo usermod -aG docker ${USER}

To apply the new group membership, log out of the server and back in, or type the following:

1
su - ${USER}

You will be prompted to enter your user’s password to continue.

Confirm that your user is now added to the docker group by typing:

1
id -nG

I see this, confirming that i now belong to the docker group

1
2
$ id -nG
citizix docker

Now I can do docker: I can run alpine image

1
2
3
4
5
6
7
8
$ docker run --rm -it --name alpine --entrypoint /bin/sh alpine:3

Unable to find image 'alpine:3' locally
3: Pulling from library/alpine
2408cc74d12b: Pull complete
Digest: sha256:686d8c9dfa6f3ccfc8230bc3178d23f84eeaf7e457f36f271ab1acc53015037c
Status: Downloaded newer image for alpine:3
/ #

We have successfully installed docker in our system!

# Uninstalling docker when it’s longer needed

To complately erase docker engine from the system, we need to uninstall the Docker Engine, CLI, and Containerd packages:

1
sudo apt-get purge docker-ce docker-ce-cli containerd.io -y

Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:

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

You must delete any edited configuration files manually.

# Conclusion

In this guide we managed to install docker on out Ubuntu 22.04 system.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy