How to Install Docker in Linux

Docker has revolutionized the way developers build, ship, and run applications. Its containerization technology allows for consistent environments across development, testing, and production, significantly reducing the “it works on my machine” problem. For Linux users, installing Docker is a straightforward process that unlocks a powerful suite of tools for modern software development and deployment. This guide will walk you through the essential steps to get Docker up and running on your Linux distribution.

Understanding Docker and Its Installation on Linux

Before diving into the installation process, it’s beneficial to understand what Docker is and why its Linux installation is so prevalent. Docker operates on the principle of containerization, packaging an application and its dependencies into a portable unit called a container. This container can then run consistently on any machine with a Docker engine installed. Linux, being the native environment for many containerization technologies, offers robust and efficient ways to install and manage Docker.

The primary components you’ll install are the Docker Engine, Docker CLI (Command Line Interface), and Docker Compose. The Docker Engine is the daemon that runs in the background, managing containers. The Docker CLI is the tool you’ll use to interact with the Docker daemon, issuing commands to build images, run containers, and more. Docker Compose is a tool for defining and running multi-container Docker applications.

Why Linux is Ideal for Docker

Linux’s kernel features, such as namespaces and control groups (cgroups), are fundamental to how Docker achieves isolation and resource management for its containers. This native integration means Docker often performs exceptionally well on Linux systems. Furthermore, many cloud platforms and server environments are Linux-based, making Docker installation on Linux a standard practice for deploying scalable applications.

Prerequisites for Installation

While Docker’s installation is generally smooth, a few prerequisites ensure a seamless experience. Ensure your Linux system is up-to-date. This typically involves running package manager updates. For instance, on Debian-based systems (like Ubuntu), you would use sudo apt update && sudo apt upgrade. On Fedora or CentOS, you might use sudo dnf update or sudo yum update.

You will also need administrative (root) privileges to install software and manage system services. Most of the commands that follow will require sudo.

Installing Docker on Debian-Based Systems (Ubuntu, Mint)

For users of Debian-based Linux distributions such as Ubuntu and Linux Mint, the recommended method for installing Docker is through their official repositories. While a “get-docker.sh” script exists, using the package manager ensures easier updates and better integration with your system.

Setting up the Docker Repository

The first step involves adding Docker’s official GPG key to your system and then adding the Docker repository to your package manager’s sources list. This ensures that your system fetches Docker packages directly from Docker’s trusted servers.

  1. Update your apt package index:

    sudo apt update
    
  2. Install necessary packages to allow apt to use a repository over HTTPS:

    sudo apt install 
        ca-certificates 
        curl 
        gnupg 
        lsb-release
    
  3. Add Docker’s official GPG key:
    Create a directory for Docker’s GPG key if it doesn’t exist and then download and add the key.

    sudo mkdir -p /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    
  4. Set up the Docker repository:
    This step adds the Docker repository to your system’s sources. The $(lsb_release -cs) command dynamically determines your Ubuntu version (e.g., “jammy”, “focal”) to ensure you get the correct repository.
    bash
    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

Installing the Docker Engine

With the repository configured, you can now install the Docker packages. It’s good practice to update your apt index again after adding a new repository.

  1. Update your apt package index once more:

    sudo apt update
    
  2. Install Docker Engine, CLI, and Containerd:
    This command installs the core Docker components.
    bash
    sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

    • docker-ce: The Docker Community Edition engine.
    • docker-ce-cli: The Docker command-line interface.
    • containerd.io: A core container runtime.
    • docker-buildx-plugin: Enables advanced build capabilities.
    • docker-compose-plugin: Integrates Docker Compose functionality.

Verifying the Installation

After installation, you can verify that Docker is running correctly.

  1. Check the Docker service status:

    sudo systemctl status docker
    

    You should see output indicating that the service is active and running.

  2. Run the “hello-world” container:
    This is a standard test to ensure Docker can pull an image and run a container successfully.
    bash
    sudo docker run hello-world

    If successful, you will see a message stating that Docker is installed and working correctly.

Managing the Docker Service

Docker runs as a system service. You can manage it using systemctl:

  • Start Docker: sudo systemctl start docker
  • Stop Docker: sudo systemctl stop docker
  • Restart Docker: sudo systemctl restart docker
  • Enable Docker to start on boot: sudo systemctl enable docker
  • Disable Docker from starting on boot: sudo systemctl disable docker

Running Docker Commands Without sudo

By default, Docker commands require root privileges. To run Docker commands as a non-root user, you need to add your user to the docker group.

  1. Add your user to the docker group:

    sudo usermod -aG docker $USER
    

    Replace $USER with your actual username if needed, though $USER is usually sufficient.

  2. Apply group changes:
    For the changes to take effect, you need to log out and log back in, or run the following command to reload your user’s group memberships:
    bash
    newgrp docker

    After this, you should be able to run Docker commands without sudo. Verify this by running docker run hello-world again.

Installing Docker on Fedora, CentOS, and RHEL

For Red Hat-based distributions like Fedora, CentOS Stream, and RHEL, the installation process is similar in principle but uses different package management tools (dnf or yum).

Setting up the Docker Repository

Similar to Debian-based systems, you’ll add Docker’s official repository to your system.

  1. Install dnf-utils (for Fedora) or yum-utils (for CentOS/RHEL):
    These utilities provide the dnf config-manager or yum-config-manager commands to easily manage repositories.
    On Fedora:

    sudo dnf install -y dnf-plugins-core
    

    On CentOS/RHEL:

    sudo yum install -y yum-utils
    
  2. Add the Docker repository:
    This command adds the Docker CE repository to your system’s configuration.
    On Fedora:
    bash
    sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

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

    (Note: For RHEL, you might need to specify the correct CentOS version in the URL if a direct RHEL repo isn’t available, or use the Fedora repo as it’s often compatible.)

Installing the Docker Engine

Once the repository is configured, you can install Docker.

  1. Install Docker Engine, CLI, and Containerd:
    On Fedora:
    bash
    sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

    On CentOS/RHEL:
    bash
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Starting and Enabling Docker

After installation, you need to start the Docker daemon and configure it to start on boot.

  1. Start the Docker service:

    sudo systemctl start docker
    
  2. Enable Docker to start on boot:

    sudo systemctl enable docker
    
  3. Check the Docker service status:
    bash
    sudo systemctl status docker

Verifying the Installation

Run the hello-world container to confirm the installation.

sudo docker run hello-world

Running Docker Commands Without sudo

Similar to Debian-based systems, you can add your user to the docker group to avoid using sudo for Docker commands.

  1. Add your user to the docker group:

    sudo usermod -aG docker $USER
    
  2. Apply group changes:
    Log out and log back in, or use newgrp to reload your group memberships:
    bash
    newgrp docker

Alternative Installation Method: Convenience Script

Docker also provides a convenience script (get-docker.sh) that can be used for development or testing environments. However, it is not recommended for production environments as it bypasses package managers, making updates and uninstallation more complex.

Using the Convenience Script

  1. Download the script:

    curl -fsSL https://get.docker.com -o get-docker.sh
    
  2. Run the script:

    sudo sh get-docker.sh
    

    This script will detect your distribution and install Docker accordingly.

  3. Verify the installation:
    bash
    sudo docker run hello-world

Important Considerations for the Convenience Script

  • Updates: You’ll need to re-run the script or manually manage updates.
  • Uninstallation: Uninstalling might require manual removal of files and configurations.
  • Production Use: Always prefer the official repository installation for production systems for better stability, security, and manageability.

Next Steps After Installation

With Docker installed and verified, you’re ready to explore its capabilities. Here are a few common next steps:

  • Learning Docker Commands: Familiarize yourself with essential commands like docker ps, docker images, docker pull, docker build, and docker stop.
  • Writing Dockerfiles: Create your own container images by defining instructions in a Dockerfile.
  • Using Docker Compose: Orchestrate multi-container applications by defining services in a docker-compose.yml file.
  • Exploring Docker Hub: Discover and pull pre-built container images from Docker Hub, the world’s largest container registry.

Installing Docker on Linux is a foundational step for anyone involved in modern software development, deployment, and system administration. By following these steps, you can efficiently set up Docker and begin leveraging its powerful containerization capabilities.

Leave a Comment

Your email address will not be published. Required fields are marked *

FlyingMachineArena.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.
Scroll to Top