一、需求

公司经常会有上docker的需求,大多数使用的环境为单独docker环境。通过docker-compose进行管理。此时如何快速部署docker则可以提高效率。

二、环境

  • CentOS 7.X服务器
  • 系统进行过初始化

三、安装流程

  • 该软件需要通过网络进行安装,则有判断网络是否正常
  • 需要添加docker安装源后进行安装docker-ce
  • docker的json配置文件修改
  • docker-compose的安装(涉及到python3.6安装)
//核心代码如下
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# yum -y install docker-ce
# pip3 install docker-compose

四、脚本安装

#!/bin/bash
# Docker安装
# Author yousong.xiang 250919938@qq.com
# Date 2019.4.8
# v1.0.1

[ -f /etc/profile ] && . /etc/profile

cmd=`pwd`

function check_rpm() {
    rpm_package=1
    package_num=`rpm -qa |grep{rpm_package}|wc -l`
    #此类判断VSFTPD可以检测到,其他RPM包检测请慎用
    echo {package_num}
}

function check_install() {
    if [ -f /var/log/lock.docker ]; then
        echo -e "\033[31;1mdocker已经安装过,请确认\033[0m"
        exit 1
    fi
}

function check_network() {
    check_install
    ping -c 1 -W 1 www.baidu.com &>/dev/null
    if [? -ne 0 ]; then
        echo -e "\033[31;1m网络连接失败,请检查\033[0m"
        exit 5
    fi

    if [ "`echo UID`" != "0" ]; then
        echo -e "\033[31;1m该软件需要root安装权限\033[0m"
        exit 4
    fi
}

function epel_install() {
    #关闭selinux,安装基础依赖环境函数
    sed -i '/^SELINUX=.*/s/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
    setenforce 0
    #判断是否安装redhat-lsb-core
    if [ `check_rpm redhat-lsb-core` == '0' ]; then
        yum install -y redhat-lsb-core  >/dev/null >&1
    fi 

    if [ `check_rpm epel-release` == '0' ]; then
        yum install -y epel-release
    fi

}

function docker_install() {
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    if [? -ne 0 ]; then
        echo -e "\033[31m添加docker-ce.repo失败\033[0m"
        exit 3
    fi
    yum makecache fast
    yum -y install docker-ce
    if [ ? -ne 0 ]; then
        echo -e "\033[31mdocker安装失败\033[0m"
        exit 4
    fi
    systemctl start docker
    if [? -ne 0 ]; then
        echo -e "\033[31mdocker start fail\033[0m"
        exit 4
    fi 

    tee /etc/docker/daemon.json <<-'EOF'
{
  "data-root": "/data/docker",
  "storage-driver": "overlay2",
  "registry-mirrors": ["https://yx8zsx76.mirror.aliyuncs.com"]
}
EOF

    if [ ! -d /data/docker ]; then
        mkdir /data/docker -p
    fi
    systemctl daemon-reload
    systemctl restart docker
    systemctl enable docker.service
}

function python_env() {
    python3 -V &>/dev/null
    if [ ? -ne 0 ]; then
        yum -y install python36 python36-devel
    fi

    pip3 -V &>/dev/null

    if [? -ne 0 ]; then
        if [ ! -f /usr/bin/python3 ]; then
            ln -s /usr/bin/python3.6 /usr/bin/python3
        fi
        yum install -y python36-setuptools python36-six.noarch
        wget -P /tmp/ https://bootstrap.pypa.io/get-pip.py
        if [ ? -ne 0 ]; then
            echo -e "\033[31mdownload get-pip.py fail\033[0m"
            exit 5
        fi
        /usr/bin/python3.6 /tmp/get-pip.py
        pip3 install docker-compose
        if [? -eq 0 ]; then
             echo -e "\033[32mdocker-compose install seccuess\033[0m"
        fi


    else
        pip3 install docker-compose
        if [ $? -eq 0 ]; then
             echo -e "\033[32mdocker-compose install seccuess\033[0m"
        fi
    fi

}

epel_install
docker_install
python_env

脚本参考文档:

https://github.com/xiangys0134/deploy/blob/master/software_install/docker/docker_install.sh
更多运维配置请访问:https://github.com/xiangys0134
最后修改日期: 2023年12月13日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。