Docker Community Forums

Share and learn in the Docker community.

Pass container ID to java process as env/system variable


(Bitsofinfo) #1

Hi,
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 -Ddocker.container.id=HERE_IS_WHERE_I_NEED_THE_CONTAINER_ID -jar myjar.jar

(Jeff Anderson) #2

Hello,

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 -Ddocker.container.id=$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.

/Jeff


(Bitsofinfo) #3

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?