Docker is a platform that enables developers to build, deploy, and manage applications in containers. Containers are lightweight, portable, and ensure that software runs consistently regardless of the environment. This tutorial will guide you through the steps to install Docker on an Ubuntu Linux system.

Prerequisites

  • A system running Ubuntu Linux (20.04 LTS or later recommended).
  • A user account with sudo privileges.

Step 1: Update Your System

Before installing Docker, it's a good practice to update the package index and upgrade installed packages to the latest version.

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Packages

Docker requires certain packages to be installed on your system. These include apt-transport-https, ca-certificates, curl, gnupg, and lsb-release.

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

Step 3: Add Docker's Official GPG Key

Add Docker's official GPG key to ensure the 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 4: Set Up the Docker Repository

Add Docker's official repository to your system's package sources.

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 5: Install Docker Engine

Update the package index again and install the latest version of Docker Engine, along with Docker CLI and Docker Compose.

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

Step 6: Verify Docker Installation

Once the installation is complete, verify that Docker is installed correctly by running the following command:

sudo docker --version

You should see the version number of Docker displayed.

Step 7: Run Docker as a Non-root User (Optional)

By default, Docker requires root privileges. To avoid using sudo with Docker commands, add your user to the docker group.

sudo usermod -aG docker $USER

After adding your user to the docker group, log out and log back in for the changes to take effect.

Step 8: Test Docker Installation

Test Docker by running the following command to pull and run the hello-world image:

docker run --rm hello-world

This command downloads a test image and runs it in a container. If Docker is installed correctly, you'll see a message indicating that the installation was successful.