github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/docs/extend/index.md (about)

     1  ---
     2  description: Develop and use a plugin with the managed plugin system
     3  keywords: "API, Usage, plugins, documentation, developer"
     4  title: Managed plugin system
     5  ---
     6  
     7  <!-- This file is maintained within the docker/docker Github
     8       repository at https://github.com/docker/docker/. Make all
     9       pull requests against that repo. If you see this file in
    10       another repository, consider it read-only there, as it will
    11       periodically be overwritten by the definitive file. Pull
    12       requests which include edits to this file in other repositories
    13       will be rejected.
    14  -->
    15  
    16  # Docker Engine managed plugin system
    17  
    18  * [Installing and using a plugin](index.md#installing-and-using-a-plugin)
    19  * [Developing a plugin](index.md#developing-a-plugin)
    20  * [Debugging plugins](index.md#debugging-plugins)
    21  
    22  Docker Engine's plugin system allows you to install, start, stop, and remove
    23  plugins using Docker Engine.
    24  
    25  For information about the legacy plugin system available in Docker Engine 1.12
    26  and earlier, see [Understand legacy Docker Engine plugins](legacy_plugins.md).
    27  
    28  > **Note**: Docker Engine managed plugins are currently not supported
    29  on Windows daemons.
    30  
    31  ## Installing and using a plugin
    32  
    33  Plugins are distributed as Docker images and can be hosted on Docker Hub or on
    34  a private registry.
    35  
    36  To install a plugin, use the `docker plugin install` command, which pulls the
    37  plugin from Docker Hub or your private registry, prompts you to grant
    38  permissions or capabilities if necessary, and enables the plugin.
    39  
    40  To check the status of installed plugins, use the `docker plugin ls` command.
    41  Plugins that start successfully are listed as enabled in the output.
    42  
    43  After a plugin is installed, you can use it as an option for another Docker
    44  operation, such as creating a volume.
    45  
    46  In the following example, you install the `sshfs` plugin, verify that it is
    47  enabled, and use it to create a volume.
    48  
    49  > **Note**: This example is intended for instructional purposes only. Once the volume is created, your SSH password to the remote host will be exposed as plaintext when inspecting the volume. You should delete the volume as soon as you are done with the example.
    50  
    51  1.  Install the `sshfs` plugin.
    52  
    53      ```bash
    54      $ docker plugin install vieux/sshfs
    55  
    56      Plugin "vieux/sshfs" is requesting the following privileges:
    57      - network: [host]
    58      - capabilities: [CAP_SYS_ADMIN]
    59      Do you grant the above permissions? [y/N] y
    60  
    61      vieux/sshfs
    62      ```
    63  
    64      The plugin requests 2 privileges:
    65      
    66      - It needs access to the `host` network.
    67      - It needs the `CAP_SYS_ADMIN` capability, which allows the plugin to run
    68      the `mount` command.
    69  
    70  2.  Check that the plugin is enabled in the output of `docker plugin ls`.
    71  
    72      ```bash
    73      $ docker plugin ls
    74  
    75      ID                    NAME                  TAG                 DESCRIPTION                   ENABLED
    76      69553ca1d789          vieux/sshfs           latest              the `sshfs` plugin            true
    77      ```
    78  
    79  3.  Create a volume using the plugin.
    80      This example mounts the `/remote` directory on host `1.2.3.4` into a
    81      volume named `sshvolume`.   
    82  
    83      This volume can now be mounted into containers.
    84  
    85      ```bash
    86      $ docker volume create \
    87        -d vieux/sshfs \
    88        --name sshvolume \
    89        -o sshcmd=user@1.2.3.4:/remote \
    90        -o password=$(cat file_containing_password_for_remote_host)
    91  
    92      sshvolume
    93      ```
    94  4.  Verify that the volume was created successfully.
    95  
    96      ```bash
    97      $ docker volume ls
    98  
    99      DRIVER              NAME
   100      vieux/sshfs         sshvolume
   101      ```
   102  
   103  5.  Start a container that uses the volume `sshvolume`.
   104  
   105      ```bash
   106      $ docker run --rm -v sshvolume:/data busybox ls /data
   107  
   108      <content of /remote on machine 1.2.3.4>
   109      ```
   110  
   111  6.  Remove the volume `sshvolume`
   112      ```bash
   113      docker volume rm sshvolume
   114  
   115      sshvolume
   116      ```
   117  To disable a plugin, use the `docker plugin disable` command. To completely
   118  remove it, use the `docker plugin remove` command. For other available
   119  commands and options, see the
   120  [command line reference](../reference/commandline/index.md).
   121  
   122  
   123  ## Developing a plugin
   124  
   125  #### The rootfs directory
   126  The `rootfs` directory represents the root filesystem of the plugin. In this
   127  example, it was created from a Dockerfile:
   128  
   129  >**Note:** The `/run/docker/plugins` directory is mandatory inside of the
   130  plugin's filesystem for docker to communicate with the plugin.
   131  
   132  ```bash
   133  $ git clone https://github.com/vieux/docker-volume-sshfs
   134  $ cd docker-volume-sshfs
   135  $ docker build -t rootfsimage .
   136  $ id=$(docker create rootfsimage true) # id was cd851ce43a403 when the image was created
   137  $ sudo mkdir -p myplugin/rootfs
   138  $ sudo docker export "$id" | sudo tar -x -C myplugin/rootfs
   139  $ docker rm -vf "$id"
   140  $ docker rmi rootfsimage
   141  ```
   142  
   143  #### The config.json file
   144  
   145  The `config.json` file describes the plugin. See the [plugins config reference](config.md).
   146  
   147  Consider the following `config.json` file.
   148  
   149  ```json
   150  {
   151  	"description": "sshFS plugin for Docker",
   152  	"documentation": "https://docs.docker.com/engine/extend/plugins/",
   153  	"entrypoint": ["/docker-volume-sshfs"],
   154  	"network": {
   155  		   "type": "host"
   156  		   },
   157  	"interface" : {
   158  		   "types": ["docker.volumedriver/1.0"],
   159  		   "socket": "sshfs.sock"
   160  	},
   161  	"linux": {
   162  		"capabilities": ["CAP_SYS_ADMIN"]
   163  	}
   164  }
   165  ```
   166  
   167  This plugin is a volume driver. It requires a `host` network and the
   168  `CAP_SYS_ADMIN` capability. It depends upon the `/docker-volume-sshfs`
   169  entrypoint and uses the `/run/docker/plugins/sshfs.sock` socket to communicate
   170  with Docker Engine. This plugin has no runtime parameters.
   171  
   172  #### Creating the plugin
   173  
   174  A new plugin can be created by running
   175  `docker plugin create <plugin-name> ./path/to/plugin/data` where the plugin
   176  data contains a plugin configuration file `config.json` and a root filesystem
   177  in subdirectory `rootfs`.
   178  
   179  After that the plugin `<plugin-name>` will show up in `docker plugin ls`.
   180  Plugins can be pushed to remote registries with
   181  `docker plugin push <plugin-name>`.
   182  
   183  
   184  ## Debugging plugins
   185  
   186  Stdout of a plugin is redirected to dockerd logs. Such entries have a
   187  `plugin=<ID>` suffix. Here are a few examples of commands for pluginID
   188  `f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62` and their
   189  corresponding log entries in the docker daemon logs.
   190  
   191  ```bash
   192  $ docker plugin install tiborvass/sample-volume-plugins
   193  
   194  INFO[0036] Starting...       Found 0 volumes on startup  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   195  ```
   196  
   197  ```bash
   198  $ docker volume create -d tiborvass/sample-volume-plugins samplevol
   199  
   200  INFO[0193] Create Called...  Ensuring directory /data/samplevol exists on host...  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   201  INFO[0193] open /var/lib/docker/plugin-data/local-persist.json: no such file or directory  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   202  INFO[0193]                   Created volume samplevol with mountpoint /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   203  INFO[0193] Path Called...    Returned path /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   204  ```
   205  
   206  ```bash
   207  $ docker run -v samplevol:/tmp busybox sh
   208  
   209  INFO[0421] Get Called...     Found samplevol                plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   210  INFO[0421] Mount Called...   Mounted samplevol              plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   211  INFO[0421] Path Called...    Returned path /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   212  INFO[0421] Unmount Called... Unmounted samplevol            plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   213  ```
   214  
   215  #### Using docker-runc to obtain logfiles and shell into the plugin.
   216  
   217  `docker-runc`, the default docker container runtime can be used for debugging
   218  plugins. This is specifically useful to collect plugin logs if they are
   219  redirected to a file.
   220  
   221  ```bash
   222  $ docker-runc list
   223  ID                                                                 PID         STATUS      BUNDLE                                                                                       CREATED
   224  f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62   2679        running     /run/docker/libcontainerd/f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62	2017-02-06T21:53:03.031537592Z
   225  r
   226  ```
   227  
   228  ```bash
   229  $ docker-runc exec f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62 cat /var/log/plugin.log
   230  ```
   231  
   232  If the plugin has a built-in shell, then exec into the plugin can be done as
   233  follows:
   234  ```bash
   235  $ docker-runc exec -t f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62 sh
   236  ```
   237  
   238  #### Using curl to debug plugin socket issues.
   239  
   240  To verify if the plugin API socket that the docker daemon communicates with
   241  is responsive, use curl. In this example, we will make API calls from the
   242  docker host to volume and network plugins using curl 7.47.0 to ensure that
   243  the plugin is listening on the said socket. For a well functioning plugin,
   244  these basic requests should work. Note that plugin sockets are available on the host under `/var/run/docker/plugins/<pluginID>`
   245  
   246  
   247  ```bash
   248  curl -H "Content-Type: application/json" -XPOST -d '{}' --unix-socket /var/run/docker/plugins/e8a37ba56fc879c991f7d7921901723c64df6b42b87e6a0b055771ecf8477a6d/plugin.sock http:/VolumeDriver.List
   249  
   250  {"Mountpoint":"","Err":"","Volumes":[{"Name":"myvol1","Mountpoint":"/data/myvol1"},{"Name":"myvol2","Mountpoint":"/data/myvol2"}],"Volume":null}
   251  ```
   252  
   253  ```bash
   254  curl -H "Content-Type: application/json" -XPOST -d '{}' --unix-socket /var/run/docker/plugins/45e00a7ce6185d6e365904c8bcf62eb724b1fe307e0d4e7ecc9f6c1eb7bcdb70/plugin.sock http:/NetworkDriver.GetCapabilities
   255  
   256  {"Scope":"local"}
   257  ```
   258  When using curl 7.5 and above, the URL should be of the form
   259  `http://hostname/APICall`, where `hostname` is the valid hostname where the
   260  plugin is installed and `APICall` is the call to the plugin API.
   261  
   262  For example, `http://localhost/VolumeDriver.List`