部署docker和docker-compose
docker 安装
1 在线安装,则执行脚本后,根据提示选择在线安装的方式即可
https://blog.csdn.net/qq_44246980/article/details/124531349
#!/bin/bash
#该脚本用于测试环境部署docker
#所有者:北城半夏
#
#当前版本
#v1.0.1
#################################################################################
#离线安装docker#
###############
docker_tar_dir="/opt/docker"
docker_tar_name="docker-19.03.tar.gz"
docker_tar_name1="docker"
#安装完成后是否清理解压后的文件、目录
docker_pd="no"
#################################################################################
# docker基础环境部署 #
#################################################################################
info(){
echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info1(){
echo -e "\033[36m $1 \033[0m "
}
info2(){
echo -e "\033[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
info6(){
echo -e "\033[33m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[91m$1\033[0m "
}
info7(){
echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[32m$1\033[0m "
}
info8(){
echo -e "\033[31m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[35m$1\033[0m "
}
#在线安装docker
install_docker(){
#官方网站下载最新版docker
info "官方网站下载最新版docker"
yum install -y vim yum-utils bash-completion net-tools gcc ipvsadm wget ipset bc &>/dev/null
if [ $? -eq 0 ];then
info "安装docker的相关依赖包"
else
info8 "install docker CE fail"
fi
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo &>/dev/null
if [ $? -eq 0 ];then
info "添加docker的在线yum仓库"
else
info8 "install docker CE fail"
fi
info6 "重新加载yum仓库,根据网速决定等待的时间,请耐心等待!!!"
yum makecache &>/dev/null
info "yum仓库加载成功"
info3
info4 "开始安装docker基础环境,亲耐心等待"
yum -y install docker-ce >/dev/null
[ $? -eq 0 ] && info2 "docker 环境安装成功"
#开启路由转发
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p &>/dev/null
#添加阿里云镜像加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://geuj9lut.mirror.aliyuncs.com"]
}
EOF
}
off_line_install_docker(){
info6 "离线安装包的名称(${docker_tar_name}):"
info6 "离线安装包解压后目录(${docker_tar_name1}):"
info6 "离线安装包的位置(${docker_tar_dir}):"
read -p "安装完成后是否清理解压后的文件、目录(${docker_pd}):" docker_pd
local path_dir_tar="$docker_tar_dir"
local docker_tar_name1=${docker_tar_name1}
local docker_tar_name=${docker_tar_name}
local docker_pd=${docker_pd}
info "离线安装docke,版本基于用户提供的离线安装包${path_dir_tar}/${docker_tar_name}"
if [ -f ${path_dir_tar}/${docker_tar_name} ];then
cd ${path_dir_tar}
info "解压${docker_tar_name}到${path_dir_tar}"
tar -xf ${docker_tar_name} -C ${path_dir_tar}
[ $? -eq 0 ] && info7 "${docker_tar_name}解压成功,开始准备安装,请等待!!!"
cd ${path_dir_tar}/${docker_tar_name1}
yum -y install ./*.rpm >/dev/null
[ $? -eq 0 ] && info7 "docker安装成功"
else
info8 "${path_dir_tar}/${docker_tar_name} 不存在,请检查后重试!!!"
mkdir -p ${path_dir_tar}
exit 0
fi
if [ ${docker_pd} == yes ];then
rm -rf ${path_dir_tar}
[ $? -eq 0 ] && info "清理已安装的安装包及相应的压缩包"
fi
#开启路由转发
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p &>/dev/null
#添加阿里云镜像加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://geuj9lut.mirror.aliyuncs.com"]
}
EOF
}
#安装docker是的帮助信息
install_docker_help(){
info8 "离线安装docker时,需要提前准备好对应的RPM的安装包,且需要存放在$docker_tar_dir"
info8 "相应的配置可根据屏幕打印的提示继续即可"
}
#启动docker
start_docker(){
if [ `rpm -qa|grep docker |wc -l` -eq 0 ];then
install_docker_help
read -p "选择docker的安装方式:(在线=0,离线=1):" start_install_docerk
start_install_docerk=${start_install_docerk:-0}
if [ ${start_install_docerk} -eq 0 ];then
install_docker
systemctl daemon-reload
systemctl restart docker
systemctl enable docker >/dev/null
else
off_line_install_docker
systemctl restart docker
systemctl enable docker >/dev/null
fi
else
a=`rpm -qa|grep docker-ce|tail -1`
info8 "\033[32m ${a}已安装,不需要重新部署\033[0m"
local docker_status=`systemctl status docker |grep Active|awk '{print $2}'`
if [ ! ${docker_status} == "active" ];then
systemctl start docker
info8 "\033[32m docker服务已重新正常启动\033[0m"
else
info8 "\033[32m docker服务已正常启动!!!\033[0m"
fi
fi
}
start_docker
centos7.9
Docker Compose 安装
安装 Docker Compose 可以通过下面命令自动下载适应版本的 Compose
curl -L https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
为安装脚本添加执行权限
sudo chmod +x /usr/local/bin/docker-compose
查看安装是否成功
docker-compose -v
原文链接:https://blog.csdn.net/Black794/article/details/124046911