admin 管理员组

文章数量: 887021

脚本代码

文件名字为:docker.sh
#!/bin/sh
# 脚本环境在:centos7.x 以上
function install() {
  echo "检查Docker是否已安装……"
  docker -v
  if [ $? -eq  0 ]; then
  	echo "已安装!执行退出";
    exit 1
  fi
  echo "==========开始安装docker=============";
  #结束yum 命令 如果有正在执行的安装进程,先结束
  pkill -9 yum
  #通过阿里云脚本安装
  #curl -fsSL https://get.docker | bash -s docker --mirror Aliyun
  #国内daocloud 安装,,速度比较快些
  curl -sSL https://get.daocloud.io/docker | sh
  echo "==========安装docker结束=============";
  echo "==========验证是否安装成功=============";
  docker -v
  if [ $? -ne  0 ]; then
    echo "安装失败!结束安装!";
    exit 1
  fi
  echo "==========docker安装成功=============";
  echo "==========启动服务|开机启动=============";
  systemctl start docker
  systemctl enable docker
  echo "==========配置镜像加速=============";
  mkdir -p /etc/docker
  echo '{ "registry-mirrors": [ "https://w0epqs17.mirror.aliyuncs" ] }' >> /etc/docker/daemon.json
  systemctl daemon-reload
  #重启
  systemctl restart docker
  echo "==========Docker-compose安装=============";
  docker-compose -v
  if [ $? -eq  0 ]; then
    echo "已经安装Docker-compose!";
    exit 1
  fi
  yum -y install python-pip
  yum -y install docker-compose
  docker-compose -v
  if [ $? -eq  0 ]; then
    echo "安装Docker-compose!完成";
  else
    echo "安装Docker-compose!失败了~~~~";
  fi
}
#卸载
function uninstall() {
  echo "检查Docker是否已安装……"
  docker -v
  if [ $? -ne  0 ]; then
  	echo "================未安装!卸载结束================";
    exit 1
  fi
  echo "==========docker开始卸载=============";
  yum -y remove docker docker-client docker-client-latest docker-common  docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
  echo "==========清理配置=============";
  rm -rf /etc/systemd/system/docker.service.d
  rm -rf /var/lib/docker
  rm -rf /var/run/docker
  #查看安装的
  #yum list installed |grep docker
  text=`yum list installed |grep docker |awk '{print $1}'`
  for element in $text
  do
    echo "卸载$element"
    #卸载docker相关的工具
    yum -y remove $element
  done
  echo "==========卸载完毕=============";
}
#重装
function reinstall() {
    echo "检查Docker是否已安装……"
    docker -v
    if [ $? -ne  0 ]; then
      echo "================未安装!卸载结束================";
    else
       #卸载
       uninstall
    fi
    #安装
    install
}
#使用方法
function usage()
{
    echo "请使用这些命令: $0 {install|uninstall|reinstall}"
    echo "例如: $0 install "
    exit 1
}
function help()
{
    echo "install :安装"
    echo "uninstall :卸载"
    echo "reinstall :重装"
    exit 1
}

case $1 in
	install)
	install;;

	uninstall)
	uninstall;;

  reinstall)
	reinstall;;

  help)
	help;;
 -help)
	help;;
  -h)
	help;;
  h)
	help;;

	*)
	usage;;
esac

运行

新建为docker.sh 放在服务器上
授权 chmod 775 docker.sh
执行命令:

安装:

./docker.sh install

卸载(慎重):

./docker.sh uninstall

重装(慎重):

./docker.sh reinstall

检查

是否安装成功

docker version
docker-compose --version

本文标签: 一键 重装 脚本 shell docker