代码:
#!/bin/bash
SYSTEMD_PATH=/usr/lib/systemd/system/docker.service
DOCKER_FILE=docker-20.10.23.tgz
DOCKER_COMPOSE_FILE=docker-compose-plugin-2.15.1-3.el8.x86_64.rpm
RED='\E[1;31m'
GREEN='\E[1;32m'
YELOW='\E[1;33m'
SHAN='\E[1;31;5m'
RES='\E[0m'
function install_docker_system
{
if ! which docker;then
echo -e "${RED}[+]No docker command, Begin install docker...${RES}"
#cd ./deploy
tar -zxvf $DOCKER_FILE
cp docker/* /usr/bin/
fi
if [ ! -f "$SYSTEMD_PATH" ];then
>$SYSTEMD_PATH cat <<EOF
[Unit]
Description=Docker service
Requires=network.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/dockerd
StartLimitBurst=3000
PIDfile=/var/run/docker.pid
Restart=always
[Install]
WantedBy=basic.target
EOF
chmod +x $SYSTEMD_PATH
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
sleep 3s
fi
docker ps > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "${GREEN}[+]Docker install successed!${RES}"
fi
if ! which docker-compose;then
echo -e "${RED}[+]No docker-compose command,Begin install docker-compose...${RES}"
rpm -ivh $DOCKER_COMPOSE_FILE
( echo '#!/bin/bash'; echo 'docker compose $@' ) > /bin/docker-compose
chmod 755 /bin/docker-compose
fi
which docker-compose
if [ $? -eq 0 ];then
echo -e "${GREEN}[+]Docker-compose install successed!${RES}"
fi
}
function uninstall_docker_system
{
echo "Stop docker systemd..."
systemctl stop docker
for i in `ls docker`;do rm -v /usr/bin/"$i";done
rm -v $SYSTEMD_PATH
echo -e "${GREEN}[+]Uninstall docker complete!${RES}"
for w in `rpm -qa | grep docker-compose`;do rpm -e --nodeps $w;done
rm -v /usr/bin/docker-compose
echo -e "${GREEN}[+]Uninstall docker-compose complete!${RES}"
}
case "$1" in
install)
install_docker_system
;;
uninstall)
uninstall_docker_system
esac
exit 0
其中往docker.service写systemd管理的内容也可以替换成如下内容:
if [ ! -f "$SYSTEMD_PATH" ];then
echo """
[Unit]
Description=Docker service
Requires=network.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/dockerd
StartLimitBurst=3000
PIDfile=/var/run/docker.pid
Restart=always
[Install]
WantedBy=basic.target
""" > $SYSTEMD_PATH
运行结果: