开发者

Shell 命令启动Docker Container的实现

开发者 https://www.devze.com 2022-12-07 10:19 出处:网络 作者: vio_dzyls
linux虚拟机重启后,都要手动启动多个docker Container,着实麻烦,使用Shell命令可以减去这个麻烦。

linux虚拟机重启后,都要手动启动多个docker Container,着实麻烦,使用Shell命令可以减去这个麻烦。

创建文件,添加可执行权限

touch startcontiner.sh
chomd +x startcontainer.sh

编写startcontainer.sh

#!/bin/sh             
#chkconfig: 35 90 80       
#description: start docker container
containerNames="mysql redis rabbitmq mongo zookeeper"
# 根据名称启动Docker Contianers
function startContainer(){
  sudo docker start $1
}
# 遍历containerNames
for containerName in ${containerNames}
do
    echo "Container ${containerName} sta开发者_Centosrt"
    startContainer ${containerName}
done

启动并测试

./startcontainer.sh

PS:docker 批量启动项目 shell 脚本

Dockerfile

FROM java:8
VOLUME /tmp
COPY blockchain-core-0.1.jar /project/app.jar
CMD java -jar /project/app.jar

文件目录

docker/
├── logs
│  └── logs
│    ├── block-chai编程客栈n-core
│    │  ├── 2018-11-23
│    │  │  ├── access.0.log
│    │  │  ├── debug.0.log
│    │  │  ├── detail.0.log
│    │  │  ├── error.0.log
│    │  │  ├── execute.0.log
│    │  │  └── info.0.log
│    │  └── 2018-11-26
│    │    ├── access.0.log
│    │    ├── debug.0.log
│    │    ├── detail.0.log
│    │    ├── error.0.log
│    │    ├── execute.0.log
│    │    └── info.0.log
│    └── gateway-zuul
│      ├── 2018-11-23
│      │  ├── access.0.log
│      │  ├── debug.0.log
│      │  ├── detail.0.log
│      │  ├── error.0.log
│      │  ├── execute.0.log
│      │  └── info.0.log
│      └── 2018-11-26
│        ├── access.0.log
│        ├── debug.0.log
│        ├── detail.0.log
│        ├── error.0.log
│        ├── execute.0.log
│        └── info.0.log
├── projects
│  ├── blockchain-core
│  │  ├── app.jar
│  │  ├── blockchain-core-0.1.jar
│  │  └── Dockerfile
│  ├── eureka-server
│  │  ├── Dockerfile
│  │  └── eureka-server-1.0.jar
│  └── gateway-zuul
│    ├── app.jar
│    ├── Dockerfile
│    └── gateway-zuul-0.0.1.jar
├── run

脚本

#!/bin/bash

#serverList=("eureka-server" "gateway-zuul" "blockchain-core" "blockchain-order");
#------------配置 start-------------
# 项目目录
projectsDir="/opt/docker/projects"
# 日志输出目录
logsDir="/opt/docker/logs"
#------------配置 end-------------

serverList=`ls ${projectsDir}`ObVkYdtU;
commond=$1;
serverName=$2;
port=$3;

# 启动服务方法 第一个参数为 服务名,第二个参数为 端口
function startServer(){
 echo "--------------启动项目开始:$1 $2"
 serverCount=`docker ps -f status=exited | grep $1 |wc -l`;
  # 判断是否已经启动过,且端口为默认端口
    if [[ ${serverCount} > 0 && $2 == "" ]];then
        echo "docker start 启动项目:$1"
        docker start $1
    else
      # 判断是否生成了镜像
      imageCount=`docker images | grep $1 | wc -l`;

      if [[ ${imageCount} > 0 ]];then
         echo "docker run 启动项目: $1"
         # 复制项目jar包为 app.jar
         currProjectDir=$projectsDir/$1
         cd $currProjectDir
         cp `ls | grep $1` "app.jar"
         # 执行命令
         if [[ $2 == "" ]];then
          echo "docker run -d -v ${logsDir}:/opt -v $currProjectDir:/project --name $1 $1 "
          docker run -d -v ${logsDir}:/opt -v $currProjectDir:/project --name $1 $1
          else
           echo "docker run -d -p $2:$2 -v ${logsDir}:/opt -v ${currProjectDir}:/project --name $1_$2 $1 java -jar /project/app.jar --swww.devze.comerver.port=$2"
           docker rObVkYdtUun -d -p $2:$2 -v ${logsDir}:/opt -v ${currProjectDir}:/project --name $1_$2 $1 java -jar /project/app.jar --server.port=$2
          fi
        else
         echo "生成 $1 项目镜像:$1"
         
          cd "${projectsDir}/$1"
         
         docker build -t $1 .

         # 再次调用该方法
         startServer $1 $2
       fi
    fi
}

# 根据项ObVkYdtU目名获取端口号 参数为项目名
function getPort(){
 tempProjectDirName=`ls $projectsDir | grep $1`
 OLD_IFS="$IFS";
  IFS="_"
  arr=($tempProjectDirName)
  IFS="$OLD_IFS"
  echo ${arr[1]}
 return ${arr[1]}
}


if [[ ${commond} == "-help" ]];then
 echo " "
 echo "参数1:执行命令"
 echo "  1. start 启动"
 echo "  2. stop  停止"
 echo "  3. restart 重启"
 echo "参数2:服务名"
 echo "  1.eureka-server  服务注册中心"
 echo "  2.gateway-zuul  网关服"
 echo "  3.blockchain-core 系统服务服"
 echo "  4.blockchain-order 订单服"
 echo "  5.all   全部"
 exit;
elif [[ ${commond} == "" || ${serverName} == "" ]];then
 echo "错误:请传入启动参数!详情:-help"
    exit;
fi

# 单个项目操作
if [[ ${serverName} != "all" && ${commond} == "start" ]];then
 
 startServer ${serverName} ${port}
 
elif [[  ${serverName} != "all" && ${commond} != "start" ]];then
 echo "开始${commond}容器中的项目:${serverName}"
 docker ${commond} `docker ps | grep ${serverName} | awk -F"    " '{print $1 }'`
fi

# 全部项目操作
# IFS=";\n"
if [[ ${serverName} == "all" && ${commond} == "start" ]];then
 for server in ${serverList[@]}
 do
  startServer ${server}
 
 done
elif [[ ${serverName} == "all" && ${commond} != "start" ]]; then
 for server in ${serverList[@]}
 do
  echo "开始 ${commond} 容器中的项目:${server}"
  docker ${commond} `docker ps | grep ${serverName} | awk -F"    " '{print $1 }'`
 
 done
fi

echo "执行完毕!"

到此这篇关于Shell 命令启动Docker Container的实现的文章就介绍到这了,更多相关Shell启动Docker Container内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消