一键安装docker脚本

Just Do It
2025-03-08 / 0 评论 / 29 阅读 / 正在检测是否收录...

#!/bin/bash

# 脚本参数提示
function help_info(){
    
        echo -e "\033[32m\t\t\t请在脚本后面输入参数\t\t\t\t\033[0m"
	echo -e "\033[32m+-----------------------+---------------------------------------+\033[0m"
	echo -e "\033[32m|\t参数\t\t|\t\t详解\t\t\t|\033[0m"
	echo -e "\033[32m+-----------------------+---------------------------------------+\033[0m"
	echo -e "\033[32m|\toffline\t\t|\t离线安装\t\t\t|\033[0m"
	echo -e "\033[32m|\tonline\t\t|\t在线安装(需要机器访问外网)\t|\033[0m"
	echo -e "\033[32m|\tremove\t\t|\t清理环境\t\t\t|\033[0m"
	echo -e "\033[32m|\thelp\t\t|\t帮助信息\t\t\t|\033[0m"
	echo -e "\033[32m|\tuninstall\t|\t卸载docker\t\t\t|\033[0m"
	echo -e "\033[32m+-----------------------+---------------------------------------+\033[0m"
}

# 安装其他docker版本
function download_other_version(){
	cd /root/docker_install_pkg/
	# 若第2个参数不做自定义版本要求,则默认使用24.0.2版本安装包
	docker_version=${2:-24.0.2}
        
     while true; do
        wget https://download.docker.com/linux/static/stable/x86_64/docker-$docker_version.tgz &> /dev/null

        if [ $? -eq 0 ]; then
           echo -e "\033[32m下载Docker离线安装包为:docker-$docker_version.tgz,包存放路径为:/root/docker_install_pkg/\033[0m"

          break
        fi
     done



#	wget https://download.docker.com/linux/static/stable/x86_64/docker-$docker_version.tgz >> /dev/null
#	echo -e "\033[32m下载Docker离线安装包为:docker-$docker_version.tgz,包存放路径为:/root/docker_install_pkg/\033[0m"
}

#
function find_pkg(){
	cp -a $(find / -name 'docker-24.0.2.tgz' | grep -i docker) /root/docker_install_pkg/
}

# 离线安装配置
function install_service(){
	cd /root/docker_install_pkg/
	tar -zxf docker*.tgz
	cp -p docker/* /usr/bin/
	cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target docker.socket

[Service]
Type=notify
EnvironmentFile=-/run/flannel/docker
WorkingDirectory=/usr/local/bin
ExecStart=/usr/bin/dockerd \
                -H tcp://0.0.0.0:4243 \
                -H unix:///var/run/docker.sock \
                --selinux-enabled=false \
                --log-opt max-size=1g
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
mkdir -p /etc/docker
	cat  > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": [
    "https://2a6bf1988cb6428c877f723ec7530dbc.mirror.swr.myhuaweicloud.com",
    "https://docker.m.daocloud.io",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://your_preferred_mirror",
    "https://dockerhub.icu",
    "https://docker.registry.cyou",
    "https://docker-cf.registry.cyou",
    "https://dockercf.jsdelivr.fyi",
    "https://docker.jsdelivr.fyi",
    "https://dockertest.jsdelivr.fyi",
    "https://mirror.aliyuncs.com",
    "https://dockerproxy.com",
    "https://mirror.baidubce.com",
    "https://docker.m.daocloud.io",
    "https://docker.nju.edu.cn",
    "https://docker.mirrors.sjtug.sjtu.edu.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://mirror.iscas.ac.cn",
    "https://docker.rainbond.cc"
    ]
}
EOF
	systemctl daemon-reload
	systemctl start docker
	systemctl enable docker >> /dev/null
	echo -e "\033[32mDocker服务启动完成,并设置为开机自启!\033[0m"
}

function service_alive(){
	systemctl is-active docker >> /dev/null
	if [ $? -eq 0 ];then
		echo -e "\033[31mDocker已经存在,无需安装!\033[0m"
	fi
}
#卸载docker
function uninstall_docker(){
                 docker stop $(docker ps -aq) &> /dev/null
                 docker rm $(docker ps -aq)  &> /dev/null
                 docker rmi $(docker images -q)  &> /dev/null
                 systemctl stop  docker
                 rm -rf /etc/systemd/system/docker.service /usr/bin/containerd /usr/bin/containerd-shim /usr/bin/ctr /usr/bin/runc  /usr/bin/docker* /etc/docker/ /var/lib/docker
                 echo -e "\033[32mDocker卸载完成!\033[0m"

}
# 安装执行
if [ `whoami` = 'root' ];then
	# 安装目录
	mkdir -pv /root/docker_install_pkg/ >> /dev/null
	case $1 in
	offline )
		service_alive
		# 离线安装
		find_pkg && install_service
		;;
	online )
		service_alive
		# 在线安装
		download_other_version && install_service
		;;
	remove )
		# 清理环境
		if [ -d /root/docker_install_pkg/ ];then
			rm -r /root/docker_install_pkg/
                        echo -e "\033[32m环境清理完成!\033[0m"
		else
			echo -e "\033[31m环境清理完成,无需重复执行!\033[0m"
		fi
		;;
        uninstall )
                # 卸载docker
                 uninstall_docker
                 ;;
	* | help )
		# 帮助
		help_info
		;;
	esac
else
	echo -e "\033[31m当前执行用户为非root用户,推荐使用root用户执行安装操作!\033[0m"
fi

0

评论 (0)

取消