#!/usr/bin/env bash
# Install Docker Engine.
__Author__="liy"
path="$(dirname ${0})"
logfile=${1:-"${path}/install.log"}
: ‘print log info.‘
function printlog(){
datetime="$(date ‘+%F %T‘)"
echo -e "\033[32m[${datetime}] $1\033[0m" |tee -a ${logfile}
}
function init(){
which curl &>/dev/null
if [ $? -ne 0 ];then
printlog "Install curl command..."
apt install curl -y &>> ${logfile}
fi
}
function install(){
if [ $UID == "0" ];then
printlog "Start install Depend pack..."
apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y &>> ${logfile}
printlog "Start install public key..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
printlog "Start install Docker repository..."
add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable" &>> ${logfile}
apt-get update &>> ${logfile}
printlog "Start install Docker Engine..."
apt-get install docker-ce=5:19.03.15~3-0~ubuntu-bionic docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic -y &>> ${logfile}
printlog "Start setting daemon.json file..."
[ -d "/etc/docker" ] || mkdir /etc/docker
cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
printlog ‘Update GRUB_CMDLINE_LINUX...\nPlease Reboot...‘
sed -Ei ‘/GRUB_CMDLINE_LINUX=/s/(.*)"/\1 cgroup_enable=memory swapaccount=1"/‘ /etc/default/grub
update-grub &>> ${logfile}
printlog ‘Disable Swap Partition... backup file in /etc/fstab.bak‘
sed -i.bak ‘/swap/s/^/#/‘ /etc/fstab
systemctl daemon-reload
printlog "Setting Docker Starting UP..."
systemctl enable docker
else
echo -e "\033[31mUse Root Exec $0...\033[0m"
exit 127
fi
}
function main(){
echo -e "\033[31m${logfile} will be emptied after 3 seconds...\033[0m"
sleep 5
echo -n > ${logfile}
printlog "log output to ${logfile} and use tail -f to view..."
init
install
}
main
UbuntuServer 1804.5安装Docker