脚本安装docker

/

使用脚本在不同发行版linux 上安装 docker

脚本内容:

  1. # cat docker-install.sh
  1. #!/bin/bash
  2. DOCKER_VER="28.0.1"
  3. DOCKER_FILE="docker-${DOCKER_VER}.tgz"
  4. # "x86_64" or "aarch64"
  5. SYS_ARCH="x86_64"
  6. DOCKER_URL="https://download.docker.com/linux/static/stable/${SYS_ARCH}"
  7. SRC_DIR="/tools"
  8. ## Initialize the environment And Install Tools
  9. . /etc/os-release
  10. if grep -qs "ubuntu" /etc/os-release; then
  11. os="ubuntu"
  12. os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
  13. elif [[ -e /etc/debian_version ]]; then
  14. os="debian"
  15. os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
  16. elif egrep -i "openeuler|rocky|almalinux|anolis|centos|kylin" /etc/os-release &> /dev/null then
  17. os="centos"
  18. os_version=$(grep "VERSION_ID" /etc/os-release | awk -F'"' '{print $2}' | cut -d'.' -f1)
  19. elif [[ -e /etc/fedora-release ]]; then
  20. os="fedora"
  21. os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
  22. else
  23. echo "This installer seems to be running on an unsupported distribution.Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora."
  24. exit
  25. fi
  26. if [[ "$os" == "ubuntu" && "$os_version" -lt 2204 ]]; then
  27. echo "Ubuntu 22.04 or higher is required to use this installer.This version of Ubuntu is too old and unsupported."
  28. exit
  29. fi
  30. if [[ "$os" == "debian" ]]; then
  31. if grep -q '/sid' /etc/debian_version; then
  32. echo "Debian Testing and Debian Unstable are unsupported by this installer."
  33. exit
  34. fi
  35. if [[ "$os_version" -lt 11 ]]; then
  36. echo "Debian 11 or higher is required to use this installer.This version of Debian is too old and unsupported."
  37. exit
  38. fi
  39. fi
  40. if [[ "$os" == "centos" && "$os_version" -lt 8 ]]; then
  41. os_name=$(grep "^NAME" /etc/os-release | awk -F'"' '{print $2}')
  42. echo "$os_name 8 or higher is required to use this installer.This version of $os_name is too old and unsupported."
  43. exit
  44. fi
  45. if ! hash ufw 2>/dev/null && ! hash firewall-cmd 2>/dev/null && ! hash iptables 2>/dev/null; then
  46. if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
  47. apt -y update && apt install -y ufw
  48. [ $? -ne 0 ] && { echo "Install UFW Failed" ; exit; }
  49. ufw disable
  50. echo 'ufw is closed'
  51. else
  52. dnf makecache && dnf install -y firewalld
  53. [ $? -ne 0 ] && { echo "Install Firewalld Failed" ; exit; }
  54. setenforce 0
  55. sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
  56. echo 'Selinux has been disabled, and Permanent After Restarting'
  57. systemctl disable --now firewalld.service &>/dev/null
  58. echo 'firewalld is complete closed'
  59. fi
  60. fi
  61. if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
  62. apt -y update && apt install -y tar vim bash-completion
  63. [ $? -ne 0 ] && { echo "Install Tools Failed" ; exit; }
  64. apt remove -y docker docker-common docker-selinux docker-engine docer-io
  65. else
  66. dnf makecache && dnf install -y tar vim-enhanced bash-completion
  67. [ $? -ne 0 ] && { echo "Install Tools Failed" ; exit; }
  68. dnf remove -y docker docker-common docker-selinux docker-engine docer-io
  69. fi
  70. ## DownloadFiles
  71. [ ! -d ${SRC_DIR} ] && mkdir ${SRC_DIR}
  72. if [ -e ${SRC_DIR}/${DOCKER_FILE} ];then
  73. echo "The DockerFile is ready"
  74. else
  75. echo 'Start download Docker Package'
  76. curl ${DOCKER_URL}/${DOCKER_FILE} -o ${SRC_DIR}/${DOCKER_FILE}
  77. [ $? -ne 0 ] && { echo "Download ${DOCKER_FILE} Failed" ; exit; }
  78. fi
  79. cd ${SRC_DIR}
  80. tar -xvf ${DOCKER_FILE}
  81. cp -af docker/* /usr/bin/
  82. ## Service
  83. cat > /usr/lib/systemd/system/docker.service <<EOF
  84. [Unit]
  85. Description=Docker Application Container Engine
  86. Documentation=https://docs.docker.com
  87. # BindsTo=containerd.service
  88. # After=network-online.target firewalld.service containerd.service
  89. After=network-online.target firewalld.service
  90. Wants=network-online.target
  91. # Requires=docker.socket
  92. [Service]
  93. Type=notify
  94. # the default is not to use systemd for cgroups because the delegate issues still
  95. # exists and systemd currently does not support the cgroup feature set required
  96. # for containers run by docker
  97. # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  98. ExecStart=/usr/bin/dockerd
  99. ExecReload=/bin/kill -s HUP \$MAINPID
  100. TimeoutSec=0
  101. RestartSec=2
  102. Restart=always
  103. # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
  104. # Both the old, and new location are accepted by systemd 229 and up, so using the old location
  105. # to make them work for either version of systemd.
  106. StartLimitBurst=3
  107. # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
  108. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
  109. # this option work for either version of systemd.
  110. StartLimitInterval=60s
  111. # Having non-zero Limit*s causes performance problems due to accounting overhead
  112. # in the kernel. We recommend using cgroups to do container-local accounting.
  113. LimitNOFILE=infinity
  114. LimitNPROC=infinity
  115. LimitCORE=infinity
  116. # Comment TasksMax if your systemd version does not support it.
  117. # Only systemd 226 and above support this option.
  118. # TasksMax=infinity
  119. # set delegate yes so that systemd does not reset the cgroups of docker containers
  120. Delegate=yes
  121. # kill only the docker process, not all processes in the cgroup
  122. KillMode=process
  123. [Install]
  124. WantedBy=multi-user.target
  125. EOF
  126. mkdir /etc/docker/ /data/docker -pv
  127. cat > /etc/docker/daemon.json <<EOF
  128. {
  129. "data-root": "/data/docker",
  130. "storage-driver": "overlay2",
  131. "live-restore": true
  132. }
  133. EOF
  134. ## Start Service
  135. systemctl daemon-reload
  136. systemctl enable --now docker.service
  137. systemctl status docker.service

执行脚本

  1. bash installdocker.sh

转载请注明作者和出处,并添加本页链接。
原文链接: //www.wwtou.com/rd0a4xDs.html