How to Install Docker on Ubuntu 22.04, 20.04

As you may know, Docker allows you to pack, ship and run any application as a lightweight container. It’s like a virtual machine, only more portable and resources-efficient. This tutorial is going to show you how to install Docker on Ubuntu.

Requirements of Installing Docker on Ubuntu

You must be using a 64 bits OS because Docker doesn’t support 32 bits.

Install Docker from Ubuntu Repository

Docker is included in Ubuntu software repository. We can install the Docker runtime by executing the following command in terminal. This works on any current Ubuntu versions, including Ubuntu 16.04, Ubuntu 18.04, Ubuntu 19.10, Ubuntu 20.04.

sudo apt install docker.io

install docker on ubuntu from ubuntu repository

During the installation, a docker group and a Systemd service will be created. You can check the systemd service with:

systemctl status containerd

Sample output:

 containerd.service - containerd container runtime
     Loaded: loaded (/lib/systemd/system/containerd.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-21 09:32:38 HKT; 1min 45s ago
       Docs: https://containerd.io
   Main PID: 2035184 (containerd)
      Tasks: 11
     Memory: 22.2M
     CGroup: /system.slice/containerd.service
             └─2035184 /usr/bin/containerd

Install Docker on Ubuntu from Docker’s APT Repository

The upstream Docker repository currently supports Ubuntu 16.04, Ubuntu 18.04 and Ubuntu 19.10.

To ensure that we have the latest and greatest version, we will have to install it from Docker’s APT repository. Run the following command to add Docker repository to your Ubuntu system.

echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list

Next, run the following command to import the Docker GPG key to Ubuntu system so that APT can verify package integrity during installation.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

And because this repository uses HTTPS connection, which I recommend all software repositories should be using, we also need to install apt-transport-https and ca-certificates package.

sudo apt install apt-transport-https ca-certificates

Finally, update the package index on your Ubuntu system and install docker-ce (Docker Community Edition).

sudo apt update

sudo apt install docker-ce

Some Simple Commands You May Want to Run After Installing Docker

Once Docker is installed, the Docker daemon should be automatically started. You can check its status with:

systemctl status docker

systemctl status docker

If it’s not running, then start the daemon with this command:

sudo systemctl start docker

And enable autostart at boot time:

sudo systemctl enable docker

Check Docker version.

docker -v

Sample output:

Docker version 20.10.8, build 3967b7d

Display system-wide information regarding the Docker installation.

sudo docker info

Output:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.8
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e25210fe30a0a703442421b0f60afac609f950a3
 runc version: v1.0.1-0-g4144b63
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.11.0-22-generic
 Operating System: Ubuntu 20.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 10
 Total Memory: 58.88GiB
 Name: focal
 ID: C7AR:BNAO:ASNW:W2FT:PA3E:RXQL:GLYY:EHNI:LRTK:6LPC:CM7R:MIFR
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Verify Docker is installed correctly.

sudo docker run hello-world

You should see the following message indicating that Docker is working correctly.

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

sudo docker run hello-world

Install a New Linux Kernel

If you take a look at the Docker logs,

sudo journalctl -eu docker

You might see the following warning

level=warning msg="Your kernel does not support swap memory limit"
level=warning msg="Your kernel does not support CPU realtime scheduler"
level=warning msg="Your kernel does not support cgroup blkio weight"
level=warning msg="Your kernel does not support cgroup blkio weight_device"

You can install a new version of the Linux kernel.

Ubuntu 20.04

sudo apt install linux-image-generic-hwe-20.04-edge

Ubuntu 18.04

sudo apt install linux-image-generic-hwe-18.04-edge

Then restart your Ubuntu server.

sudo shutdown -r now

Troubleshooting

If docker.service can’t start, and you find the following error in the journal logs (sudo journalctl -eu docker.service),

failed to load listeners: no sockets found via socket activation

Then it’s because docker.socket failed to start. You can check it status with:

sudo systemctl status docker.socket

Check logs with:

sudo journalctl -eu docker.socket

How to Remove Docker

If you don’t want to use Docker anymore, here’s how to remove it completely.

Stop all containers.

sudo docker stop $(docker ps -a -q)

Remove all containers.

sudo docker rm $(docker ps -a -q)

Stop the docker systemd service.

sudo systemctl stop docker.service docker.socket

Purge the Docker package.

sudo apt purge docker.io

or

sudo apt purge docker-ce

The /var/lib/docker/ directory will be removed during the process.

Wrapping Up

I hope this tutorial helped you install Docker on Ubuntu. As always, if you found this post useful, then subscribe to our free newsletter.

Rate this tutorial
[Total: 33 Average: 4.8]

2 Responses to “How to Install Docker on Ubuntu 22.04, 20.04

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here