这篇文章主要介绍“怎么用Shell命令启动Docker Container”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用Shell命令启动Docker Container”文章能帮助大家解决问题。
Linux虚拟机重启后,都要手动启动多个Docker Container,着实麻烦,使用Shell命令可以减去这个麻烦。
创建文件,添加可执行权限
touch startcontiner.shchomd +x startcontainer.sh
编写startcontainer.sh
#!/bin/sh #chkconfig: 35 90 80 #description: start docker containercontainerNames="mysql redis rabbitmq mongo zookeeper"# 根据名称启动Docker Contianersfunction startContainer(){ sudo docker start $1}# 遍历containerNamesfor containerName in ${containerNames}do echo "Container ${containerName} start" startContainer ${containerName}done
启动并测试
./startcontainer.sh
PS:docker 批量启动项目 shell 脚本
Dockerfile
FROM java:8 VOLUME /tmp COPY blockchain-core-0.1.jar /project/app.jarCMD java -jar /project/app.jar
文件目录
docker/├── logs│ └── logs│ ├── block-chain-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}`;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 --server.port=$2" docker run -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}# 根据项目名获取端口号 参数为项目名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} doneelif [[ ${serverName} == "all" && ${commond} != "start" ]]; then for server in ${serverList[@]} do echo "开始 ${commond} 容器中的项目:${server}" docker ${commond} `docker ps | grep ${serverName} | awk -F" " '{print $1 }'` donefiecho "执行完毕!"
关于“怎么用Shell命令启动Docker Container”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。