centos7 安装docker 并配置仓库源地址 shell 脚本

2021-05-11 10:27:58 浏览数 (1)

代码语言:javascript复制
#!/bin/bash
repo="/etc/yum.repos.d/epel-7.repo"
if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录
yum install -y wget
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
                                        	# 移动两个文件到 目录
#wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
#sed -i  's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
                                        	# yum源没有对应版本包匹配;修改yum为centos7
yum clean all                           # 清除原有缓存
yum makecache                      # 生成新的缓存
                                        	# make cache [me?k]  [ka?]  制作 缓存{ka xi}
yum  list                               	# 校验yum
cd
else 
echo "yum已经切换"
fi

DockerInstall(){
dci=`rpm -qa | grep docker | wc -l`
if [ $dci -lt 1 ]
  then 
      echo "正在安装docker =======》"
yum -y remove docker docker-common docker-selinux docker-engine
						# 卸载旧版本docker
yum install -y yum-utils device-mapper-persistent-data lvm2
						# 配置yum 源码
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
						# 添加docker yum 源码
yum list docker-ce --showduplicates | sort -r
						# 查看docker 版本
cat /dev/null > /etc/docker/daemon.json
                        # 先清空文件内容重新写入
cat > /etc/docker/daemon.json << EOF
{
"insecure-registries": ["registry.local", "127.0.0.1:5001", "10.10.13.42:5000"],
  "registry-mirrors": ["http://mirror.local"],
  "bip": "172.18.18.1/24",
  "data-root": "/var/lib/docker",
  "storage-driver": "overlay2",
  "live-restore": true,
  "log-opts": {
    "max-size": "500m"
  }
}
EOF
# 文件中不需要IPv6
# 配置镜像加速器
yum install -y docker-ce		# 默认安装最新版本 docker
# yum install  docker-ce-<VERSION_STRING> (指定安装版本) 
# yum install docker-ce-18.03.1.ce
systemctl start docker  		# 启动docker
systemctl enable docker   	# 开机启动
docker version  			# 查看docker版本号
docker run hello-world		# 启动一个docker 验证是否安装成功

  else
     echo "已经成功安装docker"
fi
}
DockerInstall

dockerCangku(){

echo "
 {
        "registry-mirrors": ["https://registry.docker-cn.com"]
  }
" >> /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart   docker.service
systemctl status docker -l
docker info
docker pull centos:7
                           # 拉取centos7 镜像
}
dockerCangku


# wget eisc.cn/file/shell/docker-install.sh ; sh docker-install.sh

0 人点赞