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

dockerinstall() {
    yum remove runc* -y
    yum remove containerd* -y
    sleep 5
    rpm -ivh rpm-* --force
    rpm -ivh selinux-policy-* --force
    rpm -ivh python3-rpm-4.14.3-31.0.1.an8.x86_64.rpm --force
    rpm -ivh container* --force
    rpm -ivh fuse* --force
    rpm -ivh lib* --force
    rpm -ivh docker-ce-cli-25.0.3-1.el8.x86_64.rpm --force
    rpm -ivh slirp4netns-* --force
    rpm -ivh docker-ce-* --force
    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/anolis-soft/docker
fi

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

dockerinstall
