Docker Installation on Linux: Complete Guide for All Major Distributions

Docker has revolutionized application deployment by providing a lightweight, portable containerization platform that enables developers and system administrators to package applications with all their dependencies. This comprehensive guide walks you through installing Docker Engine on major Linux distributions including Ubuntu, Debian, CentOS, Rocky Linux, and Fedora.

Table of Contents

Introduction to Docker

Docker is an open-source containerization platform that packages applications and their dependencies into standardized units called containers. Unlike virtual machines, containers share the host system's kernel, making them incredibly lightweight and fast to start. Docker has become the de facto standard for containerization in production environments, supporting microservices architectures, CI/CD pipelines, and cloud-native applications.

Why Use Docker?

  • Portability: Run the same container on any system that supports Docker
  • Isolation: Applications run in isolated environments without conflicts
  • Efficiency: Containers use fewer resources than traditional VMs
  • Version Control: Track changes to container images like code
  • Rapid Deployment: Start containers in seconds, not minutes
  • Scalability: Easily scale applications horizontally

Prerequisites

Before installing Docker on your Linux system, ensure you meet these requirements:

System Requirements

  • Operating System: 64-bit Linux distribution (Docker doesn't support 32-bit systems)
  • Kernel Version: Linux kernel 3.10 or higher (4.x or 5.x recommended)
  • Memory: Minimum 2GB RAM (4GB+ recommended for production)
  • Disk Space: At least 10GB free disk space
  • CPU: Any modern 64-bit processor with virtualization support

User Permissions

You need either:

  • Root access (sudo privileges)
  • Or be part of the docker group (configured during installation)

Check Your System

Verify your system meets the requirements:

# Check kernel version
uname -r

# Check system architecture (should show x86_64)
uname -m

# Check available disk space
df -h

# Check memory
free -h

Remove Old Docker Versions

If you have older Docker versions installed, remove them first:

# Ubuntu/Debian
sudo apt-get remove docker docker-engine docker.io containerd runc

# CentOS/Rocky Linux/Fedora
sudo dnf remove docker docker-client docker-client-latest docker-common \
  docker-latest docker-latest-logrotate docker-logrotate docker-engine

Installation Methods Overview

Docker can be installed using several methods:

  1. Repository Method (Recommended): Install from Docker's official repository
  2. Convenience Script: Quick installation using Docker's automated script
  3. Manual Installation: Download and install packages manually
  4. Binary Installation: Install from static binaries

This guide focuses on the repository method as it provides the most reliable and maintainable installation.

Installing Docker on Ubuntu/Debian

Step 1: Update Package Index

sudo apt-get update
sudo apt-get upgrade -y

Step 2: Install Required Dependencies

sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Step 3: Add Docker's Official GPG Key

# Create directory for keyrings
sudo install -m 0755 -d /etc/apt/keyrings

# Download and add Docker's GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
  sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set proper permissions
sudo chmod a+r /etc/apt/keyrings/docker.gpg

For Debian, replace ubuntu with debian in the URL above.

Step 4: Set Up Docker Repository

# For Ubuntu
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

# For Debian
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Install Docker Engine

# Update package index with Docker repository
sudo apt-get update

# Install Docker Engine, containerd, and Docker Compose plugin
sudo apt-get install -y \
    docker-ce \
    docker-ce-cli \
    containerd.io \
    docker-buildx-plugin \
    docker-compose-plugin

Step 6: Verify Installation

# Check Docker version
docker --version

# Check Docker service status
sudo systemctl status docker

Installing Docker on CentOS/Rocky Linux/Fedora

Step 1: Update System Packages

# CentOS/Rocky Linux
sudo dnf update -y

# Or for older CentOS versions
sudo yum update -y

Step 2: Install Required Dependencies

sudo dnf install -y \
    dnf-plugins-core \
    yum-utils \
    device-mapper-persistent-data \
    lvm2

Step 3: Add Docker Repository

# For CentOS/Rocky Linux
sudo dnf config-manager --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

# For Fedora
sudo dnf config-manager --add-repo \
    https://download.docker.com/linux/fedora/docker-ce.repo

Step 4: Install Docker Engine

# Install Docker packages
sudo dnf install -y \
    docker-ce \
    docker-ce-cli \
    containerd.io \
    docker-buildx-plugin \
    docker-compose-plugin

If you encounter conflicts with containerd.io, you may need to remove podman first:

sudo dnf remove podman buildah

Step 5: Start and Enable Docker Service

# Start Docker daemon
sudo systemctl start docker

# Enable Docker to start on boot
sudo systemctl enable docker

# Verify service is running
sudo systemctl status docker

Post-Installation Configuration

Enable Docker Service

Ensure Docker starts automatically on system boot:

sudo systemctl enable docker
sudo systemctl enable containerd

Add User to Docker Group

Running Docker commands without sudo requires adding your user to the docker group:

# Add current user to docker group
sudo usermod -aG docker $USER

# For adding a specific user
sudo usermod -aG docker username

# Apply group changes (log out and back in, or use)
newgrp docker

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

Configure Docker Daemon

Create or edit the Docker daemon configuration file:

sudo mkdir -p /etc/docker
sudo nano /etc/docker/daemon.json

Add the following configuration:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "storage-driver": "overlay2",
  "default-address-pools": [
    {
      "base": "172.17.0.0/16",
      "size": 24
    }
  ]
}

Restart Docker to apply changes:

sudo systemctl restart docker

Configure Docker to Use Systemd Cgroup Driver

For Kubernetes compatibility, configure the cgroup driver:

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}

Docker Compose Installation

Docker Compose is now included as a plugin with Docker Engine. Verify it's installed:

docker compose version

If you need the standalone version:

# Download latest version
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
  -o /usr/local/bin/docker-compose

# Make executable
sudo chmod +x /usr/local/bin/docker-compose

# Verify installation
docker-compose --version

Verification and Testing

Test Docker Installation

Run the hello-world container:

docker run hello-world

Expected output:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Run Ubuntu Container

Test with an interactive Ubuntu container:

docker run -it ubuntu bash

Inside the container:

# Check Ubuntu version
cat /etc/os-release

# Exit container
exit

Check Docker System Information

# Display system-wide information
docker info

# Show Docker version details
docker version

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# List downloaded images
docker images

Test Docker Network

# Run nginx container
docker run -d -p 8080:80 --name test-nginx nginx

# Test connection
curl http://localhost:8080

# Stop and remove container
docker stop test-nginx
docker rm test-nginx

Security Best Practices

Enable Docker Content Trust

Enable content trust to verify image authenticity:

export DOCKER_CONTENT_TRUST=1

Add to .bashrc or .zshrc for persistence:

echo 'export DOCKER_CONTENT_TRUST=1' >> ~/.bashrc
source ~/.bashrc

Limit Docker Daemon Exposure

Never expose Docker daemon socket over TCP without TLS:

{
  "hosts": ["unix:///var/run/docker.sock"]
}

Configure User Namespace Remapping

Enable user namespace remapping for additional isolation:

{
  "userns-remap": "default"
}

Set Resource Limits

Configure default resource limits in daemon.json:

{
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 64000,
      "Soft": 64000
    }
  }
}

Enable AppArmor/SELinux

Keep security modules enabled:

# Check AppArmor status (Ubuntu/Debian)
sudo aa-status | grep docker

# Check SELinux status (CentOS/Rocky)
getenforce

Regular Updates

Keep Docker updated:

# Ubuntu/Debian
sudo apt-get update && sudo apt-get upgrade docker-ce

# CentOS/Rocky Linux
sudo dnf update docker-ce

Troubleshooting Common Issues

Docker Daemon Won't Start

Check systemd service status:

sudo systemctl status docker
sudo journalctl -u docker.service -n 50 --no-pager

Common fixes:

# Remove broken state files
sudo rm /var/lib/docker/network/files/local-kv.db

# Reset Docker daemon
sudo systemctl reset-failed docker
sudo systemctl start docker

Permission Denied Error

If you see "permission denied" when running docker commands:

# Verify you're in docker group
groups

# If not, add yourself
sudo usermod -aG docker $USER

# Reload group membership
newgrp docker

# Or log out and back in

Port Already in Use

Find process using the port:

sudo netstat -tulpn | grep :port_number
# Or
sudo lsof -i :port_number

Storage Driver Issues

Check current storage driver:

docker info | grep "Storage Driver"

If needed, change to overlay2:

sudo nano /etc/docker/daemon.json

Add:

{
  "storage-driver": "overlay2"
}

DNS Resolution Issues

If containers can't resolve domain names:

{
  "dns": ["8.8.8.8", "8.8.4.4"]
}

Clean Up Docker Resources

Free up disk space:

# Remove unused containers, networks, images
docker system prune -a

# Remove specific resources
docker container prune
docker image prune
docker volume prune
docker network prune

Check Docker Logs

# System logs
sudo journalctl -fu docker.service

# Container logs
docker logs container_name
docker logs --tail 100 -f container_name

Conclusion

You have successfully installed Docker on your Linux system. Docker provides a powerful platform for containerizing applications, enabling consistent environments from development to production.

Key Takeaways

  • Docker installation varies slightly between distributions but follows similar patterns
  • Post-installation configuration is crucial for security and optimal performance
  • Adding users to the docker group enables non-root Docker usage
  • Regular updates and security best practices protect your containerized infrastructure
  • Docker Compose extends Docker's capabilities for multi-container applications

Next Steps

  1. Learn Docker Basics: Explore container management commands
  2. Create Dockerfiles: Build custom container images
  3. Use Docker Compose: Orchestrate multi-container applications
  4. Implement Security: Apply security hardening techniques
  5. Explore Networking: Understand Docker network modes
  6. Manage Volumes: Implement persistent data storage
  7. Monitor Resources: Set up logging and monitoring

Recommended Reading

  • Docker official documentation: https://docs.docker.com/
  • Docker Hub for container images: https://hub.docker.com/
  • Best practices for writing Dockerfiles
  • Docker security scanning and vulnerability management
  • Container orchestration with Kubernetes

Docker's containerization technology has transformed modern application deployment. With your Docker installation complete, you're ready to build, ship, and run containerized applications efficiently and securely across any Linux environment.