您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“怎么用Shell命令启动Docker Container”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用Shell命令启动Docker Container”文章能帮助大家解决问题。
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} start" startContainer ${containerName} done
启动并测试
./startcontainer.sh
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-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} 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”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。