1

I have a docker container with apache running in foreground. On stopping the docker container , a SIGTERM is sent to all the child processes , which is apache in our case.

Now, the problem i am facing is to gracefully shutdown apache on receiving SIGTERM signal. Apache normally terminates on the current requests immediately which is the main cause of the problem . Somehow, i need to translate the SIGTERM signal to SIGWINCH , which would eventually gracefully shutdown the server.

I was thinking of writing some kind of wrapper script , but couldn't get as to how to start.

Any suggestions in this regard would be highly appreciated!

Thanks.

Ritesh
  • 301
  • 1
  • 4
  • 11
  • how are you starting the docker container ? why do you need to run it in foreground? – Rao Oct 28 '16 at 06:14
  • @Rao we are starting the container using CMD ["script.sh"] . The script contains the command to start apache using :- httpd -k start -DFOREGROUND. We need to run it in the foreground in order to get it logs using DOCKER LOGS command . any other way to do that? – Ritesh Oct 28 '16 at 06:37

1 Answers1

1

The tomcat inside of container can be stopped gracefully by issuing below command (change tomcat path if needed):

docker exec -it <container id / name> /usr/local/apache2/bin/apachectl -k graceful

And to your comment, if you want to see the tomcat log in case if it is not running in foreground

docker exec -it <container id / name> tail -f tail -f /usr/local/apache2/logs/error_log

UPDATE: Based on the comments.

From the docker documentation, you may specify the time while stopping the docker container. By default, it will only wait for 10 sec.

To stop container with different timeout:

docker stop -t <time in seconds> <container id/ name>

I believe that, increasing time out while stopping might help in your case.

UPDATE2 sending custom signal, SIGWINCH in your case. Please refer here for more details.

docker kill -s SIGWINCH <apache container id / name>

UPDATE3 There are helpful resources on signal trapping:

https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.qp68kskwd

http://www.techbar.me/stopping-docker-containers-gracefully/

Hope these are helpful.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • we don't have a tomcat application server. We are having apache running on Passenger for Ruby application! – Ritesh Oct 28 '16 at 06:47
  • sorry, assumed apache-tomcat, but never mind. All you need to use right right path to apache. updated answer to use apache2. – Rao Oct 28 '16 at 06:49
  • Thanks..but we acually have 2 things over here:- 1) We are using docker stop to stop the container . so can't specifically just sto apache!! .. We need to somehow intercet SIGTERM (sent by docker stop ) and fire apachectl -k graceful !! 2) We are redirecting the logs to stdout and not any file! Any suggestion ? – Ritesh Oct 28 '16 at 06:53
  • First one is actually you are looking for. 2nd is provided based on you comment why you went for fore ground, just to show you could see what is going on without even in the foreground. If you think this answer is helpful, consider accepting it. – Rao Oct 28 '16 at 06:55
  • Thanks..but we acually have 2 things over here:- 1) We are using docker stop to stop the container . so can't specifically just sto apache!! .. We need to somehow intercet SIGTERM (sent by docker stop ) and fire apachectl -k graceful !! 2) We are redirecting the logs to stdout and not any file! Any suggestion ? – Ritesh Oct 28 '16 at 06:56
  • Is any data loss happening because of `docker stop`? What I believe is that it is not so special to this case, in fact applicable to every case. And container is stopped only after the process running inside of it is stopped. If apache is running in the background, you may not land into #2. Give a trying running in background. – Rao Oct 28 '16 at 06:59
  • Actually apache stops abruptly killing all the request it is serving at that time!.. This isn't good. We wanted to gracefully stop it!.. As in it should wait for requests to complete and not take any further requests AND shutsdown once all the requests are done! Any suggestions? – Ritesh Oct 28 '16 at 07:02
  • @Ritesh, please check the updated answer. In essence, process is taking more time to shutdown gracefully. Docker will only wait for 10 sec, then send kill signal. Increasing time out will help, I believe in your case. You need to come up with right time for your container to property shutdown. – Rao Oct 28 '16 at 07:16
  • Thanks , but apache works in a different manner . As soon as it gets a SIGTERM , which it would get because of SIGTERM , it would eventually and immediately kill itself and all it's child processes without waiting for any pending requests to complete. We need to somehow intercept SIGTERM and fire apachectl stop graceful command! – Ritesh Oct 28 '16 at 07:25
  • @Ritesh, please check the answer `UPDATE2` to send the custom signal. I belie this will help. – Rao Oct 28 '16 at 08:30
  • Thanks Rao, but i need to use docker stop command! Any clues ? ..Can u help with writing a wrapper script which can trap SIGTERM and send SIGWINCH to apache? – Ritesh Oct 28 '16 at 08:36
  • See there are two ways to stop docker, namely stop and kill. And `stop` with timeout is also not helping you. To overwrite default signal `SIGTERM` to different value, `kill` is the option. You might seen the details in the given link. Do not just go by key `kill`. Can you first try and update please? – Rao Oct 28 '16 at 08:39
  • yes SIGWINCH does work. But we have many docker containers running ane need to have single docker stop command, Any clues? – Ritesh Oct 28 '16 at 09:05
  • May be this is different question. Please consider creating new question. If you think the answer is helpful consider up vote & accept it as answer. And thank you for patiently trying out though. – Rao Oct 28 '16 at 09:08
  • Thanks @Rao for replying each time..! Maybe this is not the one i was looking for. Anyways thanks a lot for your time! – Ritesh Oct 28 '16 at 09:11
  • Well, sending the signal as per the `UPDATE2`, you said it is working. – Rao Oct 28 '16 at 09:13
  • @Ritesh, added some more additional resources. See if those are helpful. And you simply said this is not i am looking for, after all though it is working, when requested for accept answer if helpful. Can be you specific what you are looking for ? interested to know. – Rao Oct 28 '16 at 09:25