SoFunction
Updated on 2025-03-10

Shell command starts the implementation of Docker Container

After the Linux virtual machine is restarted, multiple Docker Containers must be manually started. It is really troublesome. Using the Shell command can reduce this trouble.

Create a file and add executable permissions

touch 
chomd +x 

write

#!/bin/sh                          
#chkconfig: 35 90 80               
#description: start docker container
containerNames="mysql redis rabbitmq mongo zookeeper"
# Start Docker Containers by namefunction startContainer(){
    sudo docker start $1
}
# traversal containerNamesfor containerName in ${containerNames}
do
        echo "Container ${containerName} start"
        startContainer ${containerName}
done

Start and test

./

PS: docker batch start project shell script

Dockerfile

FROM java:8 
VOLUME /tmp 
COPY blockchain-core-0. /project/
CMD java -jar /project/ 

File Directory

docker/
├── logs
│   └── logs
│       ├── block-chain-core
│       │   ├── 2018-11-23
│       │   │   ├── access.
│       │   │   ├── debug.
│       │   │   ├── detail.
│       │   │   ├── error.
│       │   │   ├── execute.
│       │   │   └── info.
│       │   └── 2018-11-26
│       │       ├── access.
│       │       ├── debug.
│       │       ├── detail.
│       │       ├── error.
│       │       ├── execute.
│       │       └── info.
│       └── gateway-zuul
│           ├── 2018-11-23
│           │   ├── access.
│           │   ├── debug.
│           │   ├── detail.
│           │   ├── error.
│           │   ├── execute.
│           │   └── info.
│           └── 2018-11-26
│               ├── access.
│               ├── debug.
│               ├── detail.
│               ├── error.
│               ├── execute.
│               └── info.
├── projects
│   ├── blockchain-core
│   │   ├── 
│   │   ├── blockchain-core-0.
│   │   └── Dockerfile
│   ├── eureka-server
│   │   ├── Dockerfile
│   │   └── eureka-server-1.
│   └── gateway-zuul
│       ├── 
│       ├── Dockerfile
│       └── gateway-zuul-0.0.
├── run

script

#!/bin/bash

#serverList=("eureka-server" "gateway-zuul" "blockchain-core" "blockchain-order");
#------------------------------------------------------------------------------------------------------------------------------# Project DirectoryprojectsDir="/opt/docker/projects"
# Log output directorylogsDir="/opt/docker/logs"
#------------------------------------------------------------------------------------------------------------------------------
serverList=`ls ${projectsDir}`;
commond=$1;
serverName=$2;
port=$3;

# Start the service method The first parameter is the service name, and the second parameter is the portfunction startServer(){
    echo "------------------------------------------------------------------------------------------------------------------------------
    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 Start the project:$1"
                docker start $1
        else 
                # Determine whether a mirror has been generated                imageCount=`docker images | grep $1 | wc -l`;

                if [[ ${imageCount} > 0 ]];then
                    echo "docker run starts the project: $1"
                    # Copy the project jar package as                    currProjectDir=$projectsDir/$1
                    cd $currProjectDir
                    cp `ls | grep $1` ""
                    # 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 $2:$2 -v ${logsDir}:/opt -v ${currProjectDir}:/project --name $1_$2 $1 java -jar /project/ --=$2"
                           docker run  -d -p $2:$2 -v ${logsDir}:/opt -v ${currProjectDir}:/project --name $1_$2 $1 java -jar /project/ --=$2
                       fi
                 else
                     echo "Generate $1 project image: $1"
                     
                         cd "${projectsDir}/$1"
                     
                     docker build -t $1 .

                     # Call this method again                     startServer $1 $2
                fi
        fi
}

# Get the port number according to the project name Parameter is the project namefunction 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 the command"
    echo "       1. start    Start"
    echo "       2. stop       Stop”
    echo "       3. restart    Restart"
    echo "Parameter 2: Service name"
    echo "
    echo "            Gateway Server"
    echo "        -core    System Service Service"
    echo "         -order    Order Service"
    echo "                    all"
    exit;
elif [[ ${commond} == "" || ${serverName} == "" ]];then 
    echo "mistake:Please pass in startup parameters!Details:-help"
        exit; 
fi

# Single project operationif [[ ${serverName} != "all" && ${commond} == "start" ]];then
    
    startServer ${serverName} ${port}
    
elif [[   ${serverName} != "all" && ${commond} != "start" ]];then
    echo "start${commond}Items in the 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} 
    
    done
elif [[ ${serverName} == "all" && ${commond} != "start" ]]; then
    for server in ${serverList[@]}
    do
        echo "start ${commond} Items in the container:${server}"
        docker ${commond} `docker ps | grep ${serverName} | awk -F"        " '{print $1 }'`
    
    done
fi

echo "Execution is complete!"

This is the end of this article about the implementation of the Shell command to start Docker Container. For more relevant Shell startup Docker Container content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!