Skip to content

Commit 333e51d

Browse files
committed
runtime: Add more steps to the lifecycle docs
And explain how the handling differs when a new start-call joins an existing container vs. creating a new container. This is spun off from the mailing list thread now that that's settled down [1]. This write-up is just about Linux containers, since I don't understand the other systems well enough to do them justice. [1]: https://groups.google.com/a/opencontainers.org/forum/#!topic/dev/frUXLljXy8Y Message-ID: <20150915034717.GO18018@odin.tremily.us> Signed-off-by: W. Trevor King <wking@tremily.us>
1 parent bfe1aba commit 333e51d

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

runtime-config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Hook paths are absolute and are executed from the host's filesystem.
1818

1919
### Pre-start
2020

21-
The pre-start hooks are called after the container process is spawned, but before the user supplied command is executed.
21+
The pre-start hooks are called [after the container process is spawned, but before the user supplied command is executed](runtime.md#typical-lifecycle).
2222
They are called after the container namespaces are created on Linux, so they provide an opportunity to customize the container.
2323
In Linux, for e.g., the network namespace could be configured in this hook.
2424

25-
If a hook returns a non-zero exit code, then an error including the exit code and the stderr is returned to the caller and the container is torn down.
25+
If a hook returns a non-zero exit code, [then an error including the exit code and the stderr is returned to the caller and the container is torn down](runtime.md#typical-lifecycle).
2626

2727
### Post-start
2828

@@ -33,7 +33,7 @@ If a hook returns a non-zero exit code, then an error is logged and the remainin
3333

3434
### Post-stop
3535

36-
The post-stop hooks are called after the container process is stopped.
36+
The post-stop hooks are called [after the container process is stopped](runtime.md#typical-lifecycle).
3737
Cleanup or debugging could be performed in such a hook.
3838
If a hook returns a non-zero exit code, then an error is logged and the remaining hooks are executed.
3939

runtime.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,59 @@ The root directory to the bundle is provided in the state so that consumers can
2929
}
3030
```
3131

32-
## Lifecycle
32+
## Typical lifecycle
33+
34+
A typical lifecyle progresses like this:
35+
36+
1. There is no container
37+
2. A user tells the runtime to start a container and launch a process inside it
38+
3. The runtime [creates the container](#create)
39+
4. The runtime executes any [pre-start hooks](runtime-config.md#pre-start)
40+
5. The runtime [executes the container process](#start-process)
41+
6. The container process is running
42+
7. A user tells the runtime to send a termination signal to the container process
43+
8. The runtime [sends a termination signal to the container process](#stop-process)
44+
9. The container process exits
45+
10. The runtime [terminates any other processes in the container](#stop-process)
46+
11. The runtime executes any [post-stop hooks](runtime-config.md#post-stop)
47+
12. The runtime [removes the container](#cleanup)
48+
49+
With steps 7 and 8, the user is explicitly stopping the container process (via the runtime), but it's also possible that the container process could exit for other reasons.
50+
In that case we skip directly from 6 to [10](#stop-process).
51+
52+
Failure in a pre-start hook or other setup task can cause a jump straight to [11](runtime-config.md#post-stop).
3353

3454
### Create
3555

36-
Creates the container: file system, namespaces, cgroups, capabilities.
56+
Create the container: file system, namespaces, cgroups, capabilities, etc.
57+
The invoked process forks, with one branch that stays in the host namespace and another that enters the container.
58+
The host process carries out all container setup actions, and continues running for the life of the container so it can perform teardown after the container process exits.
59+
The container process changes users and drops privileges in preparation for the container process start.
60+
At this point, the host process writes the [`state.json`](#state) file with the host-side version of the container-process's PID (the container process may be in a PID namespace).
3761

3862
### Start (process)
3963

40-
Runs a process in a container.
41-
Can be invoked several times.
64+
After the pre-start hooks complete, the host process signals the container process to execute the runtime.
65+
The runtime execs the process defined in `config.json`'s [**`process`** attribute](config.md#process-configuration).
66+
On Linux hosts, some information for this execution may come from outside the `config.json` and `runtime.json` specifications.
67+
See the [Linux-specific notes for details](runtime-linux.md#file-descriptors).
4268

4369
### Stop (process)
4470

45-
Not sure we need that from runc cli.
46-
Process is killed from the outside.
71+
Send a termination signal to the container process (can optionally send other signals to the container process, e.g. a kill signal).
72+
When the process exits, the host process collects it's exit status to return as its own exit status.
73+
If there are any remaining processes in the container's cgroup (and [we only support unified-hierarchies](runtime-config-linux.md#control-groups)), the host process kills and reaps them.
74+
75+
### Cleanup
76+
77+
The host process removes the [`state.json`](#state) file and the container: unmounting file systems, removing namespaces, etc.
78+
This is the inverse of create.
79+
The host process then exits with the container processes's exit status.
4780

48-
This event needs to be captured by runc to run onstop event handlers.
81+
## Joining existing containers
4982

50-
## Hooks
83+
Joining an existing container looks just like the usual workflow, except that the container process [joins the target container](runtime-config-linux.md#control-groups) at the beginning of step 3.
84+
It can then, depending on its configuration, continue to create an additional child cgroup underneath the one it joined.
5185

52-
See [runtime configuration for hooks](./runtime-config.md)
86+
When exiting, the reaping logic in the [stop phase](#stop-process) is the same.
87+
If the container process created a child cgroup, all other processes in that child cgroup are reaped, but no other processes in the joined cgroup (which the container process did not create) are reaped.

0 commit comments

Comments
 (0)