注意:centos7的内核版本较老,无法支持docker所有功能,可能存在bug,不建议使用centos7安装docker,推荐使用ubuntu1604以上及centos8。
脚本需事先准备一个压缩包:harbor-offline-installer-v1.10.4.tgz,可根据自身情况更改测试。
#!/bin/bash . /etc/init.d/functions $> /dev/null set -e COLOR="echo -e \E[1;32m" COLOR1="echo -e \E[1;31m" END="\E[0m" #ubuntu依赖包 ubuntu_page=" wget apt-transport-https ca-certificates curl software-properties-common python3-pip " #centos依赖包 centos_page=" wget yum-utils device-mapper-persistent-data lvm2 " harbor_version=harbor-offline-installer-v1.10.4.tgz #centos7安装 install_centos7() { ${COLOR}"开始安装 Docker....."${END} #安装依赖包 for PAGE in ${centos_page};do rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE done #添加源信息 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && action "添加源信息成功!" || { action "添加源信息失败,请检查命令!" false ; exit; } #更新安装docker-ce yum makecache fast &> /dev/null yum -y install docker-ce-19.03.9-3.el7.x86_64 containerd.io-1.3.7-3.1.el7.x86_64 docker-ce-cli-19.03.9-3.el7.x86_64 &> /dev/null #配置镜像加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #重载配置文件 systemctl daemon-reload #启动服务 ${COLOR}"正在启动 Docker....."${END} sleep 2 systemctl start docker && action "docker启动成功!" || action "docker启动失败,请检查配置文件!" false } #centos8安装 install_centos8() { ${COLOR}"开始安装 Docker....."${END} #安装依赖包 for PAGE in ${centos_page};do rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE done yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.1.el7.x86_64.rpm &> /dev/null #添加源信息 cat > /etc/yum.repos.d/docker.repo <<EOF [docker] name=docker gpgcheck=0 baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/ EOF #更新安装docker-ce yum clean all &> /dev/null yum -y install docker-ce-19.03.9-3.el7.x86_64 containerd.io-1.3.7-3.1.el7.x86_64 docker-ce-cli-19.03.9-3.el7.x86_64 &> /dev/null && action "docker安装完成!" || action "docker按照失败!请检查网络及Yum源!" false #配置镜像加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #重载配置文件 systemctl daemon-reload #启动服务 ${COLOR}"正在启动 Docker....."${END} sleep 2 systemctl start docker && action "docker启动成功!" || action "docker启动失败,请检查配置文件!" false } #ubuntu安装 install_ubuntu() { ${COLOR}"开始安装 Docker....."${END} #更新源及安装依赖包 apt-get update &> /dev/null for PAGE in ${ubuntu_page};do dpkg -s $PAGE &> /dev/null || apt -y install $PAGE &> /dev/null done #安装GPG证书 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - #写入软件源信息 echo 'deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable' >> /etc/apt/sources.list #更新源信息 apt -y update &> /dev/null #配置镜像加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #安装docker-ce ${COLOR}"正在安装Docker,请稍等...."${END} apt-get -y install docker-ce &> /dev/null && ${COLOR}"docker启动成功!"${END} || ${COLOR1}"docker启动失败,请检查配置文件!"${END} } #ubuntu安装harbor install_harbor_ubuntu(){ mulu=`pwd` myip=`hostname -I | awk '{print $1}'` harbor_passwd=123456 [ -d /apps ] || mkdir -p /apps [ -f /root/${harbor_version} ] || { ${COLOR1}"harbor安装包不存在,请检查!"${END}; exit; } [[ $mulu == "/root" ]] && tar xf $harbor_version -C /apps/ || { cd; tar xf $harbor_version -C /apps/ ; } #修改配置文件 sed -i "s#reg.mydomain.com#${myip}#" /apps/harbor/harbor.yml sed -i "s#Harbor12345#${harbor_passwd}#" /apps/harbor/harbor.yml sed -i "s/https:/#https/" /apps/harbor/harbor.yml sed -i "s/port: 443/#port: 443/" /apps/harbor/harbor.yml /apps/harbor/install.sh #配置服务文件 cat > /lib/systemd/system/harbor.service <<EOF [Unit] Description=Harbor After=docker.service systemd-networkd.service systemd-resolved.service Requires=docker.service Documentation=http://github.com/vmware/harbor [Service] Type=simple Restart=on-failure RestartSec=5 ExecStart=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml up ExecStop=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml down [Install] WantedBy=multi-user.target EOF #设置开机启动 systemctl daemon-reload systemctl enable harbar.service } #centos安装harbor install_harbor_centos(){ mulu=`pwd` myip=`hostname -I | awk '{print $1}'` harbor_passwd=123456 #创建harbor的目录 [ -d /apps ] || mkdir -p /apps [ -f /root/${harbor_version} ] || { ${COLOR1}"harbor安装包不存在,请检查!"${END}; exit; } [[ $mulu == "/root" ]] && tar xf $harbor_version -C /apps/ || { cd; tar xf $harbor_version -C /apps/ ; } #修改配置文件 sed -i "s#reg.mydomain.com#${myip}#" /apps/harbor/harbor.yml sed -i "s#Harbor12345#${harbor_passwd}#" /apps/harbor/harbor.yml sed -i "s/https:/#https/" /apps/harbor/harbor.yml sed -i "s/port: 443/#port: 443/" /apps/harbor/harbor.yml /apps/harbor/install.sh && ${COLOR}"harbor安装成功"$END || ${COLOR1}"harbor安装失败"$END #配置服务文件 cat > /lib/systemd/system/harbor.service <<EOF [Unit] Description=Harbor After=docker.service systemd-networkd.service systemd-resolved.service Requires=docker.service Documentation=http://github.com/vmware/harbor [Service] Type=simple Restart=on-failure RestartSec=5 ExecStart=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml up ExecStop=/usr/bin/docker-compose -f /apps/harbor/docker-compose.yml down [Install] WantedBy=multi-user.target EOF #设置开机启动 systemctl daemon-reload systemctl enable harbar.service } #安装docker-compose install_compose(){ #安装docker-compose,需实现安装python3-pip,利用pip库安装harbor rpm -q python3 &> /dev/null || yum -y install python3 rpm -q python3-pip &> /dev/null || yum -y install python3-pip pip3 install docker-compose -i https://mirrors.aliyun.com/pypi/simple ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose } #系统类型 ostype1=`awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release` ostype2=`awk -F'"' '/^NAME/{print $2}' /etc/os-release` if [[ $ostype2 == "CentOS Linux" ]];then if [ $ostype1 = 8 ];then rpm -q docker-ce &> /dev/null && action "Docker已安装" || install_centos8 elif [ $ostype1 = 7 ];then rpm -q docker-ce &> /dev/null && action "Docker已安装" || install_centos7 fi elif [[ $ostype2 == "Ubuntu" ]];then dpkg -s docker-ce &> /dev/null && ${COLOR}"Docker已安装"$END || install_ubuntu fi #判断compose是否安装 docker-compose --version && ${COLOR}"Docker Compose已安装"${END} || install_compose #harbor安装判断 if [[ $ostype2 == "Ubuntu" ]];then if [ -e /lib/systemd/system/harbar.service ];then ${COLOR}"harbor已安装"${END};exit else install_harbor_ubuntu && ${COLOR}"harbor安装完成,已开机启动!"${END} fi elif [[ $ostype2 == "CentOS Linux" ]];then if [ -e /lib/systemd/system/harbar.service ];then ${COLOR}"harbor已安装"${END};exit else install_harbor_centos fi fi