文章目录

  • 1. 功能介绍
  • 2. 代码详解
  • 3. 使用图例


1. 功能介绍

一键安装Docker容器服务,支持自定义版本下在线或离线安装。

备注

  • 第一个参数选择安装模式:[offline(离线安装)/online(在线安装)/remove(清理环境)/help(参数介绍)],第二个参数为下载版本;
  • 若需要使用离线下载,则需提前将安装包上传至Linux服务器内,存放路径推荐与脚本一致
  • 若需要修改默认安装版本,则更改代码内第19行${2:-24.0.2}24.0.2为自定义版本即可;
  • 安装包下载链接:Docker容器服务安装包下载
  • 安装路径:/root/docker_install_pkg/,使用remove参数可以清理此目录

2. 代码详解

#!/bin/bash

# 脚本参数提示
function help_info(){
    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+-----------------------+---------------------------------------+\033[0m"
}

# 安装其他docker版本
function download_other_version(){
	cd /root/docker_install_pkg/
	# 若第2个参数不做自定义版本要求,则默认使用24.0.2版本安装包
	docker_version=${2:-24.0.2}
	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
	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
}

# 安装执行
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/
		else
			echo -e "\033[31m环境清理完成,无需重复执行!\033[0m"
		fi
		;;
	* | help )
		# 帮助
		help_info
		;;
	esac
else
	echo -e "\033[31m当前执行用户为非root用户,推荐使用root用户执行安装操作!\033[0m"
fi

3. 使用图例

下图为选择 在线安装默认版本 截图:

Shell ❀ 脚本实现Docker容器服务一键安装_容器