欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

docker和docker-compose一键安装教程(支持在线和离线)

程序员文章站 2022-06-22 13:54:53
shell脚本#!/bin/sh# 当前目录current_dir=$( cd "$(dirname "$0")" pwd)#install docker if which docker >/d...

shell脚本

#!/bin/sh
# 当前目录
current_dir=$(
 cd "$(dirname "$0")"
 pwd
)

#install docker 
if which docker >/dev/null; then
 echo "检测到 docker 已安装,跳过安装步骤"
 docker -v
 echo "启动 docker "
 service docker start 2>&1 | tee -a ${current_dir}/install.log
else
 if [[ -d "$current_dir/docker" ]]; then
  echo "... 离线安装 docker"
	 cp $current_dir/docker/centos-local.tgz /root/
	 cd /root && tar -xvzf centos-local.tgz 
  cd /root/docker-ce-local &&rpm -ivh createrepo-0.9.9-28.el7.noarch.rpm
  mkdir -p /etc/yum.repos.d/repobak && mv /etc/yum.repos.d/centos* /etc/yum.repos.d/repobak
  cp $current_dir/docker/docker-ce-local.repo /etc/yum.repos.d/docker-ce-local.repo
  cd /root/docker-ce-local &&createrepo /root/docker-ce-local && yum makecache
	 cd $current_dir/docker/ &&yum install -y container-selinux-2.9-4.el7.noarch.rpm &&yum install -y docker-ce
	 echo "... 启动 docker"
	 sudo systemctl start docker 2>&1 | tee -a ${current_dir}/install.log
	 echo '{"registry-mirrors":["https://registry.docker-cn.com"]}'>/etc/docker/daemon.json
	 cat /etc/docker/daemon.json
	 service docker restart
 else
  echo "... 在线安装 docker"
  curl -fssl https://get.docker.com -o get-docker.sh 2>&1 | tee -a ${current_dir}/install.log
  sudo sh get-docker.sh 2>&1 | tee -a ${current_dir}/install.log
  echo "... 启动 docker"
  service docker start 2>&1 | tee -a ${current_dir}/install.log
 fi
fi

##install latest stable docker compose release
if which docker-compose >/dev/null; then
 echo "检测到 docker compose 已安装,跳过安装步骤"
 docker-compose -v
else
 if [[ -d "$current_dir/docker-compose" ]]; then
  echo "... 离线安装 docker-compose"
	 cd $current_dir/docker-compose/ && cp docker-compose /usr/local/bin/
	 chmod +x /usr/local/bin/docker-compose
	 docker-compose -version
	 echo "... 离线安装 docker-compose 成功"
 else
  echo "... 在线安装 docker-compose"
  curl -l "https://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 2>&1 | tee -a ${current_dir}/install.log
  chmod +x /usr/local/bin/docker-compose
  ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
	 docker-compose -version
	 echo "... 在线安装 docker-compose 成功"
 fi
fi

如果脚本复制后使用报下面的错误

docker和docker-compose一键安装教程(支持在线和离线)

请执行

sed -i 's/\r$//' installdocker.sh
chmod +x installdocker.sh

在网络条件不好的情况下建议离线安装,请先下载下面的包

离线的安装包
链接: https://pan.baidu.com/s/1andm5v-z1zmljekc4djadq

 提取码: 39dm

到此这篇关于docker和docker-compose一键安装(支持在线和离线)的文章就介绍到这了,更多相关docker和docker-compose安装内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!