centos7 一键安装docker,docker-compose环境

post by rocdk890 / 2020-3-19 11:01 Thursday linux技术
  docker环境相信大家都有在用,但安装环境过程太烦人了,所以我写成一键安装脚本来解放双手.
  系统:centos7.x(64位)

cat /root/soft_shell/auto_install_docker.sh
#!/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
clear

# 检查是否 root 用户
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to initialization OS."
    exit 1
fi

# 安装 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

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

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://ujkmu6sm.mirror.aliyuncs.com"]
}
EOF

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 '^<a'|awk -F "=" '{print $2}'|awk -F '"' '{print $2}'|awk -F / '{print $1}'|sort -V |tail -1)/docker-compose-Linux-x86_64 -o  /usr/bin/docker-compose

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

function iptable_config(){
if [ ! -f "/etc/sysconfig/iptables.bak--" ];then
  cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak--
fi
cat > /etc/sysconfig/iptables << EOF
*nat
:PREROUTING ACCEPT [3094660:174262658]
:INPUT ACCEPT [3094660:174262658]
:OUTPUT ACCEPT [15266190:798272902]
:POSTROUTING ACCEPT [15266190:798272902]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
COMMIT
# Completed on Mon Dec 17 15:32:17 2018
# Generated by iptables-save v1.4.21 on Mon Dec 17 15:32:17 2018
*filter
:INPUT ACCEPT [5:200]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:616]
:DOCKER - [0:0]
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
COMMIT
EOF

systemctl restart iptables
[ $? -eq 0 ] && echo "iptables config complete."
}

iptable_config
install_docker_compose

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
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-770.html

标签: centos 安装 shell install 一键 docker centos7 docker-compose

评论: