Search code examples
shelldockerjvm-argumentsmaven-jetty-pluginsigterm

mvn jetty:run-forked inside a docker container?


I have an application that uses the jetty maven plugin "run-forked" goal that I need to dockerize. What happens is that maven starts, the container exists only for about 10 seconds and then dies when maven exits after it forks the child JVM process.

I have investigated many options. One option that I thought might work is to set "waitForChild" to true and then do something like this:

ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "jetty:run-forked > /tmp/log 2>&1" ]

But, though this keeps maven running, the image does not build, because Docker waits for a SIGTERM.

If you are wondering why I need to use jetty:run-forked, it is because the code requires a static linked library that needs a JVM.

I am ready to throw in the towel, because this seems impossible ...


Solution

  • I'm not entirely sure about your java set up, but a neat trick that works is something like this:

    In your dockerfile, add a custom script like so:

    COPY myscript.sh /bin/myscript.sh # Remember to make this executable!
    

    then edit your ENTRYPOINT to reflect that:

    ENTRYPOINT ["/bin/myscript.sh"]
    

    Your myscript.sh could look a little something like this:

    #!/bin/bash
    
    # Run Java/mvn commands here
    ...
    jetty:run-forked > /tmp/log 2>&1
    
    # Throw in a shell command that simply executes forever
    tail -f /dev/null
    

    This will ensure your container keeps running even after your jetty/mvn stuff spawns another process and quits, because it is no longer PID 1 within the container, the myscript.sh shell script is. This shell script continues to run forever because of the tail -f.