docker receiving signals

We have a bash strip that is waiting for a trap on the SIGTERM.

But is does not seem to be reached.

this is because the start command is being run from

ENTRYPOINT exec /usr/local/bin/startup.sh

Here the startup.sh is running in a sub-shell. And the SIGTERM is not propogated.

Run using command

Checking

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a92cf81a744e        tim/startstop:1.0   "/usr/local/bin/star…"   1 second ago        Up 9 seconds                            vigorous_jang

Lets look inside this container....

tim@tims-MBP ~/Dev/Docker_dev $ docker exec a92cf81a744e  ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 08:10 pts/0    00:00:00 /bin/bash /usr/local/bin/startup.sh
root        35     1  0 08:10 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    38    35  0 08:10 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    39    35  0 08:10 ?        00:00:00 /usr/sbin/apache2 -k start
root        96     1  0 08:10 pts/0    00:00:00 /bin/bash /usr/local/bin/loop.sh
root        97    96  0 08:10 pts/0    00:00:00 tail -f /dev/null
root        98     0  0 08:10 ?        00:00:00 ps -ef

I can see PID 1 - running the startup.

Check Containers exit status

docker inspect -f '{{.State.ExitCode}}' a92cf81a744e

You should see 0

Example Startup.sh

This starts the apache2 service then waits for a terminate signal.

If you run interactivly then on Ctrl-C it also quits.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
service apache2 start
echo "Apache2 is now running"

trap cleanup 1 2 3 6 9 15

cleanup()
{
  echo "Caught Signal ... cleaning up."
  service apache2 stop
  sleep 1s
  echo "Done  ... quitting."
  exit 1
}

# wait forever
while true
do
  tail -f /dev/null & wait ${!}
done