注解:一键安装docker单环境脚本,支持自定义版本,支持自定义版本升级;
#!/bin/bash

#确保网络连通
wangluo(){

ping -c 2 -w 3 www.baidu.com  &>/dev/null
if [ $? -eq 0 ]; then
        echo "已连通外网,执行下步操作"
    else
        echo "外网不通,正在退出"
        exit 1
fi

}
wangluo #检查网络连通性,不需要时将此禁用即可

#获取系统版本信息
Arch=$(arch)

#选择需要下载的版本
curl -s https://download.docker.com/linux/static/stable/$Arch/ | tail -n +11 | head -n -1  | awk -F "href=" '{print $2}' | awk -F ">" '{print $1}'  | sed 's/"//g'

#tgz包名称
read -ep "请选择您需要安装的版本:"  tgz
#二进制所在文件
Erjinzhi="/usr/bin/"
#创建的用户组
user="docker"
#二进制包解压所在文件
PWD="/opt"


#修改时区为上海
echo "修改时区,并同步时间:timedatectl set-timezone Asia/Shanghai"
timedatectl set-timezone Asia/Shanghai

#修改selinux
echo "关闭selinux"
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config   #s/^ 以哪个开头 /改成什么/
setenforce 0

#关闭防火墙
echo "关闭防火墙,并禁止开机自启动:systemctl stop  firewalld | systemctl  disable  firewalld"
systemctl stop  firewalld
systemctl  disable  firewalld


#停止docker服务
systemctl stop docker.service
systemctl stop docker.socket
systemctl stop containerd.service

#下载插件
yum  -y install wget  ntp

#下载所选docker版本
wget -c https://download.docker.com/linux/static/stable/$Arch/$tgz


#解压文件
tar -zxvf $tgz  -C $PWD/
#改变权限
chown root:root $PWD/docker/* 
#复制文件到指定位置
\cp -p $PWD/docker/* $Erjinzhi 
#创建用户和组
groupadd $user
#检查是否生效
docker version | grep Version | head -n 1
if [ $? -ne 0 ]; then
    echo "docker 二进制包复制失败"
    exit 1
fi

#添加对应的服务文件
#####{docker.socket}#####
tee /usr/lib/systemd/system/docker.socket <<EOF
[Unit]
Description=Docker Socket for the API

[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target
EOF

#####{containerd.service}#####
tee /usr/lib/systemd/system/containerd.service <<EOF
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity

# Comment TasksMax if your systemd version does not supports it.

# Only systemd 226 and above support this version.

TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target
EOF

#####{docker.service}#####
tee /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

StartLimitBurst=3

StartLimitInterval=60s

LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

TasksMax=infinity

Delegate=yes

KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
EOF

#定时清理日志
mkdir /etc/docker
touch /etc/docker/daemon.json
cat << EOF > /etc/docker/daemon.json
{
"log-opts": {
    "max-size": "10m",
    "max-file": "3"
    }
}
EOF


#启用 dockerd 服务
systemctl enable docker.service
if [ $? -ne 0 ]; then
    echo "docker 服务加入开机自启失败"
    exit 1
fi

#查看状态
systemctl is-enabled docker.socket docker.service containerd.service  | sed ":a;N;s#\n#,#g;ta" 

#运行docker
systemctl enable --now docker.service 
if [ $? -ne 0 ]; then
    echo "docker 服务启动失败"
    exit 1
fi

#查看docker是否启动
systemctl  status docker | egrep -i  "running"
if [ $? -eq 0 ];then   #$? 返回值 -eq 等于  0 数值也可以是字符   如果返回值等于0那么执行成功,反之执行失败!
    echo -e "\033[42;37m docker服务启动成功 \033[0m" 
else
    echo -e "\033[31m docker服务检测出现异常 \033[0m" 
    exit 1
fi



#同步时间
ntpdate ntp1.aliyun.com  
echo "已同步阿里服务器时间!"

echo  "安装完成请查收!"