Cenots7.6 环境一键安装 Docker,Docker-Compose 脚本

1. Docker概述

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙盒机制,相互之间不会有任何接口(类似 iPhone 的 app)。几乎没有性能开销,可以很容易地在机器和数据中心中运行。最重要的是,他们不依赖于任何语言、框架或包装系统。更多相关说明,请点击查看"Cenots7.6 安装部署 Docker 环境" 文章

#!/usr/bin/env bash
#
# 基于操作系统《CentOS 6.5_x64最小化安装》的安全和优化脚本
#
# CentOS/RadHat 6+ Debian 7+ and Ubuntu 14+
#
#  Copyright © 2015年10月1日  Email:websfx@gmail.com
#
#    个人博客: http://www.fooher.com by 运维天涯
#

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear

# 检查是否 root 用户
[ $(id -u) != '0' ] && { echo "${CFAILURE}错误: 你必需以root身份运行此脚本${CEND}"; exit 1; }

# 安装 Docker-compose(必须)
function install_docker_compose(){
    echo -e "\033[32m [INFO]: 安装 Docker,Docker-Compose \033[0m"

# 卸载docker旧版本
yum remove -y docker \
     docker-client \
     docker-client-latest \
     docker-common \
     docker-latest \
     docker-latest-logrotate \
     docker-logrotate \
     docker-selinux \
     docker-engine-selinux \
     docker-engine

# 安装相关工具类
  yum install -y yum-utils device-mapper-persistent-data lvm2

# yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
  yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  yum makecache fast
  yum -y install docker-ce
  systemctl start docker
  systemctl enable docker
  docker version

# 镜像加速
mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://你的id.mirror.aliyuncs.com"],
  "live-restore": true
}
EOF

# vim /etc/sysconfig/docker
# --signature-verification=false --registry-mirror=http://f2d6cb40.m.daocloud.io'
# sed -i "s@signature-verification=false.*@signature-verification=false --registry-mirror=http://f2d6cb40.m.daocloud.io'@" /etc/sysconfig/docker

  systemctl daemon-reload
  systemctl restart docker

  curl -L https://mirrors.aliyun.com/docker-toolbox/linux/compose/$(curl -s https://mirrors.aliyun.com/docker-toolbox/linux/compose/ |egrep '^|' '{print $2}' |sort -V |tail -1)docker-compose-Linux-x86_64 -o  /usr/bin/docker-compose

  chmod +x /usr/bin/docker-compose
  docker-compose -v

 [ ! -e "/etc/sysctl.conf_bk" ] && /bin/mv /etc/sysctl.conf{,_bk}
cat > /etc/sysctl.conf << EOF
fs.file-max=1000000
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_fin_timeout = 20
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_syncookies = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65000
net.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_established = 3600
#开启动网络转发功能
net.ipv4.ip_forward = 1
EOF

sysctl -p

  if [ $? == 0 ];then
      echo -e "\033[32m [INFO]: 安装 Docker,Docker-Compose 成功. \033[0m"
  else
      echo -e "\033[31m [ERROR]: 安装 Docker,Docker-Compose 失败 \033[0m"
      exit -2
  fi