gentoo-syslog/Dockerfile.template: Add tail-syslog (and other tweaks)
The main change here is a new tail-syslog script that traps SIGTERM
and shuts down running OpenRC processes (using 'rc shutdown'). This
gives us clean shutdowns for 'docker stop' and friends, which makes
'docker restart' and host reboots more reliable.
The tail-syslog script is not the most elegant solution. In fact,
rc(8) suggests:
There are some special runlevels that you should be aware of:
...
shutdown Changes to the shutdown runlevel and then halts the host.
You should not call any of these runlevels yourself. Instead you
should use init(8) and shutdown(8) and let them call these special
runlevels.
Indead, using 'CMD /sbin/init' seems to work fine, but I can't figure
out how to get the logs out via 'docker logs'. I tried a number of
things, including
RUN sed -i 's|^\([^#].*/sbin/agetty.*\)|#\1|' /etc/inittab
RUN echo 'log1:12345:respawn:/usr/bin/tail -F /var/log/messages >/dev/console' >> /etc/inittab
but none of my attempts along those lines worked.
You could work around this by volume-mounting a host directory to the
container's /var/log, or by configuring syslog-ng to forward logs to a
remote destination with something like:
RUN sed -i 's/^\(destination messages\).*;$/\1 { tcp("logs.example.net" port(514) };/' /etc/syslog-ng/syslog-ng.conf
But we don't have a central syslog instance to forward to, and I like
'docker logs' ;).
So that nixed the /sbin/init approach, and I started looking at signal
trapping in a shell script [1]. When I tried to use 'shutdown -h now'
in place of 'rc shutdown', I got log messages like:
Mar 12 18:29:14
f50a7b4bd8c9 logger: trapped SIGTERM, shutting down
Mar 12 18:29:14
f50a7b4bd8c9 shutdown[272]: shutting down for system halt
shutdown: /dev/initctl: No such file or directory
init: /dev/initctl: No such file or directory
because /sbin/init (which creates /dev/initctl) was not running).
Using 'rc shutdown' directly avoids that problem.
Also in this commit:
* Add an initial ^ to the console_all sed line, which just makes
explicit the fact that I'm matching from the beginning of the line.
* Enable boot logging to /var/log/rc.log, in case someone actually
does run /sbin/init.
[1]: 'trap' is in POSIX.1-2008 (IEEE Std 1003.1, 2013 Edition)
http://pubs.opengroup.org/onlinepubs/
9699919799/utilities/V3_chap02.html#tag_18_28