github.com/yogeshlonkar/moby@v1.13.2-0.20201203103638-c0b64beaea94/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  ## Service creation using plugins
   123  
   124  In swarm mode, it is possible to create a service that allows for attaching
   125  to networks or mounting volumes. Swarm schedules services based on plugin availability
   126  on a node. In this example, a volume plugin is installed on a swarm worker and a volume
   127  is created using the plugin. In the manager, a service is created with the relevant
   128  mount options. It can be observed that the service is scheduled to run on the worker
   129  node with the said volume plugin and volume.
   130  
   131  In the following example, node1 is the manager and node2 is the worker.
   132  
   133  1.  Prepare manager. In node 1:
   134  
   135      ```bash
   136      $ docker swarm init
   137      Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
   138      ```
   139  
   140  2. Join swarm, install plugin and create volume on worker. In node 2:
   141  
   142      ```bash
   143      $ docker swarm join \
   144      --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
   145      192.168.99.100:2377
   146      ```
   147  
   148      ```bash
   149      $ docker plugin install tiborvass/sample-volume-plugin
   150      latest: Pulling from tiborvass/sample-volume-plugin
   151      eb9c16fbdc53: Download complete
   152      Digest: sha256:00b42de88f3a3e0342e7b35fa62394b0a9ceb54d37f4c50be5d3167899994639
   153      Status: Downloaded newer image for tiborvass/sample-volume-plugin:latest
   154      Installed plugin tiborvass/sample-volume-plugin
   155      ```
   156  
   157      ```bash
   158      $ docker volume create -d tiborvass/sample-volume-plugin --name pluginVol
   159      ```
   160  
   161  3. Create a service using the plugin and volume. In node1:
   162  
   163      ```bash
   164      $ docker service create --name my-service --mount type=volume,volume-driver=tiborvass/sample-volume-plugin,source=pluginVol,destination=/tmp busybox top
   165  
   166      $ docker service ls
   167      z1sj8bb8jnfn  my-service   replicated  1/1       busybox:latest
   168      ```
   169      docker service ls shows service 1 instance of service running.
   170  
   171  4. Observe the task getting scheduled in node 2:
   172  
   173      ```bash
   174      {% raw %}
   175      $ docker ps --format '{{.ID}}\t {{.Status}} {{.Names}} {{.Command}}'
   176      83fc1e842599     Up 2 days my-service.1.9jn59qzn7nbc3m0zt1hij12xs "top"
   177      {% endraw %}
   178      ```
   179  
   180  ## Developing a plugin
   181  
   182  #### The rootfs directory
   183  The `rootfs` directory represents the root filesystem of the plugin. In this
   184  example, it was created from a Dockerfile:
   185  
   186  >**Note:** The `/run/docker/plugins` directory is mandatory inside of the
   187  plugin's filesystem for docker to communicate with the plugin.
   188  
   189  ```bash
   190  $ git clone https://github.com/vieux/docker-volume-sshfs
   191  $ cd docker-volume-sshfs
   192  $ docker build -t rootfsimage .
   193  $ id=$(docker create rootfsimage true) # id was cd851ce43a403 when the image was created
   194  $ sudo mkdir -p myplugin/rootfs
   195  $ sudo docker export "$id" | sudo tar -x -C myplugin/rootfs
   196  $ docker rm -vf "$id"
   197  $ docker rmi rootfsimage
   198  ```
   199  
   200  #### The config.json file
   201  
   202  The `config.json` file describes the plugin. See the [plugins config reference](config.md).
   203  
   204  Consider the following `config.json` file.
   205  
   206  ```json
   207  {
   208  	"description": "sshFS plugin for Docker",
   209  	"documentation": "https://docs.docker.com/engine/extend/plugins/",
   210  	"entrypoint": ["/docker-volume-sshfs"],
   211  	"network": {
   212  		   "type": "host"
   213  		   },
   214  	"interface" : {
   215  		   "types": ["docker.volumedriver/1.0"],
   216  		   "socket": "sshfs.sock"
   217  	},
   218  	"linux": {
   219  		"capabilities": ["CAP_SYS_ADMIN"]
   220  	}
   221  }
   222  ```
   223  
   224  This plugin is a volume driver. It requires a `host` network and the
   225  `CAP_SYS_ADMIN` capability. It depends upon the `/docker-volume-sshfs`
   226  entrypoint and uses the `/run/docker/plugins/sshfs.sock` socket to communicate
   227  with Docker Engine. This plugin has no runtime parameters.
   228  
   229  #### Creating the plugin
   230  
   231  A new plugin can be created by running
   232  `docker plugin create <plugin-name> ./path/to/plugin/data` where the plugin
   233  data contains a plugin configuration file `config.json` and a root filesystem
   234  in subdirectory `rootfs`.
   235  
   236  After that the plugin `<plugin-name>` will show up in `docker plugin ls`.
   237  Plugins can be pushed to remote registries with
   238  `docker plugin push <plugin-name>`.
   239  
   240  
   241  ## Debugging plugins
   242  
   243  Stdout of a plugin is redirected to dockerd logs. Such entries have a
   244  `plugin=<ID>` suffix. Here are a few examples of commands for pluginID
   245  `f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62` and their
   246  corresponding log entries in the docker daemon logs.
   247  
   248  ```bash
   249  $ docker plugin install tiborvass/sample-volume-plugins
   250  
   251  INFO[0036] Starting...       Found 0 volumes on startup  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   252  ```
   253  
   254  ```bash
   255  $ docker volume create -d tiborvass/sample-volume-plugins samplevol
   256  
   257  INFO[0193] Create Called...  Ensuring directory /data/samplevol exists on host...  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   258  INFO[0193] open /var/lib/docker/plugin-data/local-persist.json: no such file or directory  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   259  INFO[0193]                   Created volume samplevol with mountpoint /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   260  INFO[0193] Path Called...    Returned path /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   261  ```
   262  
   263  ```bash
   264  $ docker run -v samplevol:/tmp busybox sh
   265  
   266  INFO[0421] Get Called...     Found samplevol                plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   267  INFO[0421] Mount Called...   Mounted samplevol              plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   268  INFO[0421] Path Called...    Returned path /data/samplevol  plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   269  INFO[0421] Unmount Called... Unmounted samplevol            plugin=f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62
   270  ```
   271  
   272  #### Using docker-runc to obtain logfiles and shell into the plugin.
   273  
   274  `docker-runc`, the default docker container runtime can be used for debugging
   275  plugins. This is specifically useful to collect plugin logs if they are
   276  redirected to a file.
   277  
   278  ```bash
   279  $ docker-runc list
   280  ID                                                                 PID         STATUS      BUNDLE                                                                                       CREATED
   281  f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62   2679        running     /run/docker/libcontainerd/f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62	2017-02-06T21:53:03.031537592Z
   282  r
   283  ```
   284  
   285  ```bash
   286  $ docker-runc exec f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62 cat /var/log/plugin.log
   287  ```
   288  
   289  If the plugin has a built-in shell, then exec into the plugin can be done as
   290  follows:
   291  ```bash
   292  $ docker-runc exec -t f52a3df433b9aceee436eaada0752f5797aab1de47e5485f1690a073b860ff62 sh
   293  ```
   294  
   295  #### Using curl to debug plugin socket issues.
   296  
   297  To verify if the plugin API socket that the docker daemon communicates with
   298  is responsive, use curl. In this example, we will make API calls from the
   299  docker host to volume and network plugins using curl 7.47.0 to ensure that
   300  the plugin is listening on the said socket. For a well functioning plugin,
   301  these basic requests should work. Note that plugin sockets are available on the host under `/var/run/docker/plugins/<pluginID>`
   302  
   303  
   304  ```bash
   305  curl -H "Content-Type: application/json" -XPOST -d '{}' --unix-socket /var/run/docker/plugins/e8a37ba56fc879c991f7d7921901723c64df6b42b87e6a0b055771ecf8477a6d/plugin.sock http:/VolumeDriver.List
   306  
   307  {"Mountpoint":"","Err":"","Volumes":[{"Name":"myvol1","Mountpoint":"/data/myvol1"},{"Name":"myvol2","Mountpoint":"/data/myvol2"}],"Volume":null}
   308  ```
   309  
   310  ```bash
   311  curl -H "Content-Type: application/json" -XPOST -d '{}' --unix-socket /var/run/docker/plugins/45e00a7ce6185d6e365904c8bcf62eb724b1fe307e0d4e7ecc9f6c1eb7bcdb70/plugin.sock http:/NetworkDriver.GetCapabilities
   312  
   313  {"Scope":"local"}
   314  ```
   315  When using curl 7.5 and above, the URL should be of the form
   316  `http://hostname/APICall`, where `hostname` is the valid hostname where the
   317  plugin is installed and `APICall` is the call to the plugin API.
   318  
   319  For example, `http://localhost/VolumeDriver.List`