minilabs

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

功能说明:

在 CentOS 和 Ubuntu 系统上安装 Docker 和 Docker Compose 的 shell 脚本,并在安装后显示 Docker 和 Docker Compose 版本。

Certainly! Below is a shell script that installs Docker and Docker Compose on both CentOS and Ubuntu systems. It follows your requirements for functional programming, includes a description in the header, and displays the Docker and Docker Compose versions after installation.

#!/bin/bash

# @Author : Lan Shiyun
# @Create Time : 2021/8/18
# @FileName : docker_install.sh
# @Version : 1.0
# @Software : VS Code
# @Update Time : 2021/8/21
# @UpdateBy : Lan Shiyun
# @Description : Script to install Docker and Docker Compose

main() {
    echo "Installing Docker and Docker Compose..."

    # Check the OS type
    if [[ -f /etc/centos-release ]]; then
        install_docker_centos
        install_docker_compose
    elif [[ -f /etc/lsb-release ]]; then
        install_docker_ubuntu
        install_docker_compose
    else
        echo "Unsupported OS. Exiting..."
        exit 1
    fi

    echo "Installed Docker version:"
    docker --version

    echo "Installed Docker Compose version:"
    docker-compose --version
}

install_docker_centos() {
    sudo yum update -y
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install -y docker-ce
    sudo systemctl start docker
    sudo systemctl enable docker
}

install_docker_ubuntu() {
    sudo apt update
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [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
    sudo apt update
    sudo apt install -y docker-ce
    sudo systemctl start docker
    sudo systemctl enable docker
}

install_docker_compose() {
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
}

main

Please note that you might need to give execute permissions to the script using the command chmod +x docker_install.sh. Also, always ensure that you understand what a script does before executing it on your system.

posted on 2024-02-24 10:34  lanshiyun  阅读(50)  评论(0编辑  收藏  举报