当需要对多台机器安装部署Docker时,面对这种重复性的操作,可以使用脚本的方法来快速安装。
这是本人在学习过程中写的笔记,恳请大家多多指教。
第一步,编辑脚本(具体内容请参考下文):vim docker-install.sh
#! /bin/bash
#脚本安装Docker
#作者:dorte
#时间:2021.9.18
#Email:1615360614qq.com
#优化环境,避免出现一些不必要的问题
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config &> /dev/null
setenforce 0
#停止防火墙,设置开机不启动防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
#清除规则
iptables -F
#停止NetworkManager,设置它开机不启动
systemctl stop NetworkManager &> /dev/null
systemctl disable NetworkManager &> /dev/null
#配置aliyun网络源
cat > /etc/yum.repos.d/aliyun.repo <<STOP
[aliyun-os]
name=aliyun-os
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
[aliyun-epel]
name=aliyun-epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
[aliyun-extra]
name=aliyun-extra
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
enabled=1
gpgcheck=0
STOP
#重建缓存
yum clean all && yum makecache
#添加epel源
yum install -y epel-release
#添加docker-ce源
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat /etc/yum.repos.d/docker-ce.repo
yum clean all && yum makecache
#同步时间
unalias cp #取消cp的别名,让其不进行确认提示,临时的,重新开机后还是会恢复
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#配置pip镜像源,以便快速下载Python库
mkdir /.pip
cat > /.pip/pip.conf <<STOP
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
STOP
#安装Docker
#卸载旧版本Docker,以免引起不兼容
yum remove docker docker-io docker-selinux python-docker-py -y
#安装Docker-CE社区版本
yum install docker-ce -y
systemctl start docker
systemctl enable docker
systemctl status docekr
docker --version
#指定docker镜像加速器,此处指定网易的
cat > /etc/docker/daemon.json <<STOP
{
"registry-mirrors":["https://hub-mirror.c.163.com"]
}
STOP
#重启相关服务,修改了脚本要执行重载操作
systemctl daemon-reload
#重启Docker,查看状态
systemctl restart docker
systemctl status docker
#查看版本信息
docker --version
#查看Docker的详细信息
docker info
#安装Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#给运行权限docker-compose
chmod +x /usr/local/bin/docker-compose
#查看Docker Compose版本信息
docker-compose -version
#尝试拉取一个centos:7的镜像
docker image pull centos:7
#查看当前已有镜像
docker images
第二步,给予编辑好的脚本运行权限:chmod +755 docker-install.sh
第三步,执行脚本:./docker-install.sh
至此,实现用脚本安装部署Dcoker的操作。