Ritesh
Ritesh

Reputation: 301

Docker Apache graceful shutdown

We are trying to make OS signals (TERM,KILL etc) reach the Apache server ,which is started using a shell script. Here is what we are trying to do :-

1) We have a docker file which ends at :

 CMD ["sh","-c","/apps/scripts/run.sh" ]

The idea is to start apache and hold the docker container onto it .

2) Then we have a shell script run.sh:-

_term() {
  echo "Caught signal!"
  apachectl -k graceful-stop
}

trap _term SIGINT SIGTERM SIGWINCH

apachectl start &

PID=$!

echo "APACHE PID is ${PID}"

wait $PID

trap - SIGINT SIGTERM SIGWINCH

wait $PID

EXIT_STATUS=$?

echo "Exiting with Exit Status of ${EXIT_STATUS}"

The idea is to wait for Apache server and gracefully kill it when it receives SIGTERM/SIGINT/SIGWINCH.

The problems that we are facing is :-

1) Upon running the docker container ,which in turn executes the script , it throws the following error and exits :

wait: pid 102 is not a child of this shell

Seems like the shells script is not considering apachectl start & as it's child process.

How do we ensure that shell script sends the signal to apache?

Any help as to how to go about it would be highly appreciated!

Upvotes: 2

Views: 3028

Answers (1)

red
red

Reputation: 21

Something like dumb-init works well for this. You can use -r 15:28 to rewrite SIGTERM to SIGWICH. SIGWICH is the same as graceful-stop.

So your Dockerfile will have something like: CMD ["dumb-init", "-v", "--rewrite", "15:28", "apachectl -D FOREGROUND"]

Output looks like:

[core:notice] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
[dumb-init] Received signal 15.
[dumb-init] Translating signal 15 to 28.
[dumb-init] Forwarded signal 28 to children.
[mpm_event:notice] AH00492: caught SIGWINCH, shutting down gracefully
[dumb-init] Received signal 17.
[dumb-init] A child with PID 16701 exited with exit status 0.
[dumb-init] Translating signal 15 to 28.
[dumb-init] Forwarded signal 28 to children.
[dumb-init] Child exited with status 0. Goodbye.

Upvotes: 2

Related Questions