脚本安装docker
使用脚本在不同发行版linux 上安装 docker
脚本内容:
# cat docker-install.sh
#!/bin/bash
DOCKER_VER="28.0.1"
DOCKER_FILE="docker-${DOCKER_VER}.tgz"
# "x86_64" or "aarch64"
SYS_ARCH="x86_64"
DOCKER_URL="https://download.docker.com/linux/static/stable/${SYS_ARCH}"
SRC_DIR="/tools"
## Initialize the environment And Install Tools
. /etc/os-release
if grep -qs "ubuntu" /etc/os-release; then
os="ubuntu"
os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
elif [[ -e /etc/debian_version ]]; then
os="debian"
os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
elif egrep -i "openeuler|rocky|almalinux|anolis|centos|kylin" /etc/os-release &> /dev/null; then
os="centos"
os_version=$(grep "VERSION_ID" /etc/os-release | awk -F'"' '{print $2}' | cut -d'.' -f1)
elif [[ -e /etc/fedora-release ]]; then
os="fedora"
os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
else
echo "This installer seems to be running on an unsupported distribution.Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora."
exit
fi
if [[ "$os" == "ubuntu" && "$os_version" -lt 2204 ]]; then
echo "Ubuntu 22.04 or higher is required to use this installer.This version of Ubuntu is too old and unsupported."
exit
fi
if [[ "$os" == "debian" ]]; then
if grep -q '/sid' /etc/debian_version; then
echo "Debian Testing and Debian Unstable are unsupported by this installer."
exit
fi
if [[ "$os_version" -lt 11 ]]; then
echo "Debian 11 or higher is required to use this installer.This version of Debian is too old and unsupported."
exit
fi
fi
if [[ "$os" == "centos" && "$os_version" -lt 8 ]]; then
os_name=$(grep "^NAME" /etc/os-release | awk -F'"' '{print $2}')
echo "$os_name 8 or higher is required to use this installer.This version of $os_name is too old and unsupported."
exit
fi
if ! hash ufw 2>/dev/null && ! hash firewall-cmd 2>/dev/null && ! hash iptables 2>/dev/null; then
if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
apt -y update && apt install -y ufw
[ $? -ne 0 ] && { echo "Install UFW Failed" ; exit; }
ufw disable
echo 'ufw is closed'
else
dnf makecache && dnf install -y firewalld
[ $? -ne 0 ] && { echo "Install Firewalld Failed" ; exit; }
setenforce 0
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
echo 'Selinux has been disabled, and Permanent After Restarting'
systemctl disable --now firewalld.service &>/dev/null
echo 'firewalld is complete closed'
fi
fi
if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
apt -y update && apt install -y tar vim bash-completion
[ $? -ne 0 ] && { echo "Install Tools Failed" ; exit; }
apt remove -y docker docker-common docker-selinux docker-engine docer-io
else
dnf makecache && dnf install -y tar vim-enhanced bash-completion
[ $? -ne 0 ] && { echo "Install Tools Failed" ; exit; }
dnf remove -y docker docker-common docker-selinux docker-engine docer-io
fi
## DownloadFiles
[ ! -d ${SRC_DIR} ] && mkdir ${SRC_DIR}
if [ -e ${SRC_DIR}/${DOCKER_FILE} ];then
echo "The DockerFile is ready"
else
echo 'Start download Docker Package'
curl ${DOCKER_URL}/${DOCKER_FILE} -o ${SRC_DIR}/${DOCKER_FILE}
[ $? -ne 0 ] && { echo "Download ${DOCKER_FILE} Failed" ; exit; }
fi
cd ${SRC_DIR}
tar -xvf ${DOCKER_FILE}
cp -af docker/* /usr/bin/
## Service
cat > /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
# BindsTo=containerd.service
# After=network-online.target firewalld.service containerd.service
After=network-online.target firewalld.service
Wants=network-online.target
# Requires=docker.socket
[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 -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# 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
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
# TasksMax=infinity
# 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
[Install]
WantedBy=multi-user.target
EOF
mkdir /etc/docker/ /data/docker -pv
cat > /etc/docker/daemon.json <<EOF
{
"data-root": "/data/docker",
"storage-driver": "overlay2",
"live-restore": true
}
EOF
## Start Service
systemctl daemon-reload
systemctl enable --now docker.service
systemctl status docker.service
执行脚本
bash installdocker.sh
转载请注明作者和出处,并添加本页链接。
原文链接:
//www.wwtou.com/rd0a4xDs.html