ashishb
ashishb

Reputation: 81

Run java program and shell script on entrypoint in Dockerfile

I have base docker image where I have a Dockerfile which has an entry like follows

CMD ["/bootstrap.sh"]

In my child docker image where I am referencing this parent docker image, I have a separate entrypoint which starts my container as java program like below

ENTRYPOINT exec java -Duser.timezone=GMT -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar

When I build this child docker image and try to run it, only the java program is coming up. bootstrap,sh is not building up.

After going through some blogs and stackoverflow posts, I cam to know in the child image, CMD context from parent image is lost and overwritten by child image entrypoint. However, I would like to run both shell script as well as java program on entrypoint. In my shell script, I jhave few executables reference which I require it to run and they will keep running the shell programs in background which my Java code will call to and do the processing based on these shell programs.

I also did try to combine both of these steps into one entrypoint like below but then this is only starting up one process - either shell script or Java program depending on which is specified first in dockerfile

ENTRYPOINT /bootstrap.sh && exec java -Duser.timezone=GMT -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar

How can I specify in Dockerfile so that it can execute both the processes on startup of the container. bootstrap.sh can be run in background also.

Please help me on this one.

Upvotes: 0

Views: 6329

Answers (1)

ashishb
ashishb

Reputation: 81

For those who are struggling, I was somehow able to resolve this issue. I don't know if this is the perfect solution but it worked for me.

What I did was I created another shell script file and I called bootstrap.sh from here followed by Java program.

Script file was something like this:

#!/bin/bash

echo "[server-startup] Executing bootstrap script"
/bootstrap.sh &

echo "[server-startup] Starting java application"
exec java -Duser.timezone=GMT -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar

And my Dockerfile entry was modified like below where it called this new shell script file created above.

ENTRYPOINT ["/server-start.sh"]

Hope this helps who's looking for the answer.

Upvotes: 3

Related Questions