Skip to main content

Install Docker Engine on Ubuntu

Warning: Do not use apt to install Docker on Ubuntu. Always follow the instructions below to set up the Docker APT repository properly before attempting to install Docker.

Prerequisites

OS requirements

Docker supports the following versions of Ubuntu:

  • Ubuntu Mantic 23.10
  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Focal 20.04 (LTS)

Docker can be installed on 64-bit x86 platforms or ARM platforms. Ubuntu LTS (Long-Term-Support) releases are recommended for production environments as they receive 5 years of security updates.

Uninstall old versions

Older versions of Docker were called docker or docker-engine. If installed, first uninstall them:

$ for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker; do sudo apt-get remove $pkg; done

Moreover, Docker Engine depends on containerd and runc. Uninstall them as well to avoid conflicts:

$ sudo apt-get remove containerd runc

Install using APT repository

Set up Docker's apt repository

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

If you use an Ubuntu derivative distro, such as Linux Mint, you may need to use UBUNTU_CODENAME instead of VERSION_CODENAME.

Install Docker packages

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

Install using the convenience script

Docker provides a convenience script to streamline the installation process for testing and development environments. On Ubuntu, you can use this script and optionally use the --mirror flag to install from a mirror repository:

# $ curl -fsSL test.docker.com -o get-docker.sh
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
# $ sudo sh get-docker.sh --mirror AzureChinaCloud

This script will automatically handle all the prerequisites and install the latest stable version of Docker.

Start Docker

$ sudo systemctl enable docker
$ sudo systemctl start docker

Manage Docker as a non-root user

By default, the docker command uses the Unix socket to communicate with the Docker engine. Only root and users in the docker group can access the Unix socket.

To avoid using root, create a docker group and add your user to it:

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect.

Test Docker installation

$ docker run --rm hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest

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

...

If you see the output above, Docker is installed correctly.

References