Automating Docker Installation on Multiple Linux Distributions

This blog post introduces a powerful Bash script that automates the entire Docker installation and configuration process. It works seamlessly across Ubuntu, CentOS, and Fedora. Let’s dive deep into the script and understand each step to see how it achieves this.

Automating Docker Installation on Multiple Linux Distributions

Docker has become a cornerstone of modern application development and deployment. Its ability to package applications and dependencies into lightweight, portable containers makes it indispensable for developers and system administrators alike. But installing and configuring Docker across different Linux distributions can be a daunting task.

This blog post introduces a powerful Bash script that automates the entire Docker installation and configuration process. It works seamlessly across Ubuntu, CentOS, and Fedora. Let’s dive deep into the script and understand each step to see how it achieves this.

1. Prerequisites

  1. Root Access: This script must be run as the root user, as it modifies system configurations and installs software.
  2. Supported Distributions:
    • Ubuntu (or other Debian-based systems)
    • CentOS
    • Fedora
  3. Package Managers:
    • apt (for Ubuntu)
    • yum or dnf (for CentOS and Fedora)

2. Script Overview

Here’s the complete script, followed by a detailed breakdown of its functionality:

#!/bin/bash -eu

# Function to detect the Linux distribution
detect_distro() {
    if [[ -f /etc/os-release ]]; then
        . /etc/os-release
        echo $ID
    else
        echo "Unknown"
    fi
}

# Uninstall conflicting packages
uninstall_conflicting_packages() {
    echo "==> Removing conflicting packages"
    local packages=(docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc)
    if command -v apt-get &> /dev/null; then
        for pkg in "${packages[@]}"; do
            apt-get -y remove $pkg || echo "$pkg not installed."
        done
    elif command -v yum &> /dev/null || command -v dnf &> /dev/null; then
        for pkg in "${packages[@]}"; do
            yum -y remove $pkg || echo "$pkg not installed."
        done
    else
        echo "Unsupported package manager. Skipping package removal."
    fi
}

# Setup Docker repository
setup_docker_repo() {
    echo "==> Setting up Docker repository"
    local distro=$(detect_distro)

    if command -v apt-get &> /dev/null; then
        apt-get update
        apt-get -y install ca-certificates curl gnupg
        install -m 0755 -d /etc/apt/keyrings
        curl -fsSL https://download.docker.com/linux/$distro/gpg > /etc/apt/keyrings/docker.asc
        chmod a+r /etc/apt/keyrings/docker.asc

        echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/$distro $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
            tee /etc/apt/sources.list.d/docker.list > /dev/null
        apt-get update
    elif command -v yum &> /dev/null || command -v dnf &> /dev/null; then
        yum -y install yum-utils
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    else
        echo "Unsupported distribution for setting up Docker repository."
        exit 1
    fi
}

# Install Docker
install_docker() {
    echo "==> Installing Docker"
    if command -v apt-get &> /dev/null; then
        apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    elif command -v yum &> /dev/null || command -v dnf &> /dev/null; then
        yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    else
        echo "Unsupported package manager for Docker installation."
        exit 1
    fi
}

# Enable and start Docker service
enable_start_docker() {
    echo "==> Enabling and starting Docker service"
    systemctl enable docker
    systemctl start docker
}

# Configure Docker user group
configure_docker_group() {
    echo "==> Adding current user to Docker group"
    usermod -aG docker $USER
}

# Overwrite Docker service configuration
overwrite_docker_service_config() {
    echo "==> Overwriting Docker service configuration"
    cp /lib/systemd/system/docker.service /etc/systemd/system/
    sed -i 's/ -H fd:\/\/\///g' /etc/systemd/system/docker.service
    systemctl daemon-reload
    systemctl restart docker
}

# Add Docker daemon configuration
configure_docker_daemon() {
    echo "==> Adding Docker daemon configuration"
    mkdir -p /etc/docker
    cat <<EOT > /etc/docker/daemon.json
{
  "hosts": ["unix:///var/run/docker.sock", "tcp://127.0.0.1:2375"]
}
EOT
    systemctl restart docker
}

# Main script execution
main() {
    uninstall_conflicting_packages
    setup_docker_repo
    install_docker
    enable_start_docker
    configure_docker_group
    overwrite_docker_service_config
    configure_docker_daemon

    echo "==> Docker installation and configuration complete!"
    echo "Please log out and log back in to apply group changes for $USER."
}

main

3. Script Breakdown

3.1 Detecting the Linux Distribution

detect_distro() {
    if [[ -f /etc/os-release ]]; then
        . /etc/os-release
        echo $ID
    else
        echo "Unknown"
    fi
}
  • Reads /etc/os-release to identify the Linux distribution.
  • Outputs the distribution ID (e.g., ubuntu, centos).

3.2 Uninstalling Conflicting Packages

uninstall_conflicting_packages() {
    echo "==> Removing conflicting packages"
    local packages=(docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc)
    # Remove packages using appropriate package manager
}
  • Removes existing Docker or container-related packages to avoid conflicts.
  • Supports both apt (Debian-based) and yum/dnf (Red Hat-based) systems.

3.3 Setting Up the Docker Repository

setup_docker_repo() {
    echo "==> Setting up Docker repository"
    # Add Docker repository and GPG keys for secure installation
}
  • Configures Docker’s official repository based on the distribution.
  • Ensures Docker is installed from a trusted source.

3.4 Installing Docker

install_docker() {
    echo "==> Installing Docker"
    # Installs Docker and its essential components
}
  • Installs Docker CE, CLI, containerd, and plugins like Buildx and Compose.

3.5 Enabling and Starting the Docker Service

enable_start_docker() {
    echo "==> Enabling and starting Docker service"
    systemctl enable docker
    systemctl start docker
}
  • Ensures Docker starts on boot and runs immediately after installation.

3.6 Adding the Current User to the Docker Group

configure_docker_group() {
    echo "==> Adding current user to Docker group"
    usermod -aG docker $USER
}
  • Adds the current user to the Docker group to manage Docker without root.

3.7 Customizing Docker Service Configuration

overwrite_docker_service_config() {
    echo "==> Overwriting Docker service configuration"
    # Modifies Docker service to allow multiple socket listeners
}
  • Adjusts the default Docker service configuration to meet specific requirements.

3.8 Adding Docker Daemon Configuration

configure_docker_daemon() {
    echo "==> Adding Docker daemon configuration"
    # Configures Docker to listen on additional sockets
}
  • Creates /etc/docker/daemon.json for advanced daemon configuration.

4. How to Run the Script

  1. Save the script as install_docker.sh.

Run the script as root:

./install_docker.sh

Make it executable:

chmod +x install_docker.sh

Conclusion

This script automates Docker installation and configuration across multiple Linux distributions. Its modular design ensures it adapts to different environments, making it a robust solution for system administrators. Feel free to integrate this script into your DevOps toolkit and simplify Docker management. Happy containerizing! 🚀

Read next

Playing God of War on Linux

Black Friday brought new games to my Steam library, and one of them was God of War. I’m not much of a gamer—I'm more interested in how games run on Linux. Here’s my full guide on installing, configuring, and playing God of War on Linux with Proton.

Managing Users and Permissions in Ubuntu: Creating and Managing Users

In any multi-user operating system, user management is a fundamental task. Linux, including Ubuntu, provides robust tools to handle users and permissions, which are essential for system administration. Whether you're setting up a server or managing a desktop system, knowing how to create, manage.

Ultimate Step‑by‑Step Guide to GameMode

A deep, practical guide to installing, configuring, and supercharging GameMode on Ubuntu for smoother, faster gaming. From GPU drivers to per-game tweaks, this post walks you through squeezing every last drop of performance out of Steam and Proton.