0

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.

ashishb
  • 81
  • 2
  • 11
  • 1
    Usually if you need two processes, it's best to start them as two separate containers. I'd recommend changing the JVM invocation to `CMD java ...`, which will make it easy to run a second container with an alternate command `docker run ... the-image /bootstrap.sh`. – David Maze Oct 10 '20 at 11:32
  • This docker finally be run in both docker-compose as well and in kubernetes. docker run will be to start the container manually. What will be the equivalent in docker-compose and Kubernetes then where I can provide /bootstrap.sh to kick in? – ashishb Oct 10 '20 at 14:27
  • Fully agree with @DavidMaze: your `java` program on the one hand, and your services launched by `bootstrap.sh` should be in two different containers. BTW, note that an `ENTRYPOINT` command should always be *in exec form* (i.e., with `["…", "…"]`), otherwise the `CMD` spec is ignored (even if you specify `CMD` *after* `ENTRYPOINT`). For details, see that other SO thread [CMD doesn't run after ENTRYPOINT in Dockerfile](https://stackoverflow.com/q/54447913/9164010). – ErikMD Oct 10 '20 at 14:32
  • did you find a way to run both java and shell script together in one command – subhajit biswas Mar 19 '21 at 15:04

1 Answers1

3

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.

ashishb
  • 81
  • 2
  • 11