您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。