代码:
#!/bin/bashSYSTEMD_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;thenecho -e "${RED}[+]No docker command, Begin install docker...${RES}"#cd ./deploytar -zxvf $DOCKER_FILEcp docker/* /usr/bin/fiif [ ! -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
EOFchmod +x $SYSTEMD_PATHsystemctl daemon-reloadsystemctl restart dockersystemctl enable dockersleep 3sfidocker ps > /dev/null 2>&1if [ $? -eq 0 ];thenecho -e "${GREEN}[+]Docker install successed!${RES}"fiif ! which docker-compose;thenecho -e "${RED}[+]No docker-compose command,Begin install docker-compose...${RES}"rpm -ivh $DOCKER_COMPOSE_FILE( echo '#!/bin/bash'; echo 'docker compose $@' ) > /bin/docker-composechmod 755 /bin/docker-composefiwhich docker-composeif [ $? -eq 0 ];thenecho -e "${GREEN}[+]Docker-compose install successed!${RES}"fi
}
function uninstall_docker_system
{echo "Stop docker systemd..."systemctl stop dockerfor i in `ls docker`;do rm -v /usr/bin/"$i";donerm -v $SYSTEMD_PATHecho -e "${GREEN}[+]Uninstall docker complete!${RES}"for w in `rpm -qa | grep docker-compose`;do rpm -e --nodeps $w;donerm -v /usr/bin/docker-composeecho -e "${GREEN}[+]Uninstall docker-compose complete!${RES}"
}
case "$1" ininstall)install_docker_system;;uninstall)uninstall_docker_system
esac
exit 0
其中往docker.service写systemd管理的内容也可以替换成如下内容:
if [ ! -f "$SYSTEMD_PATH" ];thenecho """
[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
运行结果: