How To Install Docker Linux

How To Install Docker Linux

Reading time1 min
#Docker#Linux#Containers#DockerInstallation#LinuxSysAdmin#Containerization

Mastering Docker Installation on Linux: A Step-by-Step Practical Guide

Installing Docker effectively on Linux systems is foundational for developers and sysadmins to leverage containerization for consistent, scalable application deployment. A clear, precise how-to ensures fewer setup errors and faster project kickoffs. Forget generic install scripts—this guide dives into the specific commands and best practices for different Linux distros, revealing common pitfalls and optimizing for reliability rather than just speed.


Why Installing Docker Properly Matters

Docker’s power lies in its ability to isolate applications within containers, providing portability and scalability. But before you can harness this power, Docker must be installed correctly. Missteps during installation can lead to permission issues, outdated versions, or failed startups—costing valuable time and causing frustration.

Whether you're using Ubuntu, CentOS, Fedora, or Debian-based systems, this guide walks you through reliable installation steps that will keep your environment stable from the start.


Preparation: Check Your Environment and Remove Old Versions

Before diving into installing Docker, ensure your system is ready:

  1. Check your Linux distribution version:
    Some commands differ between distros—or even between versions within the same distro.

    lsb_release -a  # Ubuntu/Debian
    cat /etc/redhat-release  # CentOS/Fedora
    
  2. Remove old Docker versions (if any):
    Older installations might cause conflicts.

    sudo apt-get remove docker docker-engine docker.io containerd runc  # Ubuntu/Debian
    sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine  # CentOS
    

Step 1: Installing Docker on Ubuntu (20.04+, 22.04+)

Add Docker’s official GPG key and set up the repository

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

Add Docker’s official GPG key:

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

Set up the stable repository:

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

Install Docker Engine

Update the package index:

sudo apt-get update

Install the latest version of Docker Engine and containerd:

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

Verify Installation

Check Docker version:

docker --version
# Example output: Docker version 20.10.17, build 100c701

Run the hello-world container:

sudo docker run hello-world

If you see a welcome message from Docker, congratulations—you’ve installed it successfully!


Step 2: Installing Docker on CentOS 7/8 or RHEL-based Distributions

Prepare your system

Remove older versions (if any):

sudo yum remove -y docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

Install required packages:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Add the official Docker repo

For CentOS 7 or RHEL:

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

For CentOS 8 Stream or RHEL 8+, consider using dnf instead of yum.

Install Docker Engine

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

Start and enable the service to run at boot

sudo systemctl start docker
sudo systemctl enable docker

Verify that Docker is running:

sudo systemctl status docker

Test by running hello-world container:

sudo docker run hello-world

Step 3: Post-Installation Steps (Optional but Recommended)

Running docker commands usually requires sudo. To avoid this every time:

Add your user to the docker group:

sudo usermod -aG docker $USER
newgrp docker  # Apply changes to current session without logout-login cycle.

Test without sudo:

docker run hello-world

If everything runs smoothly, you’re good to go!


Common Pitfalls & Troubleshooting Tips

  • Could not connect to the Docker daemon
    Usually caused by service not running or insufficient permissions. Run sudo systemctl start docker or check user group membership as above.

  • Proxy or Firewall blocking downloads
    Ensure ports like TCP 443 are open if behind corporate firewalls.

  • Kernel incompatibility (rare)
    Most modern Linux distros already have compatible kernels; if you're running a minimal custom kernel, check kernel modules like overlay2.

  • Old cached packages
    After updating repos or keys, always run apt-get update or yum makecache.


Wrapping Up: Next Steps After Installation

Now that you have a reliable Docker setup on your Linux system... what next?

  • Explore basic commands:
    docker ps        # List containers  
    docker images    # List images 
    
  • Start building your own images with a Dockerfile.
  • Use Docker Compose for multi-container applications.
  • Integrate with CI/CD pipelines for continuous deployment.

Mastering installation is just step one—but it’s one that sets up everything else beautifully.


Stay tuned for future posts diving deeper into optimizing your Docker development workflow on Linux! If you have questions or hit any snags during installation, drop a comment below—I’m here to help troubleshoot!