Get the App
SLTechnology News&Howtos  ›  Development  › 

How to start Docker Container with the Shell command

Shulou Source: shulou.com Published: 2022-06-01 12:11:04 10月22日 Update

This article mainly introduces the relevant knowledge of "how to start Docker Container with Shell command". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to start Docker Container with Shell command" can help you solve the problem.

After the Linux virtual machine is restarted, you have to start multiple Docker Container manually, which is really troublesome, which can be subtracted by using the Shell command.

Create files and add executable permissions

Touch startcontiner.shchomd + x startcontainer.sh

Write startcontainer.sh

#! / bin/sh # chkconfig: 35 90 80 # description: start docker containercontainerNames= "mysql redis rabbitmq mongo zookeeper" # launch Docker Contianersfunction startContainer () {sudo docker start $1} # traverse containerNamesfor containerName in ${containerNames} do echo "Container ${containerName} start" startContainer ${containerName} done according to the name

Start and test

. / startcontainer.shPS:docker batch launch project shell script

Dockerfile

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

File directory

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

Script

#! / bin/bash#serverList= ("eureka-server"gateway-zuul"blockchain-core"blockchain-order"); #-configure start-# project directory projectsDir= "/ opt/docker/projects" # log output directory logsDir= "/ opt/docker/logs" #-configure end-serverList= `ls ${projectsDir} ` The first parameter of the commond=$1;serverName=$2;port=$3;# startup service method is the service name, and the second parameter is function startServer () {echo "- start the project: $1 $2" serverCount= `docker ps-f status=exited | grep $1 | wc-l`; # determine whether it has been started, and the port is the default port if [[${serverCount} > 0 & & $2 = "]] Then echo "docker start startup project: $1" docker start $1 else # to determine whether an image imageCount= `docker images is generated | grep $1 | wc-l`; if [[${imageCount} > 0]] Then echo "docker run startup project: $1" # copy project jar package is app.jar currProjectDir=$projectsDir/$1 cd $currProjectDir cp `ls | grep $1` "app.jar" # execute the command 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 $2VV ${ LogsDir}: / opt-v ${currProjectDir}: / project-- name $1percent 2 $1 java-jar / project/app.jar-- server.port=$2 "docker run-d-p $2purge 2-v ${logsDir}: / opt-v ${currProjectDir}: / project-- name $1percent 2 $1 java-jar / project/app.jar-- server.port=$2 fi else Echo "generate $1 project image: $1" cd "${projectsDir} / $1" docker build-t $1. # call the method startServer $1 $2 fi fi} # get the port number parameter based on the project name: project name 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" parameter 1: execute command "echo" 1. Start start "echo" 2. Stop stop "echo" 3. Restart restart "echo" parameter 2: service name "echo" 1.eureka-server service registry "echo" 2.gateway-zuul Gateway service "echo" 3.blockchain-core system service service "echo" 4.blockchain-order order service "echo" 5.all all "exit" Elif [[${commond} = = "" | | ${serverName} = = "]]; then echo" error: please pass in the startup parameters! Details:-help "exit; fi# single project operation if [[${serverName}! =" all "& & ${commond} = =" start "]]; then startServer ${serverName} ${port} elif [[${serverName}! =" all "& & ${commond}! =" start "]] Items in the then echo "start ${commond} container: ${serverName}" docker ${commond} `docker ps | grep ${serverName} | awk-F "'{print $1} '`fi# all project operations # 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 "start items in the ${commond} container: ${server}" docker ${commond} `docker ps | grep ${serverName} | awk-F "'{print $1} '`donefiecho" execution completed! " This is the end of the introduction to "how to start Docker Container with the Shell command". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Project command parameter service method directory knowledge port container file script industry image trouble generation configuration different practical content again Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Information Redmi Docker