1

I have a problem running a .sh script inside a docker container. It gives me:

ml_1 | 2018-08-02 08:31:32,680 INFO spawned: 'datagrid' with pid 18

ml_1 | 2018-08-02 08:31:32,686 INFO exited: datagrid (exit status 127; not expected)

ml_1 | 2018-08-02 08:31:38,732 INFO gave up: datagrid entered FATAL state, too many start retries too quickly

My script:

#!/bin/bash

# Description: 

LIB=/home/com/com_server/lib
CONFIG=/home/com/com_server/config
JAVA="/usr/bin/java"
CLASSPATH=${CONFIG}:blackBoard.xml:${CONFIG}:hazelcastContext.xml:${CONFIG}:comAppConfig.xml:${LIB}:com-calc-0.0.1.jar:${LIB}:com-common-0.0.1.jar:${LIB}:com-hazelcast-0.0.1-jar-with-dependencies.jar:${LIB}:escomled-server-0.0.1.jar:${LIB}:spring-webmvc-4.3.0.RELEASE.jar:${LIB}:spring-jdbc-4.3.0.RELEASE.jar:${LIB}:spring-context-4.3.0.RELEASE.jar:${LIB}:hazelcast-spring-3.7.2.jar:${LIB}:hazelcast-client-3.8.3.jar:${LIB}:hazelcast-spring-3.8.3.jar:${LIB}:hazelcast-all-3.8.3.jar:${LIB}:hazelcast-3.8.3.jar:${LIB}:spring-core-4.3.0.RELEASE.jar:${LIB}:spring-beans-4.3.0.RELEASE.jar:${LIB}:log4j-1.2.16.jar:${LIB}:commons-logging-1.2.jar:${LIB}:spring-asm-4.3.0.RELEASE.jar:${LIB}:spring-expression-4.3.0.RELEASE.jar:${LIB}:mysql-connector-java-5.1.34.jar:${LIB}:spring-tx-4.3.0.RELEASE.jar:${LIB}:commons-lang-2.6.jar:${LIB}:mail-1.4.1.jar:${LIB}:commons-digester-2.0.jar:${LIB}:commons-codec-1.4.jar:${LIB}:slf4j-log4j12-1.5.10.jar:${LIB}:slf4j-api-1.5.10.jar:${LIB}:config/createClient.sql:${LIB}:commons-fileupload-1.2.jar:${LIB}:commons-io-1.4.jar:${LIB}:spring-web-4.3.0.RELEASE.jar:${LIB}:opencsv-2.3.jar:${LIB}:spring-aop-4.3.0.RELEASE.jar:${LIB}:aopalliance-1.0.jar:${LIB}:pdfbox-1.8.9.jar:${LIB}:fontbox-1.8.9.jar:${LIB}:jempbox-1.8.9.jar:${LIB}:imgscalr-lib-4.2.jar:${LIB}:dom4j-1.6.1.jar:${LIB}:xmlbeans-2.6.0.jar:${LIB}:jasypt-1.9.2.jar:${LIB}:jackson-mapper-asl-1.9.4.jar:${LIB}:jackson-core-asl-1.9.4.jar:${LIB}:jackson-jaxrs-1.9.4.jar:${LIB}:cglib-2.2.2.jar:${LIB}:asm-3.3.1.jar:${LIB}:textmagic-java-sdk-1.2.0.jar:${LIB}:httpclient-4.5.5.jar:${LIB}:commons-dbcp2-2.3.0.jar:${LIB}:commons-pool2-2.5.0.jar:${LIB}:poi-3.17.jar:${LIB}:poi-ooxml-3.17.jar:${LIB}:commons-collections4-4.1.jar:${LIB}:poi-ooxml-schemas-3.17.jar
SERVER_NAME="com_datagrid"
SLOG_NAME="com_datagrid"
PID_FILE="/var/run/${SLOG_NAME}.pid"
MEMORY="512";
ARGS="-Djava.awt.headless=true -Xmx1024M -Dcom.sun.management.jmxremote=true \
                  -Dcom.sun.management.jmxremote.port=9092 \
                  -Dcom.sun.management.jmxremote.ssl=false \
                  -Dcom.sun.management.jmxremote.authenticate=false \
                  -Djava.rmi.server.hostname=10.72.4.13 -Dhazelcast.diagnostics.enabled=true -Dhazelcast.diagnostics.metric.level=info -Dhazelcast.diagnostics.invocation.sample.period.seconds=30 -Dhazelcast.diagnostics.pending.invocations.period.seconds=30 -Dhazelcast.diagnostics.slowoperations.period.seconds=30"
start() {
     tput setaf 2;echo "---- STARTING *** com_datagrid *** DAEMON ----"
     ${JAVA} ${ARGS} -classpath "${CLASSPATH}" com.com.blackboard.main.Server &
     javaPID=$!
     tput setaf 7;echo "*** com_datagrid *** backgrounded with process id '${javaPID}'"
     #echo ${javaPID} > ${PID_FILE}
}

stop() {
   echo "STOPING *** com_datagrid ***"
   start-stop-daemon --stop \
      --pidfile ${PID_FILE}
}
restart() {
     stop
     start
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
esac

exit 0;

My Dockerfile:

#FROM maven:3.3.9-jdk-8-alpine as build-env
#COPY . /comML
#WORKDIR comML
#RUN mvn package

FROM openjdk:8-jdk-alpine
WORKDIR /comML
ADD . /comML
ADD ./MLScripts/lib comML/lib
ADD ./MLScripts/config com/config
ADD ./MLScripts/config com/home/com/com_server/config
ADD ./MLScripts comML/MLScripts
ADD target/com-machine-learning-0.0.1-SNAPSHOT-jar-with-dependencies.jar comML/lib/com-machine-learning-0.0.1-SNAPSHOT-jar-with-dependencies.jar
COPY ./MLScripts/wrapper.sh /com/MLScripts/wrapper.sh
#CMD ["export", "CLASSPATH=comML/home/com/com_server/config/blackBoard.xml;EscomledML/home/com/com_server/config/appContext.xml;comML/home/com/com_server/config/config.properties;comML/home/com/com_server/config/com.properties;comML/home/com/escomled_server/config/createClient.sql;EscomledML/home/escomled/escomled_server/config/escomled.properties;com/home/com/com_server/config/global_database.sql;comML/home/com/com_server/config/release.sql;comML/home/escomled/com_server/config/taskExecutors.xml;comML/home/com/com_server/config/test-applicationContext.xml;C:/home/com/com_server/lib/spring-jdbc-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/escomled-common-0.0.1.jar;C:/home/escomled/escomled_server/lib/escomled-machine-learning-0.0.1-SNAPSHOT-jar-with-dependencies.jar;C:/home/escomled/escomled_server/lib/assertj-core-3.3.0.jar;C:/home/escomled/escomled_server/lib/curator-test-2.9.0.jar;C:/home/escomled/escomled_server/lib/h2o-genmodel-3.18.0.1.jar;C:/home/escomled/escomled_server/lib/hazelcast-all-3.8.3.jar;C:/home/escomled/escomled_server/lib/jcl-over-slf4j-1.7.1.jar;C:/home/escomled/escomled_server/lib/junit-4.12.jar;C:/home/escomled/escomled_server/lib/spring-jdbc-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-dao-2.0.3.jar;C:/home/escomled/escomled_server/lib/kafka_2.11-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/kafka_2.11-0.10.2.1-cp1-test.jar;C:/home/escomled/escomled_server/lib/kafka-clients-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/spring-context-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-core-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-tx-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/hazelcast-all-3.8.3.jar;C:/home/escomled/escomled_server/lib/kafka-clients-0.10.2.1-cp1-test.jar;C:/home/escomled/escomled_server/lib/kafka-schema-registry-3.2.1.jar;C:/home/escomled/escomled_server/lib/kafka-schema-registry-3.2.1-tests.jar;C:/home/escomled/escomled_server/lib/kafka-streams-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/slf4j-api-1.7.1.jar;C:/home/escomled/escomled_server/lib/slf4j-log4j12-1.7.1.jar"] 
#ENTRYPOINT ["java", "-cp", "EscomledML/home/escomled/escomled_server/config/blackBoard.xml;EscomledML/home/escomled/escomled_server/config/appContext.xml;EscomledML/home/escomled/escomled_server/config/config.properties;EscomledML/home/escomled/escomled_server/config/escomled.properties;EscomledML/home/escomled/escomled_server/config/createClient.sql;EscomledML/home/escomled/escomled_server/config/escomled.properties;EscomledML/home/escomled/escomled_server/config/global_database.sql;EscomledML/home/escomled/escomled_server/config/release.sql;EscomledML/home/escomled/escomled_server/config/taskExecutors.xml;EscomledML/home/escomled/escomled_server/config/test-applicationContext.xml;C:/home/escomled/escomled_server/lib/spring-jdbc-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/escomled-common-0.0.1.jar;C:/home/escomled/escomled_server/lib/escomled-machine-learning-0.0.1-SNAPSHOT-jar-with-dependencies.jar;C:/home/escomled/escomled_server/lib/assertj-core-3.3.0.jar;C:/home/escomled/escomled_server/lib/curator-test-2.9.0.jar;C:/home/escomled/escomled_server/lib/h2o-genmodel-3.18.0.1.jar;C:/home/escomled/escomled_server/lib/hazelcast-all-3.8.3.jar;C:/home/escomled/escomled_server/lib/jcl-over-slf4j-1.7.1.jar;C:/home/escomled/escomled_server/lib/junit-4.12.jar;C:/home/escomled/escomled_server/lib/spring-jdbc-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-dao-2.0.3.jar;C:/home/escomled/escomled_server/lib/kafka_2.11-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/kafka_2.11-0.10.2.1-cp1-test.jar;C:/home/escomled/escomled_server/lib/kafka-clients-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/spring-context-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-core-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/spring-tx-4.3.0.RELEASE.jar;C:/home/escomled/escomled_server/lib/hazelcast-all-3.8.3.jar;C:/home/escomled/escomled_server/lib/kafka-clients-0.10.2.1-cp1-test.jar;C:/home/escomled/escomled_server/lib/kafka-schema-registry-3.2.1.jar;C:/home/escomled/escomled_server/lib/kafka-schema-registry-3.2.1-tests.jar;C:/home/escomled/escomled_server/lib/kafka-streams-0.10.2.1-cp1.jar;C:/home/escomled/escomled_server/lib/slf4j-api-1.7.1.jar;C:/home/escomled/escomled_server/lib/slf4j-log4j12-1.7.1.jar", "com.escomled.machinelearning.ml.Escomled_Streams_H2O_ML"]
#CMD ["java", "-jar", "escomled-machine-learning-0.0.1-SNAPSHOT-jar-with-dependencies.jar"]
EXPOSE 8085
#ENTRYPOINT ["sh", "EscomledML/MLScripts/wrapper.sh"]
#ENTRYPOINT ["sh", "EscomledML/MLScripts/escomled_data_grid.sh"]
#CMD ["sh", "EscomledML/MLScripts/escomled_dirwatcher.sh", "&&", "sh", "EscomledML/MLScripts/escomled_startmap.sh", "&&", "sh", "EscomledML/MLScripts/escomled_ml.sh"]

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD /usr/bin/supervisord -n

And my .bat script:

set LIB=C:/home/escomled/escomled_server/lib
set CONFIG=C:/home/escomled/escomled_server/config
set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_144"
set CLASSPATH=%CONFIG%/blackBoard.xml;%CONFIG%/hazelcastContext.xml;%CONFIG%/escomledAppConfig.xml;%LIB%/escomled-calc-0.0.1.jar;%LIB%/escomled-common-0.0.1.jar;%LIB%/escomled-hazelcast-0.0.1-jar-with-dependencies.jar;%LIB%/escomled-server-0.0.1.jar;%LIB%/spring-webmvc-4.3.0.RELEASE.jar;%LIB%/spring-jdbc-4.3.0.RELEASE.jar;%LIB%/spring-context-4.3.0.RELEASE.jar;%LIB%/hazelcast-spring-3.7.2.jar;%LIB%/hazelcast-client-3.8.3.jar;%LIB%/hazelcast-spring-3.8.3.jar;%LIB%/hazelcast-all-3.8.3.jar;%LIB%/hazelcast-3.8.3.jar;%LIB%/spring-core-4.3.0.RELEASE.jar;%LIB%/spring-beans-4.3.0.RELEASE.jar;%LIB%/log4j-1.2.16.jar;%LIB%/commons-logging-1.2.jar;%LIB%/spring-asm-4.3.0.RELEASE.jar;%LIB%/spring-expression-4.3.0.RELEASE.jar;%LIB%/mysql-connector-java-5.1.34.jar;%LIB%/spring-tx-4.3.0.RELEASE.jar;%LIB%/commons-lang-2.6.jar;%LIB%/mail-1.4.1.jar;%LIB%/commons-digester-2.0.jar;%LIB%/commons-codec-1.4.jar;%LIB%/slf4j-log4j12-1.5.10.jar;%LIB%/slf4j-api-1.5.10.jar;%LIB%/config/createClient.sql;%LIB%/commons-fileupload-1.2.jar;%LIB%/commons-io-1.4.jar;%LIB%/spring-web-4.3.0.RELEASE.jar;%LIB%/opencsv-2.3.jar;%LIB%/spring-aop-4.3.0.RELEASE.jar;%LIB%/aopalliance-1.0.jar;%LIB%/pdfbox-1.8.9.jar;%LIB%/fontbox-1.8.9.jar;%LIB%/jempbox-1.8.9.jar;%LIB%/imgscalr-lib-4.2.jar;%LIB%/dom4j-1.6.1.jar;%LIB%/xmlbeans-2.6.0.jar;%LIB%/jasypt-1.9.2.jar;%LIB%/jackson-mapper-asl-1.9.4.jar;%LIB%/jackson-core-asl-1.9.4.jar;%LIB%/jackson-jaxrs-1.9.4.jar;%LIB%/cglib-2.2.2.jar;%LIB%/asm-3.3.1.jar;%LIB%/textmagic-java-sdk-1.2.0.jar;%LIB%/httpclient-4.5.5.jar;%LIB%/commons-dbcp2-2.3.0.jar;%LIB%/commons-pool2-2.5.0.jar;%LIB%/poi-3.17.jar;%LIB%/poi-ooxml-3.17.jar;%LIB%/commons-collections4-4.1.jar;%LIB%/poi-ooxml-schemas-3.17.jar

set SERVER_NAME="escomled_server"
set SLOG_NAME="escomled_server"
set PID_FILE="/var/run/%SLOG_NAME%.pid"
set MEMORY="512";
 set ARGS="-Djava.awt.headless=true -Xmx1024M -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9092 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=10.72.4.13 -Dhazelcast.diagnostics.enabled=true -Dhazelcast.diagnostics.metric.level=info -Dhazelcast.diagnostics.invocation.sample.period.seconds=30 -Dhazelcast.diagnostics.pending.invocations.period.seconds=30 -Dhazelcast.diagnostics.slowoperations.period.seconds=30"
:start
   ECHO "---- STARTING *** DataGrid *** DAEMON ----"
   %JAVA_HOME%\bin\java.exe %ARGS% -classpath %CLASSPATH% com.escomled.blackboard.main.DataGridServer
   ECHO "*** DataGrid *** backgrounded with process id %javaPID%"
   ECHO %javaPID% > %PID_FILE%
   EXIT /B 0

:stop
   ECHO "STOPING *** DataGrid ***"
   start-stop-daemon --stop --pidfile %PID_FILE%
   EXIT /B 0

:restart
     CALL :stop
     CALL :start

rem case "$1" in
rem  start)
rem        start
rem        ;;
rem  stop)
rem        stop
rem        ;;
rem  restart)
rem        stop
rem        start
rem        ;;
rem esac

rem exit 0;

I will edit the question if I didn't explain something right. Thank you.

7
  • Are you sure that the problem is in the script? The output you provided seems to be a Java log, i.e. the script successfully started the application, but the application itself has failed. Commented Aug 2, 2018 at 10:26
  • I have a .bat script with the application and it works fine. But the .sh script is not working Commented Aug 2, 2018 at 11:24
  • 1
    Is this script started on container start, or you manually execute it on already running container? Can you access database from within container properly? Can you share working .bat script you mention since this is bash script in question? Did you try to restructure the daemon start sequence to foreground as suggested in answer below and can you share exact script. This does looks like application error, so it is missing some config detail... Commented Aug 2, 2018 at 11:33
  • Thanks for the response. I will now edit the question with the Dockerfile and the .bat script. I tried the suggested andswer and it gives the same error. And the problem is 1 Dockerfile can have only 1 command in the foreground Commented Aug 2, 2018 at 11:44
  • 1
    @MilanPanic, .bat is for Windows, so, the problem may be environment-specific (causing the process to die in Linux but not in Windows). I think that the good starting point for troubleshooting this issue is to start the container interactively with /bin/bash cmd, and then trying to start the application manually (just running java ... in the container without any startup script). This will help to determine whether the issue is in environment (OS-specific, Docker-specific, etc.) or somewhere else. Commented Aug 2, 2018 at 11:47

2 Answers 2

3

I have a problem running a .sh script inside a docker container

You have a number of issues here... Just some pointers:

  • Your config file structure is a bit messy, from Dockerfile:

    WORKDIR /EscomledML
    ADD . /EscomledML
    ADD ./MLScripts/lib EscomledML/lib
    ADD ./MLScripts/config EscomledML/config
    ADD ./MLScripts/config EscomledML/home/escomled/escomled_server/config
    ADD ./MLScripts EscomledML/MLScripts
    

    won't really be targeted properly by script since it is expecting it elsewhere:

    LIB=/home/escomled/escomled_server/lib
    CONFIG=/home/escomled/escomled_server/config
    

    Check all file locations to confirm they are properly placed where they should be.

  • Daemon should be started in foreground as main process. Since you have issues starting process in the first place, try starting container interactively with /bin/bash and hammer out the starting script properly before setting it as entrypoint of Dockerfile.

  • You are trying to move from one OS to another. Installation details and environment differences can be the cause of your app not starting properly.

  • You can use build args and env variables directly with docker to avoid initializing them inside script. Make sure that all env vars are properly initialized

Edit after comment:

You are also having issues with multistage build... Artifacts from previous stage you don't transfer to the last one, effectively dumping them. That's why you don't have any folder after last stage. You need something to the effect of COPY --from=0 ... and also check your desired outcome, since you might be misusing multistage build here (primary intent is to build something in previous layer as disposable build stage, not to combine two images into one).

Edit after next comment:

The way I see it, only thing you are using ubuntu image is to install supervisord. If that is the case, then simply replacing following lines:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y supervisor

with:

apk add --update supervisor

Will install supervisord in alpine image and you would be good to go.

Note that your directory structure looks quite messed up (you first ADD . then subfolders etc) so make sure to review those things. I have no idea what is your desired directory structure, but you should be able to make head or tails there...

Sign up to request clarification or add additional context in comments.

7 Comments

In the script the LIB and CONFIG paths, I changed them to LIB=./MLScripts/lib and CINFIG=./MLScripts/config but it is still not working. I have a config nodaemon=true.
Only in first Dockerfile ADD command you are referencing absolute path. From second ADD onwards you are referencing path relative to WORKDIR... Check contents of /EscomledML folder on your container and compare them to your desired state... At this point I can only guess, but I doubt you wanted this command ADD ./MLScripts/lib EscomledML/lib to place files in /EscomledML/EscomledML/lib... Your build config is still a mistery to us so only you can make head or tails of those add commands and desired layout of folders and files in container...
I can't find the folder EscomledML, it is not adding it to the container
Yes, you are not using multistage docker build properly either. Check edit in the answer.
How can I use it properly, but to have the same functionality?
|
1

To a first approximation, commands that launch system services or background processes (service, systemctl, initctl, start-stop-daemon, ...) just don't work in Docker.

If you replace all of your script after the initial environment variable setup with the command that runs the actual server in the foreground

#!/bin/sh
LIB=...
...
ARGS=...
exec ${JAVA} ${ARGS} -classpath "${CLASSPATH}" com.escomled.blackboard.main.DataGridServer

The Docker container will stay running until that server process exits, which is what you usually want.

1 Comment

Thank you for responding. I tried doing it and it is not working again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.