|
| 1 | +# Executing additional processes inside an existing container |
| 2 | + |
| 3 | +The [start operation][start] is described for creating new containers, but you can also use it to execute additional processes inside an existing container. |
| 4 | +This sort of execution is similar to [`nsenter`][nsenter.1]—which joins existing namespaces and executes a new process inside them—but also allows you to easily join existing [control groups][cgroups], etc. |
| 5 | + |
| 6 | +Because [start][] creates a new container, you'll need to give it a container ID, even if your new process is joining the existing container's sandbox with no additional isolation. |
| 7 | + |
| 8 | +The [start][] configuration will look like: |
| 9 | + |
| 10 | +## Example (Linux) |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "ociVersion": "0.5.0", |
| 15 | + "platform": { |
| 16 | + "os": "linux", |
| 17 | + "arch": "amd64" |
| 18 | + }, |
| 19 | + "process": { |
| 20 | + "terminal": true, |
| 21 | + "user": { |
| 22 | + "uid": 0, |
| 23 | + "gid": 0 |
| 24 | + }, |
| 25 | + "args": [ |
| 26 | + "sh" |
| 27 | + ], |
| 28 | + "env": [ |
| 29 | + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", |
| 30 | + "TERM=xterm" |
| 31 | + ], |
| 32 | + "cwd": "/", |
| 33 | + "capabilities": [ |
| 34 | + "CAP_AUDIT_WRITE", |
| 35 | + "CAP_KILL" |
| 36 | + ] |
| 37 | + }, |
| 38 | + "root": { |
| 39 | + "path": "rootfs" |
| 40 | + }, |
| 41 | + "linux": { |
| 42 | + "cgroupsPath": "/myRuntime/myContainer", |
| 43 | + "namespaces": [ |
| 44 | + { |
| 45 | + "type": "user", |
| 46 | + "path": "/proc/1234/ns/user" |
| 47 | + }, |
| 48 | + { |
| 49 | + "type": "pid", |
| 50 | + "path": "/proc/1234/ns/pid" |
| 51 | + }, |
| 52 | + { |
| 53 | + "type": "network", |
| 54 | + "path": "/proc/1234/ns/net" |
| 55 | + }, |
| 56 | + { |
| 57 | + "type": "ipc", |
| 58 | + "path": "/proc/1234/ns/ipc" |
| 59 | + }, |
| 60 | + { |
| 61 | + "type": "uts", |
| 62 | + "path": "/proc/1234/ns/uts" |
| 63 | + }, |
| 64 | + { |
| 65 | + "type": "mount", |
| 66 | + "path": "/proc/1234/ns/mnt" |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Process |
| 74 | + |
| 75 | +You can use whichever [**`process`**][process] settings you like. |
| 76 | + |
| 77 | +### Root |
| 78 | + |
| 79 | +The [**`root.path`**][root] value is arbitrary. |
| 80 | +You must set a value because the field is currently [required][root], but [unless you are creating a new mount namespace the value is ignored][ignored-root]. |
| 81 | + |
| 82 | +### Control group and namespace paths |
| 83 | + |
| 84 | +The [**`linux.cgroupsPath`**][cgroups] and [**`linux.namespaces[].path`**][namespaces] values can be extracted from [proc][proc.5]: |
| 85 | + |
| 86 | +1. Get the [state JSON][state] for the target container using a [state query][state-query]. |
| 87 | + The process ID for the target container is the **`pid`** field (e.g. `1234`). |
| 88 | +2. Using a [proc][proc.5] mount for the appropriate [PID namespace][pid_namespaces.7] (possibly just `/proc`), find the `{pid}` entry for the target container (e.g. `/proc/1234`). |
| 89 | +3. The namespace paths are in the [`ns`][proc.5] subdirectory (e.g. `/proc/1234/ns/user`, `/proc/1234/ns/pid`, …). |
| 90 | +4. The cgroup paths are in the [`cgroup`][proc.5] file (e.g. `/proc/1234/cgroup`). |
| 91 | + Pick whichever path makes the most sense to you; containers created via OCI runtimes will [have the same path for all controllers][cgroups]. |
| 92 | + |
| 93 | +[cgroups]: ../config-linux.md#control-groups |
| 94 | +[namespaces]: ../config-linux.md#namespaces |
| 95 | +[process]: ../config.md#process-configuration |
| 96 | +[root]: ../config.md#root-configuration |
| 97 | +[start]: ../runtime.md#start |
| 98 | +[state]: ../runtime.md#state |
| 99 | +[state-query]: ../runtime.md#query-state |
| 100 | + |
| 101 | +[ignored-root]: https://github.com/opencontainers/runtime-spec/pull/388#issuecomment-212188661 |
| 102 | + |
| 103 | +[nsenter.1]: http://man7.org/linux/man-pages/man1/nsenter.1.html |
| 104 | +[proc.5]: http://man7.org/linux/man-pages/man5/proc.5.html |
| 105 | +[pid_namespaces.7]: http://man7.org/linux/man-pages/man7/pid_namespaces.7.html |
0 commit comments