github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/experimental/plugin_api.md (about) 1 # Experimental: Docker Plugin API 2 3 Docker plugins are out-of-process extensions which add capabilities to the 4 Docker Engine. 5 6 This page is intended for people who want to develop their own Docker plugin. 7 If you just want to learn about or use Docker plugins, look 8 [here](/experimental/plugins.md). 9 10 This is an experimental feature. For information on installing and using experimental features, see [the experimental feature overview](README.md). 11 12 ## What plugins are 13 14 A plugin is a process running on the same docker host as the docker daemon, 15 which registers itself by placing a file in `/usr/share/docker/plugins` (the 16 "plugin directory"). 17 18 Plugins have human-readable names, which are short, lowercase strings. For 19 example, `flocker` or `weave`. 20 21 Plugins can run inside or outside containers. Currently running them outside 22 containers is recommended. 23 24 ## Plugin discovery 25 26 Docker discovers plugins by looking for them in the plugin directory whenever a 27 user or container tries to use one by name. 28 29 There are two types of files which can be put in the plugin directory. 30 31 * `.sock` files are UNIX domain sockets. 32 * `.spec` files are text files containing a URL, such as `unix:///other.sock`. 33 34 The name of the file (excluding the extension) determines the plugin name. 35 36 For example, the `flocker` plugin might create a UNIX socket at 37 `/usr/share/docker/plugins/flocker.sock`. 38 39 Plugins must be run locally on the same machine as the Docker daemon. UNIX 40 domain sockets are strongly encouraged for security reasons. 41 42 ## Plugin lifecycle 43 44 Plugins should be started before Docker, and stopped after Docker. For 45 example, when packaging a plugin for a platform which supports `systemd`, you 46 might use [`systemd` dependencies]( 47 http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=) to 48 manage startup and shutdown order. 49 50 When upgrading a plugin, you should first stop the Docker daemon, upgrade the 51 plugin, then start Docker again. 52 53 If a plugin is packaged as a container, this may cause issues. Plugins as 54 containers are currently considered experimental due to these shutdown/startup 55 ordering issues. These issues are mitigated by plugin retries (see below). 56 57 ## Plugin activation 58 59 When a plugin is first referred to -- either by a user referring to it by name 60 (e.g. `docker run --volume-driver=foo`) or a container already configured to 61 use a plugin being started -- Docker looks for the named plugin in the plugin 62 directory and activates it with a handshake. See Handshake API below. 63 64 Plugins are *not* activated automatically at Docker daemon startup. Rather, 65 they are activated only lazily, or on-demand, when they are needed. 66 67 ## API design 68 69 The Plugin API is RPC-style JSON over HTTP, much like webhooks. 70 71 Requests flow *from* the Docker daemon *to* the plugin. So the plugin needs to 72 implement an HTTP server and bind this to the UNIX socket mentioned in the 73 "plugin discovery" section. 74 75 All requests are HTTP `POST` requests. 76 77 The API is versioned via an Accept header, which currently is always set to 78 `application/vnd.docker.plugins.v1+json`. 79 80 ## Handshake API 81 82 Plugins are activated via the following "handshake" API call. 83 84 ### /Plugin.Activate 85 86 **Request:** empty body 87 88 **Response:** 89 ``` 90 { 91 "Implements": ["VolumeDriver"] 92 } 93 ``` 94 95 Responds with a list of Docker subsystems which this plugin implements. 96 After activation, the plugin will then be sent events from this subsystem. 97 98 ## Volume API 99 100 If a plugin registers itself as a `VolumeDriver` (see above) then it is 101 expected to provide writeable paths on the host filesystem for the Docker 102 daemon to provide to containers to consume. 103 104 The Docker daemon handles bind-mounting the provided paths into user 105 containers. 106 107 ### /VolumeDriver.Create 108 109 **Request**: 110 ``` 111 { 112 "Name": "volume_name" 113 } 114 ``` 115 116 Instruct the plugin that the user wants to create a volume, given a user 117 specified volume name. The plugin does not need to actually manifest the 118 volume on the filesystem yet (until Mount is called). 119 120 **Response**: 121 ``` 122 { 123 "Err": null 124 } 125 ``` 126 127 Respond with a string error if an error occurred. 128 129 ### /VolumeDriver.Remove 130 131 **Request**: 132 ``` 133 { 134 "Name": "volume_name" 135 } 136 ``` 137 138 Create a volume, given a user specified volume name. 139 140 **Response**: 141 ``` 142 { 143 "Err": null 144 } 145 ``` 146 147 Respond with a string error if an error occurred. 148 149 ### /VolumeDriver.Mount 150 151 **Request**: 152 ``` 153 { 154 "Name": "volume_name" 155 } 156 ``` 157 158 Docker requires the plugin to provide a volume, given a user specified volume 159 name. This is called once per container start. 160 161 **Response**: 162 ``` 163 { 164 "Mountpoint": "/path/to/directory/on/host", 165 "Err": null 166 } 167 ``` 168 169 Respond with the path on the host filesystem where the volume has been made 170 available, and/or a string error if an error occurred. 171 172 ### /VolumeDriver.Path 173 174 **Request**: 175 ``` 176 { 177 "Name": "volume_name" 178 } 179 ``` 180 181 Docker needs reminding of the path to the volume on the host. 182 183 **Response**: 184 ``` 185 { 186 "Mountpoint": "/path/to/directory/on/host", 187 "Err": null 188 } 189 ``` 190 191 Respond with the path on the host filesystem where the volume has been made 192 available, and/or a string error if an error occurred. 193 194 ### /VolumeDriver.Unmount 195 196 **Request**: 197 ``` 198 { 199 "Name": "volume_name" 200 } 201 ``` 202 203 Indication that Docker no longer is using the named volume. This is called once 204 per container stop. Plugin may deduce that it is safe to deprovision it at 205 this point. 206 207 **Response**: 208 ``` 209 { 210 "Err": null 211 } 212 ``` 213 214 Respond with a string error if an error occurred. 215 216 ## Plugin retries 217 218 Attempts to call a method on a plugin are retried with an exponential backoff 219 for up to 30 seconds. This may help when packaging plugins as containers, since 220 it gives plugin containers a chance to start up before failing any user 221 containers which depend on them.