Login

Navigation

This articles is published 646 days ago and last updated 645 days ago, some information may be out of date.

LOGO.jpg

pic from unsplash

About Script:

Creating a single bash script to automatically install Docker and Docker Compose on multiple Linux distributions can be challenging due to differences in package managers and other factors.

However, We can create a script that detects the distribution and installs the appropriate packages accordingly.


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 installs Docker and Docker Compose on a Linux system.
#!/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 download, make executable, and run an HTTPS script using a single command line in the terminal by chaining the commands together with the && operator.
Root Privileges is requires

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

This command will download the script from the specified URL, make it executable, and then run it. If any of the commands fail, the subsequent commands will not be executed.


Conclusion:

The basic script that supports Ubuntu, Debian, CentOS, and Fedora. Note that this script requires root privileges to install packages.

This script is useful for quickly and easily installing Docker and Docker Compose on a Linux system. It automates the process of installing dependencies and configuring the system, saving time and effort for the user.