centos7 一键安装docker,docker-compose环境
docker环境相信大家都有在用,但安装环境过程太烦人了,所以我写成一键安装脚本来解放双手.
系统:centos7.x(64位)
cat /root/soft_shell/auto_install_docker.sh
#!/bin/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/|grep 'href'|awk -F '"' '{print $4}'|awk -F '/' '{print $1}'|sort -V|grep -v '^$'|grep -v "[a-z]"|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
评论: