How to Install Docker on Ubuntu

This tutorial will show you how to install Docker on Ubuntu with step-by-step instructions.

Prerequisites

For this tutorial, you’ll need:

  • An Ubuntu Server or Desktop computer. Most people use Docker on a VPS. You can get a cheap VPS at Linode or check these other Linux VPS hosting providers. This tutorial was tested on Ubuntu 22.04, but it will work on Ubuntu 24.04, Ubuntu 20.04, and other Ubuntu variants.
  • root (sudo) access to your Ubuntu

The default Docker version in the Ubuntu repositories might be older. If you want to install the latest Docker version, follow this tutorial. We won’t be using the Docker version that’s in Ubuntu’s apt repositories by default, we’ll add the latest one.

Step 1: Update Ubuntu

The first step is always to update your Ubuntu system. You can do so by running:

sudo apt update && sudo apt upgrade -y

Step 2: Install the necessary packages

You might have some of these already installed. You can run the command below to install all the necessary packages so you can add additional repos and use https with apt.

sudo apt install curl gnupg coreutils software-properties-common apt-transport-https ca-certificates

Step 3: Install Docker

Next, we need to add the GPG key for the Docker repo with the curl command we installed earlier:

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

Now you can add the Docker repository to apt:

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 > /dev/null

Next, update the apt package list:

sudo apt update

To check and verify if you’re using the new Docker package, not the older one that’s already in Ubuntu, run this command:

apt-cache policy docker-ce

You should get an output similar to this:

Installed: (none)
Candidate: 5:24.0.7-1~ubuntu.22.04~jammy
Version table:
...

Make sure that the “Candidate” version is the latest Docker version.

And now finally, you can run the command to install Docker on Ubuntu:

sudo apt install docker-ce

Docker will now be installed, enabled to start on system boot, and the process will start running after the installation.

You can check if the installation was successful by running this command:

sudo service docker status

If the service is running, that means everything went great. You should get an output similar to this, were it says “active (running)”:

● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-12-07 16:21:43 EET; 4min 40s ago

Step 4: Use Docker

Now that you installed Docker, you can start using it.

If you run this command:

sudo docker

You’ll get a list of all available docker commands you can use. The basic syntax is:

sudo docker [OPTION] [COMMAND] [ARGUMENTS]

To get instructions for a specific command, you can use “–help” at the end of a command. For example, if you need more instructions and help with the “build” command, run:

sudo docker build --help

And finally, download and run the test Docker image to see if everything is working correctly:

sudo docker run hello-world

If everything is correct, the docker command will download the image and run it. You should get an output that says “Hello from Docker!” along with other information.

If you get a message like “Unable to find image ‘hello-world:latest’ locally” in the output, don’t worry, that’s the expected behavior. The image wasn’t available locally, so Docker downloaded it.

And that’s it! You can now start using Docker on Ubuntu. Check out the Docker Hub for official Docker images for various services and apps.

Quick tip if you don’t want to use “sudo” with the docker command

You might get an error like “docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?” when trying to run a docker command.

By default, you can only run the docker command with the root user or with sudo. To not have to do that, run the following command:

sudo usermod -aG docker ${USER}

This command will add your user to the docker group. For the changes to take effect, either log out and log back in or run the following:

su - ${USER}

You’ll need to enter your username and password. And that’s it, you can now run docker commands without sudo.

Leave a comment

Your email address will not be published. Required fields are marked *