Project

General

Profile

DockerInit » History » Version 6

cryptogopher, 2022-04-28 21:48

1 1 cryptogopher
h1. Container @init@ process
2
3 6 cryptogopher
h2. Problem
4
5 5 cryptogopher
To enable running multiple processes, containers require process/service management. This is normally provided by some kind of @init@ task (e.g. from @sysvinit@).
6 2 cryptogopher
7 4 cryptogopher
There are Docker-compatible replacements for full-fledged @init@'s. Unfortunately they require either custom init scripts or service configurations (https://wiki.gentoo.org/wiki/Comparison_of_init_systems). The process of migration from system provided OpenRC init scripts is time consuming and error prone.
8 2 cryptogopher
9
Usage of system's default @sysvinit@ is hampered by following shortcomings:
10
* it mostly does not respond to Unix signals, which are used by Docker to manage containers (most importantly: signal termination),
11
* it does not stop properly on container stop. Attempt to stop container with @init@ as PID 1 ends with error code 137:
12
<pre>
13 3 cryptogopher
CONTAINER ID  IMAGE        COMMAND       CREATED             STATUS                      PORTS  NAMES
14
b755c0f1b1d8  gentoo-base  "/sbin/init"  About a minute ago  Exited (137) 9 seconds ago         gentoo-base
15 2 cryptogopher
</pre>
16
@init@ process remains running afterwards:
17
<pre>
18
# docker-compose top
19
gentoo-base
20
UID    PID    PPID   C   STIME   TTY     TIME       CMD
21
----------------------------------------------------------
22
root   3510   3489   0   17:40   ?     00:00:00   init [0]
23
</pre>
24
25 6 cryptogopher
26
h2. Solution
27 1 cryptogopher
28 4 cryptogopher
Nevertheless it is possible to use @sysvinit@ inside Docker container as an @init@ process. Required steps are following:
29 3 cryptogopher
30
1. Change @sysvinit@ to exit @init@ process on hard shutdown (runlevel 0) with following patch:
31
<pre><code class="diff">
32
--- sysvinit-3.01/src/init.c    2021-12-13 20:21:26.000000000 +0100
33
+++ sysvinit-3.01/src/init.c    2022-04-18 01:21:47.966751774 +0200
34
@@ -2367,6 +2367,11 @@
35
      read_inittab();
36
      fail_cancel();
37
      setproctitle("init [%c]", (int)runlevel);
38
+
39
+      /*
40
+       * Exit on halt - causes Docker container to stop.
41
+      */
42
+      if (runlevel == '0') exit(0);
43
    }
44
  }
45
        Write_Runlevel_Log(runlevel);
46 1 cryptogopher
</code></pre>
47
48 4 cryptogopher
On Gentoo it's enough to put this patch inside _/etc/portage/patches/sys-apps/sysvinit/exit-on-halt.patch_ and reemerge @sysvinit@.
49
50
2. Change Docker signal for container termination to SIGINT and set appropriate action in @inittab@.
51
52
Container's _docker-compose.yml_:
53
<pre>
54
services:
55
  gentoo-base:
56
    ...
57
    stop_signal: SIGINT
58
</pre>
59
60
_/etc/inittab_ inside container - replace reboot action with shutdown:
61
<pre>
62
# What to do at the "Three Finger Salute".
63
ca:12345:ctrlaltdel:/sbin/shutdown -h now
64
</pre>