How to Install and Use Docker in Ubuntu 22.04

Install Docker Engine on Ubuntu 22.04: add the official Docker repo, install docker-ce and the Compose plugin, run without sudo, and uninstall when needed.

Docker is a containerization platform that uses OS-level virtualization to run applications in isolated containers. Each container bundles its own software, libraries, and config and can talk to other containers over well-defined interfaces. Docker lets you build and run the same stack on your laptop, CI, and production.

In this guide we walk through installing Docker Engine on Ubuntu 22.04: from the official Docker repository, from a .deb package, or via the convenience script. We also cover running docker without sudo and removing Docker when it’s no longer needed.

Prerequisites

  • Ubuntu 22.04 (Jammy) server or desktop with sudo access.
  • Internet access to download packages and images.

Update system packages

Update the package index and upgrade installed packages:

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.

Install packages required for HTTPS repos and the Docker GPG key:

1
2
3
4
5
6
sudo apt-get install -y \
    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

Add the Docker stable repository for your architecture and Ubuntu release:

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 package index and install Docker Engine, CLI, containerd, and the Docker Compose plugin:

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

Optional: install a specific version

If you need a specific Docker version, list available versions:

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

Install that version using the string from the second column (e.g. 5:20.10.17~3-0~ubuntu-jammy):

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

Example:

1
sudo apt-get install -y docker-ce=5:20.10.17~3-0~ubuntu-jammy docker-ce-cli=5:20.10.17~3-0~ubuntu-jammy 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.

Download the .deb from download.docker.com: pick your Ubuntu version, then pool/stable/, then amd64, armhf, or arm64. Install the package (replace with your path):

1
sudo dpkg -i /path/to/docker-ce_*.deb

The Docker daemon should start automatically. Fix any missing dependencies with sudo apt-get install -f if needed.

Install using the convenience script

For a quick install (e.g. dev or CI), use Docker’s script (review it before running):

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

This installs the latest stable Docker Engine. Add your user to the docker group if you want to run docker without sudo (see below).

Verify installation

Docker runs as a daemon after install. Check its status:

 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: [{unix:///run/containerd/containerd.sock ...}]" 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[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"

Active: active (running) means the Docker daemon is running. Test it with the hello-world image:

1
sudo docker run hello-world

Example output:

 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/

Run Docker without sudo

By default only root (or users in the docker group) can run docker. If you run docker ps as a normal user you may see:

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

To run docker without sudo, add your user to the docker group:

1
sudo usermod -aG docker ${USER}

Apply the new group: log out and back in, or run:

1
newgrp docker

(or su - ${USER} and enter your password). Confirm:

1
id -nG

You should see docker in the list (e.g. citizix docker). Then you can run Docker without sudo:

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
/ #

Uninstalling Docker

To remove Docker Engine, CLI, and Containerd:

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

Images, containers, volumes, and custom config under /var/lib/docker and /var/lib/containerd are not removed by purge. To remove all Docker data:

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

Remove any custom config files (e.g. under /etc/docker) manually if needed.

Summary

You installed Docker Engine on Ubuntu 22.04 using the official Docker repository (recommended), a .deb package, or the convenience script. You verified the daemon with systemctl status docker and docker run hello-world, added your user to the docker group to run Docker without sudo, and saw how to uninstall Docker and clean up data. The Docker Compose plugin (docker compose) is installed with the repo method; for Compose examples see Getting started with Docker Compose.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy