Centos安装Docker-ce与Docker-compose的四种安装方式

1、在线脚本安装

安装条件:机器可以访问外网
cat docker-ce.sh

# 更换yum源并安装docker、docker-compose
yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
mv CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
yum clean all && yum makecache && yum -y update
# 安装docker18.03与docker-compose  
installdocker1()
{
        yum -y install yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        yum-config-manager --enable docker-ce-edge
        yum-config-manager --enable docker-ce-test
        yum -y install docker-ce
}
installdocker2()
{
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager  --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
}
docker version
if [ $? -eq 127 ];then
        echo "we can install docker-ce"
        sleep 5
        installdocker1
        if [ $? -ne 0 ];then
            echo "you should use aliyun image."
            installdocker2
        fi
        docker version
        if [ $? -lt 127 ];then
                echo "the installation of docker-ce is ok."
                rpm -qa | grep docker | xargs rpm -e --nodeps 
                yum -y install docker-ce-18.03*
        else
                echo "the installation of docker-ce failed ,please reinstall"
                exit -1
        fi
else
        echo "docker have installed,pleae uninstall old version"
        sleep 5
        rpm -qa | grep docker | xargs rpm -e --nodeps
        docker version
        if [ $? -eq 127 ];then
                echo "old docker have been uninstalled and you can install docker-ce"
                sleep 5
                installdocker1
                if [ $? -ne 0 ];then
                    echo "you should use aliyun image."
                    installdocker2
                fi
                docker version
                if [ $? -lt 127 ];then
                        echo "the installation of docker-ce is ok."
                        rpm -qa | grep docker | xargs rpm -e --nodeps
                        yum -y install docker-ce-18.03*
                else
                        echo "the installation of docker-ce failed anad please reinstall."
                        exit -1
                fi
        else
                echo "the old docker uninstalled conpletely and please uninstall again."
                exit -1
        fi
fi
systemctl start docker && systemctl enable docker && systemctl daemon-reload 
docker_version=$(docker version | grep "Version" | awk '{print $2}' | head -n 2 | sed -n '2p')
if [ $? -eq 0 ];then
        echo "docker start successfully and the version is ${docker_version}"
fi
# 安装docker-compose并检查版本
yum -y install epel-release  && yum -y install python-pip  && pip install docker-compose  && pip install --upgrade pip 
docker-compose_version=$(docker-compose version | grep 'docker-compose' | awk '{print $3}')
if [ $? -eq 0 ];then
        echo "the docker-compose version is ${docker-compose_version}"
fi
# 配置docker加速拉取
echo {\"registry-mirrors\":[\"https://nr630v1c.mirror.aliyuncs.com\"]} > /etc/docker/daemon.json

# 安装常用工具
yum -y install lrzsz && yum -y install openssh-clients && yum -y install telnet && yum -y install rsync 

# 防火墙配置
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux
systemctl stop firewalld
systemctl disable firewalld
systemctl daemon-reload

该脚本安装的docker版本为18.03,安装其他版本可以修改版本号

优势:联网安装方便,内网安装不方便

2、yum私有仓库安装

安装条件:需要先建立私有仓库,然后将要安装的docker文件及其所有依赖导入仓库
docker文件及依赖获取方式:在1的方式上利用工具yumdownloader来实现

文件依赖获取
在用1方式安装完成后找到所有安装的包,如下图
08api75

yumdownloader --resolve --destdir=rpmdir docker
package1
package2
package3

获取所有的包后其实可以直接到目录中执行yum localinstall * 安装,如果想在整个局域网内所有机器都可以安装那么可以创建一个共享的私有yum仓库,然后yum安装

优势:适合局域网多台机器部署
缺点:需要先获取包和依赖,在创建yum私有库

docker-compose安装
下载二进制文件,使用下面脚本进行安装
cat compose.sh

mv docker-compose-Linux-x86_64 /usr/local/bin
chmod +x /usr/local/bin/docker-compose-Linux-x86_64
ln -s /usr/local/bin/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

3、二进制安装

以docker-18.09为例
下载docker18.09

tar xzvf docker-18.09.6.tgz
cp -ar docker/* /usr/bin

创建系统服务文件
cat /usr/lib/systemd/system/docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP 
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl start docker.service
docker version
显示

Client: Docker Engine - Community
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77
 Built:             Sat May  4 02:33:34 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:41:08 2019
  OS/Arch:          linux/amd64
  Experimental:     false

安装docker compose

curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose