0
0
DockerHow-ToBeginner · 4 min read

How to Install Docker on Linux: Step-by-Step Guide

To install Docker on Linux, update your package list, install required packages, add Docker’s official repository, then install docker-ce. Finally, start and enable the docker service to run containers.
📐

Syntax

These commands show the main steps to install Docker on Linux systems like Ubuntu or Debian:

  • sudo apt update: Refresh package list.
  • sudo apt install apt-transport-https ca-certificates curl software-properties-common: Install tools to add repositories.
  • curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg: Add Docker’s official GPG key.
  • 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: Add Docker repository.
  • sudo apt update: Refresh package list again.
  • sudo apt install docker-ce docker-ce-cli containerd.io: Install Docker packages.
  • sudo systemctl start docker and sudo systemctl enable docker: Start and enable Docker service.
bash
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
💻

Example

This example shows the full commands to install Docker on Ubuntu 22.04. It updates packages, adds Docker’s repository, installs Docker, and starts the service.

bash
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu jammy stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo docker run hello-world
Output
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:7e4a1f3b0a9a7e7a1e7a7e7a7e7a7e7a7e7a7e7a7e7a7e7a7e7a7e7a7e7a7e7a7 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
⚠️

Common Pitfalls

Common mistakes when installing Docker on Linux include:

  • Not updating the package list before installing, causing missing packages.
  • Skipping adding Docker’s official GPG key and repository, leading to installation errors.
  • Trying to run Docker commands without starting the Docker service.
  • Not using sudo for commands that require root permissions.

Always follow the exact steps and check for errors after each command.

bash
sudo apt install docker-ce
# This may fail if repository and keys are not added first

# Correct way:
sudo apt update
# Add keys and repo as shown in the Syntax section
sudo apt install docker-ce
📊

Quick Reference

Summary tips for installing Docker on Linux:

  • Always update package lists before installing.
  • Add Docker’s official GPG key and repository for stable releases.
  • Use sudo for all installation and service commands.
  • Start and enable Docker service to run containers.
  • Test installation with sudo docker run hello-world.

Key Takeaways

Update your package list before installing Docker to avoid missing packages.
Add Docker’s official GPG key and repository to get the latest stable version.
Use sudo to run installation and service commands with proper permissions.
Start and enable the Docker service to use Docker containers.
Verify installation by running the hello-world Docker image.