I have a Dockerfile
that ends with:
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Now I want to run some initialisation from a shell script before that. So I have to refactor that to:
ENTRYPOINT[ /entrypoint.sh]
With:
!#/bin/bash
echo "some init"
#TODO
#ENTRYOINT["java", "org.springframework.boot.loader.JarLauncher"]
Question: how can I actually simulate the java entrypoint inside the shell script?
2
Answers
ShellScript can be edited to run normal Java file.
From docker documentation:
If you need to write a starter script for a single executable, you can ensure that the final executable receives the Unix signals by using exec
TL;DR: if the entrypoint wrapper ends with the line
exec "$@"
, then it will run the image’sCMD
. If you change theENTRYPOINT
you have now toCMD
then you won’t have to hard-code it in the script.In the plain Dockerfile as you have it now, you can change
ENTRYPOINT
toCMD
with no effect on how you use the container.On its own, this gives you some flexibility; for example, it’s straightforward to peek inside the container, or to launch an interactive shell instead of the normal container process.
Now, if you have
CMD
this way, you can add anENTRYPOINT
wrapper script to it fairly straightforwardly. TheCMD
is passed to theENTRYPOINT
as additional arguments and the shell invocationexec "$@"
will run whatever is passed as theCMD
.Importantly, this works with the alternate
docker run
commands I showed above. The alternatedocker run
command replaces the DockerfileCMD
but leaves theENTRYPOINT
wrapper intact, so the alternate command gets run in theexec "$@"
line at the end.