Compiling a Startup Script

During application containerization, you need to prepare a startup script. The method of compiling this script is the same as that of compiling a shell script. The startup script is used to:

  • Start up the software on which the application depends.
  • Set the configurations that need to be changed as the environment variables.

Startup scripts vary according to applications. You need to compile the script based on your service requirements.

Procedure

  1. Log in as the root user to the device running Docker.
  2. Run the following commands to create the directory where the application is to be stored:

    mkdir apptest

    cd apptest

  3. Compile a script file. The name and content of the script file vary according to applications. You need to compile the script file based on your application. The following example is only for your reference.

    vi start_tomcat_and_mongo.sh
    #!/bin/bash
    source  /etc/profile
    ./usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --port=27017 –fork
    sed -i "s|mysql://.*/awcp_crmtile|mysql://$MYSQL_URL/$MYSQL_DB|g" /root/apache-tomcat-7.0.82/webapps/awcp/WEB-INF/classes/conf/jdbc.properties
    sed -i "s|username=.*|username=$MYSQL_USER|g" /root/apache-tomcat-7.0.82/webapps/awcp/WEB-INF/classes/conf/jdbc.properties
    sed -i "s|password=.*|password=$MYSQL_PASSWORD|g" /root/apache-tomcat-7.0.82/webapps/awcp/WEB-INF/classes/conf/jdbc.properties
    bash /root/apache-tomcat-7.0.82/bin/catalina.sh run

    Script description:

    • source /etc/profile: Load system environment variables.
    • ./usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --port=27017 –fork: Start the MongoDB database. In this example, the data storage directory is /usr/local/mongodb/data, and this directory will be used in the subsequent storage configuration.
    • sed -i "s|...: These three script commands indicate that the contents related to the MySQL database in the environment variables are written into the configuration file when Docker is started.
    • bash /root/apache-tomcat-7.0.82/bin/catalina.sh run: Start Tomcat at the end.