shell脚本离线安装docker

117 阅读1分钟

查看linux内核版本

Docker要求操作系统的内核版本不低于3.10。 在CentOS上,我们可以通过查看当前内核版本来确定是否满足要求。 uname -r 1. 该命令将返回当前系统的内核版本。 如果返回的版本号大于等于3.10,则满足Docker的内核版本要求。

#!/bin/bash

package_path="/usr/local/docker"

docker_inatall_name="docker-20.10.22.tgz"

cd $package_path

# 安装docker
if command -v docker &>/dev/null; then
	echo "----------Docker 已经安装----------"
else

	echo "----------开始安装docker----------"
	cd $package_path
	tar -xvf $docker_inatall_name
	if [ $? -ne 0 ]; then
		echo "Error: Failed to 解压安装包失败 package"
		exit 1
	fi
	cp docker/* /usr/bin/
	if [ $? -ne 0 ]; then
		echo "Error: Failed to copy docker文件失败"
		exit 1
	fi
	echo "正在配置docker服务"
	# 创建服务文件
	cat >/etc/systemd/system/docker.service <<EOF
[Unit]
 Description=Docker Application Container Engine
 Documentation=https://docs.docker.com
 After=network-online.target firewalld.service
 Wants=network-online.target
 [Service]
 Type=notify
 # the default is not to use systemd for cgroups because the delegate issues still
 # exists and systemd currently does not support the cgroup feature set required
 # for containers run by docker
 ExecStart=/usr/bin/dockerd
 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 the docker process if it exits prematurely
 Restart=on-failure
 StartLimitBurst=3
 StartLimitInterval=60s
 [Install]
 WantedBy=multi-user.target

EOF

	echo "----------设置docker存储目录----------"

	if [ ! -d "/topevery/dockerData" ]; then
		echo "----------创建docker存储目录----------"
		mkdir -p /topevery/dockerData
	fi

	if [ ! -d "/etc/docker" ]; then
		echo "----------创建docker存储配置文件----------"
		mkdir -p /etc/docker
	fi
	cat >/etc/docker/daemon.json <<EOF
{
  "data-root": "/topevery/dockerData"
}
EOF

	echo "----------注册docker服务----------"
	chmod +x /etc/systemd/system/docker.service
	systemctl daemon-reload
	echo "----------启动docker服务----------"
	systemctl start docker.service
	echo "----------docker安装完毕----------"

fi

要给shell脚本执行权限,然后执行脚本就行