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
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-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的實現的文章就介紹到這瞭,更多相關Shell啟動Docker Container內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Jenkins打包微服務構建Docker鏡像運行的實現
- docker 添加端口及獲取dockerfile的方法
- docker搭建jenkins+maven代碼構建部署平臺
- Docker環境下Spring Boot應用內存飆升分析與解決場景分析
- Docker部署單頁應用的詳細操作