shell 脚本安装docker服务
本次安装:
docker-20.10.9.tgz
安装包下载地址:https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz
1、创建文件夹
mkdir -p /data/docker/data
2、上传安装文件到/data/docker下
3、编辑安装脚本
vi /data/docker/install_docker-service.sh
【
#!/bin/bash
#上传安装包到/data/docker目录下
cd /data/docker
#解压安装包到/usr/local目录下
tar -zxvf docker-20.10.9.tgz -C /usr/local
#先删除再插入新的配置文件
rm -rf /etc/systemd/system/docker.service
#编辑配置文件
#/etc/systemd/system/docker.service
echo "[Unit]" >> /etc/systemd/system/docker.service
echo "Description=Docker Application Container Engine" >> /etc/systemd/system/docker.service
echo "Documentation=https://docs.docker.com" >> /etc/systemd/system/docker.service
echo "After=network-online.target firewalld.service" >> /etc/systemd/system/docker.service
echo "Wants=network-online.target" >> /etc/systemd/system/docker.service
echo "[Service]" >> /etc/systemd/system/docker.service
echo "Type=notify" >> /etc/systemd/system/docker.service
echo "Environment=PATH=/usr/local/docker:/usr/bin:/usr/sbin" >> /etc/systemd/system/docker.service
echo "ExecStart=/usr/local/docker/dockerd" >> /etc/systemd/system/docker.service
echo "ExecReload=/bin/kill -s HUP $MAINPID" >> /etc/systemd/system/docker.service
echo "LimitNOFILE=infinity" >> /etc/systemd/system/docker.service
echo "LimitNPROC=infinity" >> /etc/systemd/system/docker.service
echo "LimitCORE=infinity" >> /etc/systemd/system/docker.service
echo "TimeoutStartSec=0" >> /etc/systemd/system/docker.service
echo "Delegate=yes" >> /etc/systemd/system/docker.service
echo "KillMode=process" >> /etc/systemd/system/docker.service
echo "Restart=on-failure" >> /etc/systemd/system/docker.service
echo "StartLimitBurst=3" >> /etc/systemd/system/docker.service
echo "StartLimitInterval=60s" >> /etc/systemd/system/docker.service
echo "[Install]" >> /etc/systemd/system/docker.service
echo "WantedBy=multi-user.target" >> /etc/systemd/system/docker.service
#修改环境变量
#echo “export PATH=$PATH:/usr/local/docker” >> /etc/profile
sed -i '$aexport PATH=$PATH:/usr/local/docker' /etc/profile
source /etc/profile
#赋给docker.service文件添加执行权限
chmod 755 /etc/systemd/system/docker.service
#修改/usr/local/docker的所属者
chown -R root:root /usr/local/docker
#重新加载配置文件
systemctl daemon-reload
#休眠3秒,否者启动失败,因为systemctl daemon-reload命令没有执行完成,马上执行启动服务会报错
sleep 3
#启动服务
systemctl start docker
#先删除再插入新的配置文件
rm -rf /etc/docker/daemon.json
#修改docker文件存放路径
touch /etc/docker/daemon.json
echo '{"data-root": "/data/docker/data"}' >> /etc/docker/daemon.json
#重新加载配置文件
systemctl daemon-reload
#休眠3秒,否者启动失败,因为systemctl daemon-reload命令没有执行完成,马上执行启动服务会报错
sleep 3
#重新启动服务
systemctl restart docker
】
4、修改 /data/docker/install_docker-service.sh的执行权限
chmod +x /data/docker/install_docker-service.sh
5、验证
source /data/docker/install_docker-service.sh
安装成功!!!