#!/bin/bash
# Date:2022-09-05
# docker安装后优化及配置

dockerinstall(){
    cd "${event_path}"/soft
    #tar xvf docker-20.10.8.tgz
    rpm -ivh docker-ce-20.10.9-1.ky10.aarch64.rpm --force
    #\cp docker/* /usr/bin/

    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
    mkdir -p /etc/docker/
    sleep 3
    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",
  "storage-opts": [
  "overlay2.override_kernel_check=true"
  ],
  "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
fi
if [ "${TYPE}" == "cluster" ];then
    event_path="/tmp"
    kversion=$KVERSION
    system=$3
    if [ "${system}" == "KyCentos" ];then
        mkdir -p /opt/rpm && \cp -rf /usr/local/docker* /opt/rpm/
        systemctl stop docker &
        sleep 15
        yum remove docker-ce -y &
        sleep 10
	rm -rf /etc/systemd/system/docker.service &
	sleep 1
    fi
fi
dockerinstall
