Best Way To Install Docker On Ubuntu

Best Way To Install Docker On Ubuntu

Reading time1 min
#Docker#Ubuntu#Linux#Containerization#DevOps#OpenSource

Best Way to Install Docker on Ubuntu: A Practical How-To Guide

Docker has revolutionized the way developers deploy and manage applications through containerization. If you're running Ubuntu and want to leverage Docker’s power for your projects, this guide walks you through the best and cleanest way to install Docker on Ubuntu, ensuring you get the latest version with minimal hassle.

Why Install Docker from Official Repositories?

Ubuntu’s default repositories include Docker packages under names like docker or docker.io. However, these versions often lag behind the latest stable releases and lack some features or bug fixes. For best performance, security updates, and latest features, it's recommended to install Docker from Docker's official repository.


Step-by-Step Guide: Installing Docker on Ubuntu

Prerequisites

  • Ubuntu 18.04 or newer (instructions tested on 20.04 and 22.04)
  • A user account with sudo privileges

Step 1: Update your system

Before starting, always update your package database:

sudo apt update
sudo apt upgrade -y

Step 2: Uninstall older versions (if any)

If you have any older versions of Docker (called docker, docker-engine, or docker.io) installed, remove them to avoid conflicts:

sudo apt remove docker docker-engine docker.io containerd runc

(Note: This does not remove your images or containers.)


Step 3: Install required packages

Install packages that allow apt to use HTTPS repositories:

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

Step 4: Add Docker's official GPG key

This step ensures that downloaded packages are authentic.

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

Step 5: Add the official Docker repository

We add the stable release repo for your Ubuntu version:

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

Step 6: Update package index again

Now that we have added the new repo, refresh package lists:

sudo apt update

Make sure apt uses the Docker repo by checking the candidate version of docker-ce:

apt-cache policy docker-ce

You should see docker-ce listed from the download.docker.com repo with a candidate version.


Step 7: Install Docker Engine

Now install Docker Engine, CLI, and containerd:

sudo apt install docker-ce docker-ce-cli containerd.io -y

Step 8: Verify installation

Confirm that Docker is installed correctly by running:

sudo docker run hello-world

This command downloads a test image and runs it in a container. You should see a friendly welcome message confirming everything works fine.


Optional Configuration Steps

Run Docker commands without sudo (optional)

To avoid typing sudo every time you run docker commands:

sudo usermod -aG docker $USER

Log out and back in again (or reboot) so group changes take effect.

Test without sudo:

docker run hello-world 

If it works without permission errors — you’re good!


Keeping Docker up-to-date

Since we set up the official repo, updating is easy:

sudo apt update && sudo apt upgrade -y docker-ce docker-ce-cli containerd.io

Summary

Installing Docker via its official repository ensures you get the latest features and security patches directly from Docker Inc. The process involves adding their key and repo, then installing with APT – no need to mess with old packages or snaps.

Following this tutorial lets you get up and running quickly with stable, production-ready Docker on Ubuntu. Happy containerizing!


Bonus tip: Basic commands after installation

Here are some handy commands once you have Docker installed:

  • List running containers:

    docker ps
    
  • List all containers (including stopped):

    docker ps -a
    
  • Remove stopped containers:

    docker container prune
    
  • Pull an image manually (e.g., Nginx):

    docker pull nginx:latest
    
  • Run an interactive shell inside a container:

    docker run -it ubuntu bash 
    

Use these as starting points exploring what you can do with your new Docker setup!


If you found this tutorial helpful or have any questions about using Docker on Ubuntu, feel free to leave a comment below!