Skip to main content

1. Prerequisites

Before starting, uninstall any old versions of Docker (like docker, docker.io, or docker-engine) to avoid conflicts:
sudo apt-get remove docker docker-engine docker.io containerd runc

2. Installation on Ubuntu / Debian

Ubuntu is the most popular platform for Docker. Here is how to set it up:

Step 1: Update and Install Dependencies

sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release

Step 2: Add Docker’s Official GPG Key

This ensures the software you’re downloading is authentic.
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 3: Set up the Repository

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

Step 4: Install Docker Engine

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

3. Installation on CentOS / RHEL

For Red Hat-based systems, use the yum-utils package.
  1. Install yum-utils: sudo yum install -y yum-utils
  2. Add Repo: sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  3. Install: sudo yum install docker-ce docker-ce-cli containerd.io
  4. Start Docker: sudo systemctl start docker

4. Post-Installation Steps (Important)

By default, you need sudo to run Docker commands. To run Docker as a non-root user, add your user to the docker group:
  1. Create the group: sudo groupadd docker (usually already exists).
  2. Add your user: sudo usermod -aG docker $USER
  3. Apply changes: Log out and log back in, or run newgrp docker.

Verify the Installation

Run the “hello-world” image to make sure everything is working:
docker run hello-world