[root@danrtsey ~]#cat install_docker-ce_centos7.sh
#!/bin/bash
#
#********************************************************************
#script_name: install_docker-ce_centos7.sh
#Author: Danrtsey
#mail: mydefiniteaim@126.com
#attentions:
#********************************************************************
export PATH=$PATH
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
COLOR="echo -e \\033[1;31m"
END="\033[m"
###Check if user is root
if [ $UID -ne 0 ]; then
echo "Error: This script must be executed as root."
exit 1
fi
###set the ip in hosts
hostsset() {
echo "############################ Ip&Hosts Configuration #######################################"
hostname=`hostname`
ip=`ip a|grep 'inet '|grep -v '127.0.0.1'|awk '{print $2}'|awk -F '/' '{print $1}'`
for i in ${ip}
do
a=`grep "${i}" /etc/hosts`
if [ ! -n "${a}" ];then
echo "${i} ${hostname}" >> /etc/hosts
else
break
fi
done
}
ntp() {
yum -y install ntp
systemctl enable ntpd
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
systemctl start ntpd
if [ $? != 0 ]; then
errorExit 'ntp 启动未成功'
fi
return 0
}
# 配置 系统参数
syspro() {
echo "################################################################################################"
echo " 修改系统参数 "
echo "################################################################################################"
if [ `getenforce` == "Enforcing" ];then
setenforce 0
sed -i "s@SELINUX=enforcing@SELINUX=disabled@g" /etc/selinux/config
elif [ `getenforce` == "Permissive" ];then
sed -i "s@SELINUX=enforcing@SELINUX=disabled@g" /etc/selinux/config
setenforce 0
else
continue
fi
echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
cat >> /etc/sysctl.conf<<EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
kernel.shmmax = 4294967295
kernel.shmmni = 4096
kernel.shmall = 1048576
kernel.sem = 1010 129280 1010 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 4194304
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
fs.aio-max-nr = 1048576
fs.file-max = 6815744
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
modprobe br_netfilter
sysctl -p
}
# 安装更新系统,并配置docker-ce源
dkrepo() {
echo "####################### 安装更新系统,并配置docker-ce源 ########################################"
#yum update -y
yum install -y yum-utils device-mapper-persistent-data lvm2 bash-completion
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo || { ${COLOR}"互联网连接失败,请检查网络配置!"${END};exit; }
yum clean all
yum makecache all
printf '\n'
mkdir /etc/docker
cat >>/etc/docker/daemon.json<<EOF
{
"registry-mirrors": ["https://yeuphhaz.mirror.aliyuncs.com"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}
EOF
if [ $? != 0 ]; then
errorExit 'docker 源配置未成功'
fi
return 0
}
# 卸载、安装 docker-ce
uninstall() {
yum -y remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
container*
}
install() {
echo "############################ 安装 docker-ce #######################################"
yum -y install docker-ce docker-ce-selinux
systemctl start docker
docker version && ${COLOR}"Docker安装成功"${END} || ${COLOR}"Docker安装失败"${END}
systemctl enable docker
if [ $? != 0 ]; then
errorExit 'docker-ce 安装未成功'
fi
return 0
}
# Display an error and exit
errorExit() {
echo "$@" >&2
exit 1
}
hostsset
ntp
syspro
dkrepo
uninstall
install
来源:51CTO
作者:love8度幸福
链接:https://blog.51cto.com/8355320/2473690