| 1 | # <a name="runtimeAndLifecycle" />Runtime and Lifecycle |
| 2 | |
| 3 | ## <a name="runtimeScopeContainer" />Scope of a Container |
| 4 | |
| 5 | The entity using a runtime to create a container MUST be able to use the operations defined in this specification against that same container. |
| 6 | Whether other entities using the same, or other, instance of the runtime can see that container is out of scope of this specification. |
| 7 | |
| 8 | ## <a name="runtimeState" />State |
| 9 | |
| 10 | The state of a container includes the following properties: |
| 11 | |
| 12 | * **`ociVersion`** (string, REQUIRED) is version of the Open Container Initiative Runtime Specification with which the state complies. |
| 13 | * **`id`** (string, REQUIRED) is the container's ID. |
| 14 | This MUST be unique across all containers on this host. |
| 15 | There is no requirement that it be unique across hosts. |
| 16 | * **`status`** (string, REQUIRED) is the runtime state of the container. |
| 17 | The value MAY be one of: |
| 18 | |
| 19 | * `creating`: the container is being created (step 2 in the [lifecycle](#lifecycle)) |
| 20 | * `created`: the runtime has finished the [create operation](#create) (after step 2 in the [lifecycle](#lifecycle)), and the container process has neither exited nor executed the user-specified program |
| 21 | * `running`: the container process has executed the user-specified program but has not exited (after step 8 in the [lifecycle](#lifecycle)) |
| 22 | * `stopped`: the container process has exited (step 10 in the [lifecycle](#lifecycle)) |
| 23 | |
| 24 | Additional values MAY be defined by the runtime, however, they MUST be used to represent new runtime states not defined above. |
| 25 | * **`pid`** (int, REQUIRED when `status` is `created` or `running` on Linux, OPTIONAL on other platforms) is the ID of the container process. |
| 26 | For hooks executed in the runtime namespace, it is the pid as seen by the runtime. |
| 27 | For hooks executed in the container namespace, it is the pid as seen by the container. |
| 28 | * **`bundle`** (string, REQUIRED) is the absolute path to the container's bundle directory. |
| 29 | This is provided so that consumers can find the container's configuration and root filesystem on the host. |
| 30 | * **`annotations`** (map, OPTIONAL) contains the list of annotations associated with the container. |
| 31 | If no annotations were provided then this property MAY either be absent or an empty map. |
| 32 | |
| 33 | The state MAY include additional properties. |
| 34 | |
| 35 | When serialized in JSON, the format MUST adhere to the JSON Schema [`schema/state-schema.json`](schema/state-schema.json). |
| 36 | |
| 37 | See [Query State](#query-state) for information on retrieving the state of a container. |
| 38 | |
| 39 | ### Example |
| 40 | |
| 41 | ```json |
| 42 | { |
| 43 | "ociVersion": "0.2.0", |
| 44 | "id": "oci-container1", |
| 45 | "status": "running", |
| 46 | "pid": 4422, |
| 47 | "bundle": "/containers/redis", |
| 48 | "annotations": { |
| 49 | "myKey": "myValue" |
| 50 | } |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | ## <a name="runtimeLifecycle" />Lifecycle |
| 55 | The lifecycle describes the timeline of events that happen from when a container is created to when it ceases to exist. |
| 56 | |
| 57 | 1. OCI compliant runtime's [`create`](runtime.md#create) command is invoked with a reference to the location of the bundle and a unique identifier. |
| 58 | 2. The container's runtime environment MUST be created according to the configuration in [`config.json`](config.md). |
| 59 | If the runtime is unable to create the environment specified in the [`config.json`](config.md), it MUST [generate an error](#errors). |
| 60 | While the resources requested in the [`config.json`](config.md) MUST be created, the user-specified program (from [`process`](config.md#process)) MUST NOT be run at this time. |
| 61 | Any updates to [`config.json`](config.md) after this step MUST NOT affect the container. |
| 62 | 3. The [`prestart` hooks](config.md#prestart) MUST be invoked by the runtime. |
| 63 | If any `prestart` hook fails, the runtime MUST [generate an error](#errors), stop the container, and continue the lifecycle at step 12. |
| 64 | 4. The [`createRuntime` hooks](config.md#createRuntime-hooks) MUST be invoked by the runtime. |
| 65 | If any `createRuntime` hook fails, the runtime MUST [generate an error](#errors), stop the container, and continue the lifecycle at step 12. |
| 66 | 5. The [`createContainer` hooks](config.md#createContainer-hooks) MUST be invoked by the runtime. |
| 67 | If any `createContainer` hook fails, the runtime MUST [generate an error](#errors), stop the container, and continue the lifecycle at step 12. |
| 68 | 6. Runtime's [`start`](runtime.md#start) command is invoked with the unique identifier of the container. |
| 69 | 7. The [`startContainer` hooks](config.md#startContainer-hooks) MUST be invoked by the runtime. |
| 70 | If any `startContainer` hook fails, the runtime MUST [generate an error](#errors), stop the container, and continue the lifecycle at step 12. |
| 71 | 8. The runtime MUST run the user-specified program, as specified by [`process`](config.md#process). |
| 72 | 9. The [`poststart` hooks](config.md#poststart) MUST be invoked by the runtime. |
| 73 | If any `poststart` hook fails, the runtime MUST [log a warning](#warnings), but the remaining hooks and lifecycle continue as if the hook had succeeded. |
| 74 | 10. The container process exits. |
| 75 | This MAY happen due to erroring out, exiting, crashing or the runtime's [`kill`](runtime.md#kill) operation being invoked. |
| 76 | 11. Runtime's [`delete`](runtime.md#delete) command is invoked with the unique identifier of the container. |
| 77 | 12. The container MUST be destroyed by undoing the steps performed during create phase (step 2). |
| 78 | 13. The [`poststop` hooks](config.md#poststop) MUST be invoked by the runtime. |
| 79 | If any `poststop` hook fails, the runtime MUST [log a warning](#warnings), but the remaining hooks and lifecycle continue as if the hook had succeeded. |
| 80 | |
| 81 | ## <a name="runtimeErrors" />Errors |
| 82 | |
| 83 | In cases where the specified operation generates an error, this specification does not mandate how, or even if, that error is returned or exposed to the user of an implementation. |
| 84 | Unless otherwise stated, generating an error MUST leave the state of the environment as if the operation were never attempted - modulo any possible trivial ancillary changes such as logging. |
| 85 | |
| 86 | ## <a name="runtimeWarnings" />Warnings |
| 87 | |
| 88 | In cases where the specified operation logs a warning, this specification does not mandate how, or even if, that warning is returned or exposed to the user of an implementation. |
| 89 | Unless otherwise stated, logging a warning does not change the flow of the operation; it MUST continue as if the warning had not been logged. |
| 90 | |
| 91 | ## <a name="runtimeOperations" />Operations |
| 92 | |
| 93 | Unless otherwise stated, runtimes MUST support the following operations. |
| 94 | |
| 95 | Note: these operations are not specifying any command-line APIs, and the parameters are inputs for general operations. |
| 96 | |
| 97 | ### <a name="runtimeQueryState" />Query State |
| 98 | |
| 99 | `state <container-id>` |
| 100 | |
| 101 | This operation MUST [generate an error](#errors) if it is not provided the ID of a container. |
| 102 | Attempting to query a container that does not exist MUST [generate an error](#errors). |
| 103 | This operation MUST return the state of a container as specified in the [State](#state) section. |
| 104 | |
| 105 | ### <a name="runtimeCreate" />Create |
| 106 | |
| 107 | `create <container-id> <path-to-bundle>` |
| 108 | |
| 109 | This operation MUST [generate an error](#errors) if it is not provided a path to the bundle and the container ID to associate with the container. |
| 110 | If the ID provided is not unique across all containers within the scope of the runtime, or is not valid in any other way, the implementation MUST [generate an error](#errors) and a new container MUST NOT be created. |
| 111 | This operation MUST create a new container. |
| 112 | |
| 113 | All of the properties configured in [`config.json`](config.md) except for [`process`](config.md#process) MUST be applied. |
| 114 | [`process.args`](config.md#process) MUST NOT be applied until triggered by the [`start`](#start) operation. |
| 115 | The remaining `process` properties MAY be applied by this operation. |
| 116 | If the runtime cannot apply a property as specified in the [configuration](config.md), it MUST [generate an error](#errors) and a new container MUST NOT be created. |
| 117 | |
| 118 | The runtime MAY validate `config.json` against this spec, either generically or with respect to the local system capabilities, before creating the container ([step 2](#lifecycle)). |
| 119 | [Runtime callers](glossary.md#runtime-caller) who are interested in pre-create validation can run [bundle-validation tools](implementations.md#testing--tools) before invoking the create operation. |
| 120 | |
| 121 | Any changes made to the [`config.json`](config.md) file after this operation will not have an effect on the container. |
| 122 | |
| 123 | ### <a name="runtimeStart" />Start |
| 124 | `start <container-id>` |
| 125 | |
| 126 | This operation MUST [generate an error](#errors) if it is not provided the container ID. |
| 127 | Attempting to `start` a container that is not [`created`](#state) MUST have no effect on the container and MUST [generate an error](#errors). |
| 128 | This operation MUST run the user-specified program as specified by [`process`](config.md#process). |
| 129 | This operation MUST generate an error if `process` was not set. |
| 130 | |
| 131 | ### <a name="runtimeKill" />Kill |
| 132 | `kill <container-id> <signal>` |
| 133 | |
| 134 | This operation MUST [generate an error](#errors) if it is not provided the container ID. |
| 135 | Attempting to send a signal to a container that is neither [`created` nor `running`](#state) MUST have no effect on the container and MUST [generate an error](#errors). |
| 136 | This operation MUST send the specified signal to the container process. |
| 137 | |
| 138 | ### <a name="runtimeDelete" />Delete |
| 139 | `delete <container-id>` |
| 140 | |
| 141 | This operation MUST [generate an error](#errors) if it is not provided the container ID. |
| 142 | Attempting to `delete` a container that is not [`stopped`](#state) MUST have no effect on the container and MUST [generate an error](#errors). |
| 143 | Deleting a container MUST delete the resources that were created during the `create` step. |
| 144 | Note that resources associated with the container, but not created by this container, MUST NOT be deleted. |
| 145 | Once a container is deleted its ID MAY be used by a subsequent container. |
| 146 | |
| 147 | |
| 148 | ## <a name="runtimeHooks" />Hooks |
| 149 | Many of the operations specified in this specification have "hooks" that allow for additional actions to be taken before or after each operation. |
| 150 | See [runtime configuration for hooks](./config.md#posix-platform-hooks) for more information. |