#!/bin/bash
# Date:2024-02-28
# docker安装后优化及配置

dockerinstall() {
    rpm -ivh docker-compose-plugin-2.24.6-1.el8.x86_64.rpm
    rpm -ivh fuse3-libs-3.3.0-17.el8.x86_64.rpm
    rpm -ivh docker-buildx-plugin-0.12.1-1.el8.x86_64.rpm
    rpm -ivh docker-ce-cli-25.0.3-1.el8.x86_64.rpm
    rpm -ivh python3-setools-4.3.0-5.el8.x86_64.rpm
    rpm -ivh python3-libsemanage-2.9-9.el8_6.x86_64.rpm
    rpm -ivh python3-audit-3.0.7-5.el8.x86_64.rpm
    rpm -ivh libcgroup-0.41-19.el8.x86_64.rpm
    rpm -ivh fuse-common-3.3.0-17.el8.x86_64.rpm
    rpm -ivh fuse3-3.3.0-17.el8.x86_64.rpm
    rpm -ivh fuse-overlayfs-1.12-1.module+el8.9.0+1703+29de406e.x86_64.rpm
    rpm -ivh checkpolicy-2.9-1.el8.x86_64.rpm
    rpm -ivh python3-policycoreutils-2.9-24.el8.noarch.rpm
    rpm -ivh policycoreutils-python-utils-2.9-24.el8.noarch.rpm
    rpm -ivh container-selinux-2.221.0-1.module+el8.9.0+1703+29de406e.noarch.rpm
    rpm -ivh containerd.io-1.6.28-3.1.el8.x86_64.rpm
    rpm -ivh libslirp-4.4.0-1.module+el8.9.0+1703+29de406e.x86_64.rpm
    rpm -ivh slirp4netns-1.2.1-1.module+el8.9.0+1703+29de406e.x86_64.rpm
    rpm -ivh docker-ce-25.0.3-1.el8.x86_64.rpm
    rpm -ivh docker-ce-rootless-extras-25.0.3-1.el8.x86_64.rpm

    sleep 3

    cat << EOF > /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF

    chmod +x /etc/systemd/system/docker.service
    systemctl daemon-reload
    systemctl enable docker.service
    systemctl start docker

    modprobe overlay
    modprobe br_netfilter

    echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
    echo '1' > /proc/sys/net/bridge/bridge-nf-call-ip6tables
    echo '1' > /proc/sys/net/ipv4/ip_forward

    cat <<EOF > /etc/docker/daemon.json
{
  "bip": "${DOCKER0_NETWORK}",
  "default-address-pools": [{"base": "${DOCKER_GWBRIDGE_NETWORK}", "size":24}],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "max-concurrent-downloads": 25,
  "log-driver": "json-file",
  "log-opts": {
  "max-size": "5m",
  "max-file":"10"
  },
  "storage-driver": "overlay2",
  "registry-mirrors": ["http://bc437cce.m.daocloud.io"],
  "insecure-registries": [
    "${REGISTRY}",
    "192.168.251.78",
    "registry.edoc2.com:5000"
  ],
  "data-root": "${DOCKER_DATA_ROOT}"
}
EOF

    systemctl daemon-reload && systemctl enable --now docker
    systemctl restart docker
    systemctl status docker | grep ""
}

if [ "${TYPE}" == "allinone" ];then
    event_path=$1
    kversion=$2
    cd "${event_path}"/Install_Packages/rockylinux8-soft/docker
fi

if [ "${TYPE}" == "cluster" ];then
    event_path="/tmp"
    kversion=$KVERSION
    dnf remove docker-ce -y
    sleep 3
    cd "${event_path}"/rockylinux8-soft/docker
fi

dockerinstall
