How can I force the container to wait until a graceful shutdown completes?

My Dockerfile ends with this line:
ENTRYPOINT [“tini”, “–”, “/opt/payara/scripts/entrypoint.sh”]

I added in my entrypoint.sh script:

Doing a Graceful Shutdown before container stops

trap ‘echo “Received SIGTERM. Stopping Payara…”; ${PAYARA_DIR}/bin/asadmin --user=${ADMIN_USER} --passwordfile=${PASSWORD_FILE} stop-domain ${DOMAIN_NAME}; echo “Payara stopped. Exiting gracefully”;’ SIGTERM

What happens is that the command starts, but the container is stop in 2 or 3 seconds. So the logs for the stop-domain commands are incomplete.

I would like to say: “hey docker waits 30s before stop it abruptelly”. So I tried doing for example:

docker stop -t 30 <container_id>
or
adding sleep 30 in the trap command
or
docker run --stop-timeout 30 <image_name>

None of these worked for me.

I am running it in WSL with Docker Desktop (Windows 11)

In case a container is stopped, PID1 inside the container receives a SIGTERM signal, which initiates a graceful shutdown (if the process handles it). If the container did not terminate gracefully within 10
seconds, the process receives a SIGKILL and will be terminated.

Some processes require a different termination signal than SIGTERM. You should find it mentioned in the documentation of the application/service if it requires a different termination signal.

Afaik, the docker stop -t and docker run --stop-timeout only make sense if your container process already receives the correct signal, but requires more than 10 seconds to gracefully terminate the process.

If your container stops after 2-3 seconds, it means it terminated gracefully, and was not killed by a SIGKILL.

It is perfectly possible to create an image that creates containers where the main process is not started as PID1. If such an image does not use a process manager like tini, supervisord, or s6 overlay, then the SIGTERM will never reach the process, and the container will always be killed with SIGKILL.