LOGO.jpg
LOGO.jpg

pic from unsplash

About Script:

Cre­at­ing a sin­gle bash script to au­to­mat­i­cally in­stall Docker and Docker Com­pose on mul­ti­ple Linux dis­tri­b­u­tions can be chal­leng­ing due to dif­fer­ences in pack­age man­agers and other fac­tors.

How­ever, We can cre­ate a script that de­tects the dis­tri­b­u­tion and in­stalls the ap­pro­pri­ate pack­ages ac­cord­ingly.


Breakdown Of The Script

  • The first line, "#!/bin/bash," specifies that the script should be run using the Bash shell.
#!/bin/bash
  • The "set -e" command tells the shell to exit immediately if any command in the script fails.
set -e
  • The script then checks the "/etc/os-release" file to detect the Linux distribution that the script is running on.
# Detect Linux distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
else
    echo "Unsupported distribution!"
    exit 1
fi
  • If the Linux distribution is Ubuntu or Debian, the script installs the necessary dependencies for Docker, adds the Docker repository to the system, and installs Docker using the "apt-get" command.
if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
    apt-get update
    apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg
    curl -fsSL https://download.docker.com/linux/$ID/gpg | apt-key add -
    add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$ID $(lsb_release -cs) stable"
    apt-get update
    apt-get install -y docker-ce
  • If the Linux distribution is CentOS, the script installs the necessary dependencies for Docker, adds the Docker repository to the system, and installs Docker using the "yum" command.
elif [ "$ID" == "centos" ]; then
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce
    systemctl start docker.service
    systemctl enable docker.service
  • If the Linux distribution is Fedora, the script installs the necessary dependencies for Docker, adds the Docker repository to the system, and installs Docker using the "dnf" command.
elif [ "$ID" == "fedora" ]; then
    dnf -y install dnf-plugins-core
   dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
    dnf install -y docker-ce
  • After installing Docker, the script installs Docker Compose using the latest version available on GitHub.
# Install Docker Compose
install_docker_compose() {
    COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
    curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
}
  • Finally, the script prints messages indicating that Docker and Docker Compose were installed successfully.
echo "Docker Compose installed successfully."

Full Content Of The Script

The above script is a Bash script that in­stalls Docker and Docker Com­pose on a Linux sys­tem.
#!/bin/bash

set -e

# Detect Linux distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
else
    echo "Unsupported distribution!"
    exit 1
fi

# Install Docker
install_docker() {
    if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
        apt-get update
        apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg
        curl -fsSL https://download.docker.com/linux/$ID/gpg | apt-key add -
        add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$ID $(lsb_release -cs) stable"
        apt-get update
        apt-get install -y docker-ce
    elif [ "$ID" == "centos" ]; then
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        yum install -y docker-ce
        systemctl start docker.service
        systemctl enable docker.service
    elif [ "$ID" == "fedora" ]; then
        dnf -y install dnf-plugins-core
       dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
        dnf install -y docker-ce
    else
        echo "Unsupported distribution!"
        exit 1
    fi
}

# Install Docker Compose
install_docker_compose() {
    COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
    curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
}

# Start installation
echo "Installing Docker on $PRETTY_NAME..."
install_docker
echo "Docker installed successfully."

echo "Installing Docker Compose..."
install_docker_compose
echo "Docker Compose installed successfully."

Use The Script:

  1. Save the contents of the script in a file, e.g., install_docker.sh.
  2. Make the script executable with chmod +x install_docker.sh.
  3. Run the script as root or with sudo: sudo ./install_docker.sh.

Also, We can down­load, make ex­e­cutable, and run an HTTPS script us­ing a sin­gle com­mand line in the ter­mi­nal by chain­ing the com­mands to­gether with the && op­er­a­tor.
Root Privileges is requires

curl -O https://kingtam.win/usr/uploads/script/install-docker.sh && chmod +x install-docker.sh && ./install-docker.sh

This com­mand will down­load the script from the spec­i­fied URL, make it ex­e­cutable, and then run it. If any of the com­mands fail, the sub­se­quent com­mands will not be ex­e­cuted.


Conclusion:

The ba­sic script that sup­ports Ubuntu, Debian, CentOS, and Fedora. Note that this script re­quires root privileges to in­stall pack­ages.

This script is use­ful for quickly and eas­ily in­stalling Docker and Docker Com­pose on a Linux sys­tem. It au­to­mates the process of in­stalling de­pen­den­cies and con­fig­ur­ing the sys­tem, sav­ing time and ef­fort for the user.