spring-guides/gs-spring-boot-docker

SIGTERM doesn't get to JVM process

avarabyeu opened this issue · 8 comments

Example of Dockerfile contains critical issue: SIGTERM doesn't get to the JVM process..
For example, if you use integration with consul, the service is not being de-registered in case of stopping the container.
Basically, It leads to incorrect shutdown of a java application (shutdown hooks are not being executed).
It's strongly recommended to use some script with "exec" command to spawn child processes - this way SIGTERM being propagated to the JVM process and the application can shutdown gracefully.

dsyer commented

Closed via #36

Hi,
I am getting same problem docker stop is shut down hooks are not running.

dsyer commented

Are you using the "exec" entry point (like in the pull request link)? I see the code in main has rotted and no longer does. Also I think the behaviour is OS dependent, so I never had a problem on Linux, for instance.

This is my dockerfile

FROM openjdk:11
ARG projectName=projectName
ARG projectPath=projectPath
ARG httpPort=httpPort

ENV projectName ${projectName}
ENV httpPort ${httpPort}
ENV projectPath ${projectPath}
EXPOSE 9000

RUN echo "projectName :: $projectName"
RUN echo "httpPort :: $httpPort ${httpPort}"
ENV SBT_VERSION 1.5.4
ENV WORKING_DIR /workspace
ENV DEPLOYING_DIR /deployment 
RUN \
  mkdir /dump/ && \
  cd /dump/ && \
  curl -L -o sbt-$SBT_VERSION.deb https://repo.scala-sbt.org/scalasbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  rm -rf /dump/ && \
  apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR $WORKING_DIR
COPY ${projectPath}/app ${WORKING_DIR}/app
COPY ${projectPath}/conf ${WORKING_DIR}/conf
COPY ${projectPath}/session.ser ${WORKING_DIR}/session.ser
COPY ${projectPath}/timezones.txt ${WORKING_DIR}/timezones.txt
COPY ${projectPath}/public ${WORKING_DIR}/public
COPY ${projectPath}/project/build.properties ${WORKING_DIR}/project/build.properties
COPY ${projectPath}/project/plugins.sbt ${WORKING_DIR}/project/plugins.sbt
COPY ${projectPath}/build.sbt ${WORKING_DIR}/build.sbt

RUN sbt clean playUpdateSecret dist

RUN \
  pwd && \
  mkdir /$DEPLOYING_DIR/ && \
  cp target/universal/${projectName}-1.0-SNAPSHOT.zip /$DEPLOYING_DIR && \
  rm -rf $WORKING_DIR
CMD ["sh","-c","echo \"Hi \" && echo ${projectName} && cd /${DEPLOYING_DIR} && rm -rf ${projectName}-1.0-SNAPSHOT && unzip ${projectName}-1.0-SNAPSHOT.zip && cd ${projectName}-1.0-SNAPSHOT && rm -rf RUNNING* && chmod -R a+x bin/ && ls -lart bin/ && ./bin/${projectName} -Dhttp.port=${httpPort}"]
STOPSIGNAL SIGINT

i am using docker compose file in that two services using one is app and another one is watchtower. when watchtower automatically update the images. The container stopping time watchtower send SIGTERN SIGNAL but java shutdown hooks are not running why?

dsyer commented

I'm not sure we can help with your specific problem (the Dockerfile diverges a lot from what is in this guide). Did you try an entry point with "exec" as suggested in the pull request?

I am using CMD in docker file. No exec entry point using.

My main problem is I am create docker image and running the image. when I am enter the Ctrl-c the java shut down hooks are running successfully again image running in detached mode and docker stop <container_id> that time java shut down hooks are not running.
Please help me.