Pass container ID to java process as env/system variable

When I do a docker run, I’d like to someone capture the container ID and pass it as a variable to the command I am starting via ENTRYPOINT

for example, i’d like to do something like this.

ENTRYPOINT exec java -jar myjar.jar


The container’s hostname gets set to its ID. The $HOSTNAME environment variable is set as well.

Since you are using ENTRYPOINT, you are sidestepping the normal shell executing that you get when you launch a process via CMD.

Instead of using exec, you could set ENTRYPOINT to /bin/bash -c 'java$HOSTNAME -jar myjar.jar'. Bash will do the expansion of the variable, and java will get the container ID passed in this argument.

It might also make sense, depending on your exact use case, to note use ENTRYPOINT and use CMD instead.


Well I think the issue is that I have to do “exec” because otherwise when the container is given a kill signal it doesn’t make it to my java application (which has a shutdown hook thread). This is why I had to use exec.

Any other way to get hostname into my startup command?