I have a process in a docker container that is supposed to log a thread dump (on stdout) when shut down. However when docker stop is called on it, no output happens.

It seems that either the process doesn't receive the TERM signal or the output is detached before the shutdown is complete.

I'm using only a CMD (no ENTRYPOINT) in my Dockerfile:

CMD java ${MY_JAVA_OPTS} my-uberjar.jar

How do I get output on shutdown working?


The entry point of the docker container is a shell calling that CMD, because it is in shell form.

This form of calling doesn't propagate signals, however.

You need to call the process using exec, which can be done by either using the exec form:

CMD ["java", "-Doption=value", "my-uberjar.jar"]

or by using exec explicitly:

CMD exec java ${MY_JAVA_OPTS} my-uberjar.jar

This results in the shell replacing itself with the started process, which then receives the signals.

The exec form doesn't do shell expansion, though (because there is no shell), so if you need that, you'd have to wrap a shell invocation and exec again:

CMD ["/bin/sh", "-c", "exec", "java", "${MY_JAVA_OPTS}", ...

or use the shell form with exec as above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.