Search code examples
shelldockerscriptingdockerfileweblogic12c

Starting multiple services using shell script in Dockerfile


I am creating a Dockerfile to install and start the WebLogic 12c services using startup scripts at "docker run" command. I am passing the shell script in the CMD instruction which executes the startWeblogic.sh and startNodeManager.sh script. But when I logged in to the container, it has started only the first script startWeblogic.sh and not even started the second script which is obvious from the docker logs.

The same script executed inside the container manually and it is starting both the services. What is the right instruction for running the script to start multiple processes in a container and not to exit the container?

What am I missing in this script and in the dockerfile? I know that container can run only one process, but in a dirty way, how to start multiple services for an application like WebLogic which has a nameserver, node manager, managed server and creating managed domains and machines. The managed server can only be started when WebLogic nameserver is running.

Script: startscript.sh

#!/bin/bash

# Start the first process
/u01/app/oracle/product/wls122100/domains/verdomain/bin/startWebLogic.sh -D
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start my_first_process: $status"
  exit $status
fi

# Start the second process
/u01/app/oracle/product/wls122100/domains/verdomain/bin/startNodeManager.sh -D
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start my_second_process: $status"
  exit $status
fi

while sleep 60; do
  ps aux |grep "Name=adminserver" |grep -q -v grep
  PROCESS_1_STATUS=$?
  ps aux |grep node |grep -q -v grep
  PROCESS_2_STATUS=$?
  # If the greps above find anything, they exit with 0 status
  # If they are not both 0, then something is wrong
  if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
    echo "One of the processes has already exited."
    exit 1
  fi
done

Truncated the dockerfile.

RUN unzip $WLS_PKG 
RUN $JAVA_HOME/bin/java -Xmx1024m -jar /u01/app/oracle/$WLS_JAR -silent -responseFile /u01/app/oracle/wls.rsp -invPtrLoc /u01/app/oracle/oraInst.loc > install.log
RUN rm -f $WLS_PKG

RUN . $WLS_HOME/server/bin/setWLSEnv.sh && java weblogic.version
RUN java weblogic.WLST -skipWLSModuleScanning create_basedomain.py

WORKDIR /u01/app/oracle

CMD ./startscript.sh

docker build and run commands:

docker build -f Dockerfile-weblogic --tag="weblogic12c:startweb" /var/dprojects
docker rund -d -it weblogic12c:startweb
docker exec -it 6313c4caccd3 bash

Solution

  • Please use supervisord for running multiple services in a docker container. It will make the whole process more robust and reliable. Run supervisord -n as your CMD command and configure all your services in /etc/supervisord.conf.

    Sample conf would look like:

    [program:WebLogic]
    command=/u01/app/oracle/product/wls122100/domains/verdomain/bin/startWebLogic.sh -D
    stderr_logfile = /var/log/supervisord/WebLogic-stderr.log
    stdout_logfile = /var/log/supervisord/WebLogic-stdout.log
    autorestart=unexpected
    
    [program:NodeManager]
    command=/u01/app/oracle/product/wls122100/domains/verdomain/bin/startNodeManager.sh -D
    stderr_logfile = /var/log/supervisord/NodeManager-stderr.log
    stdout_logfile = /var/log/supervisord/NodeManager-stdout.log
    autorestart=unexpected
    

    It will handle all the things you are trying to do with a shell script.
    Hope it helps!