github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/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  
    21  Docker Engine's plugins system allows you to install, start, stop, and remove
    22  plugins using Docker Engine. This mechanism is currently only available for
    23  volume drivers, but more plugin driver types will be available in future releases.
    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  1.  Install the `sshfs` plugin.
    50  
    51      ```bash
    52      $ docker plugin install vieux/sshfs
    53  
    54      Plugin "vieux/sshfs" is requesting the following privileges:
    55      - network: [host]
    56      - capabilities: [CAP_SYS_ADMIN]
    57      Do you grant the above permissions? [y/N] y
    58  
    59      vieux/sshfs
    60      ```
    61  
    62      The plugin requests 2 privileges:
    63      - It needs access to the `host` network.
    64      - It needs the `CAP_SYS_ADMIN` capability, which allows the plugin to run
    65      the `mount` command.
    66  
    67  2.  Check that the plugin is enabled in the output of `docker plugin ls`.
    68  
    69      ```bash
    70      $ docker plugin ls
    71  
    72      NAME                TAG                 ENABLED
    73      vieux/sshfs         latest              true
    74      ```
    75  
    76  3.  Create a volume using the plugin.
    77      This example mounts the `/remote` directory on host `1.2.3.4` into a
    78      volume named `sshvolume`. This volume can now be mounted into containers.
    79  
    80      ```bash
    81      $ docker volume create \
    82        -d vieux/sshfs \
    83        --name sshvolume \
    84        -o sshcmd=user@1.2.3.4:/remote
    85  
    86      sshvolume
    87      ```
    88  4.  Verify that the volume was created successfully.
    89  
    90      ```bash
    91      $ docker volume ls
    92  
    93      DRIVER              NAME
    94      vieux/sshfs         sshvolume
    95      ```
    96  
    97  5.  Start a container that uses the volume `sshvolume`.
    98  
    99      ```bash
   100      $ docker run -v sshvolume:/data busybox ls /data
   101  
   102      <content of /remote on machine 1.2.3.4>
   103      ```
   104  
   105  To disable a plugin, use the `docker plugin disable` command. To completely
   106  remove it, use the `docker plugin remove` command. For other available
   107  commands and options, see the
   108  [command line reference](../reference/commandline/index.md).
   109  
   110  ## Developing a plugin
   111  
   112  Currently, there are no CLI commands available to help you develop a plugin.
   113  This is expected to change in a future release. The manual process for creating
   114  plugins is described in this section.
   115  
   116  ### Plugin location and files
   117  
   118  Plugins are stored in `/var/lib/docker/plugins`. The `plugins.json` file lists
   119  each plugin's configuration, and each plugin is stored in a directory with a
   120  unique identifier.
   121  
   122  ```bash
   123  # ls -la /var/lib/docker/plugins
   124  total 20
   125  drwx------  4 root root 4096 Aug  8 18:03 .
   126  drwx--x--x 12 root root 4096 Aug  8 17:53 ..
   127  drwxr-xr-x  3 root root 4096 Aug  8 17:56 cd851ce43a403
   128  -rw-------  1 root root 2107 Aug  8 18:03 plugins.json
   129  ```
   130  
   131  ### Format of plugins.json
   132  
   133  The `plugins.json` is an inventory of all installed plugins. This example shows
   134  a `plugins.json` with a single plugin installed.
   135  
   136  ```json
   137  # cat plugins.json
   138  {
   139    "cd851ce43a403": {
   140      "plugin": {
   141        "Config": {
   142          "Args": {
   143            "Value": null,
   144            "Settable": null,
   145            "Description": "",
   146            "Name": ""
   147          },
   148          "Env": null,
   149          "Devices": null,
   150          "Mounts": null,
   151          "Capabilities": [
   152            "CAP_SYS_ADMIN"
   153          ],
   154          "Description": "sshFS plugin for Docker",
   155          "Documentation": "https://docs.docker.com/engine/extend/plugins/",
   156          "Interface": {
   157            "Socket": "sshfs.sock",
   158            "Types": [
   159              "docker.volumedriver/1.0"
   160            ]
   161          },
   162          "Entrypoint": [
   163            "/go/bin/docker-volume-sshfs"
   164          ],
   165          "Workdir": "",
   166          "User": {},
   167          "Network": {
   168            "Type": "host"
   169          }
   170        },
   171        "Config": {
   172          "Devices": null,
   173          "Args": null,
   174          "Env": [],
   175          "Mounts": []
   176        },
   177        "Active": true,
   178        "Tag": "latest",
   179        "Name": "vieux/sshfs",
   180        "Id": "cd851ce43a403"
   181      }
   182    }
   183  }
   184  ```
   185  
   186  ### Contents of a plugin directory
   187  
   188  Each directory within `/var/lib/docker/plugins/` contains a `rootfs` directory
   189  and two JSON files.
   190  
   191  ```bash
   192  # ls -la /var/lib/docker/plugins/cd851ce43a403
   193  total 12
   194  drwx------ 19 root root 4096 Aug  8 17:56 rootfs
   195  -rw-r--r--  1 root root   50 Aug  8 17:56 plugin-settings.json
   196  -rw-------  1 root root  347 Aug  8 17:56 config.json
   197  ```
   198  
   199  #### The rootfs directory
   200  The `rootfs` directory represents the root filesystem of the plugin. In this
   201  example, it was created from a Dockerfile:
   202  
   203  >**Note:** The `/run/docker/plugins` directory is mandatory inside of the
   204  plugin's filesystem for docker to communicate with the plugin.
   205  
   206  ```bash
   207  $ git clone https://github.com/vieux/docker-volume-sshfs
   208  $ cd docker-volume-sshfs
   209  $ docker build -t rootfs .
   210  $ id=$(docker create rootfs true) # id was cd851ce43a403 when the image was created
   211  $ sudo mkdir -p /var/lib/docker/plugins/$id/rootfs
   212  $ sudo docker export "$id" | sudo tar -x -C /var/lib/docker/plugins/$id/rootfs
   213  $ sudo chgrp -R docker /var/lib/docker/plugins/
   214  $ docker rm -vf "$id"
   215  $ docker rmi rootfs
   216  ```
   217  
   218  #### The config.json and plugin-settings.json files
   219  
   220  The `config.json` file describes the plugin. The `plugin-settings.json` file
   221  contains runtime parameters and is only required if your plugin has runtime
   222  parameters. [See the Plugins Config reference](config.md).
   223  
   224  Consider the following `config.json` file.
   225  
   226  ```json
   227  {
   228  	"description": "sshFS plugin for Docker",
   229  	"documentation": "https://docs.docker.com/engine/extend/plugins/",
   230  	"entrypoint": ["/go/bin/docker-volume-sshfs"],
   231  	"network": {
   232  		   "type": "host"
   233  		   },
   234  	"interface" : {
   235  		   "types": ["docker.volumedriver/1.0"],
   236  		   "socket": "sshfs.sock"
   237  	},
   238  	"capabilities": ["CAP_SYS_ADMIN"]
   239  }
   240  ```
   241  
   242  This plugin is a volume driver. It requires a `host` network and the
   243  `CAP_SYS_ADMIN` capability. It depends upon the `/go/bin/docker-volume-sshfs`
   244  entrypoint and uses the `/run/docker/plugins/sshfs.sock` socket to communicate
   245  with Docker Engine.
   246  
   247  
   248  Consider the following `plugin-settings.json` file.
   249  
   250  ```json
   251  {
   252    "Devices": null,
   253    "Args": null,
   254    "Env": [],
   255    "Mounts": []
   256  }
   257  ```
   258  
   259  This plugin has no runtime parameters.
   260  
   261  Each of these JSON files is included as part of `plugins.json`, as you can see
   262  by looking back at the example above. After a plugin is installed, `config.json`
   263  is read-only, but `plugin-settings.json` is read-write, and includes all runtime
   264  configuration options for the plugin.
   265  
   266  ### Creating the plugin
   267  
   268  Follow these steps to create a plugin:
   269  
   270  1. Choose a name for the plugin. Plugin name uses the same format as images,
   271     for example: `<repo_name>/<name>`.
   272  
   273  2. Create a `rootfs` and export it to `/var/lib/docker/plugins/$id/rootfs`
   274     using `docker export`. See [The rootfs directory](#the-rootfs-directory) for
   275     an example of creating a `rootfs`.
   276  
   277  3. Create a `config.json` file in `/var/lib/docker/plugins/$id/`.
   278  
   279  4. Create a `plugin-settings.json` file if needed.
   280  
   281  5. Create or add a section to `/var/lib/docker/plugins/plugins.json`. Use
   282     `<user>/<name>` as “Name” and `$id` as “Id”.
   283  
   284  6. Restart the Docker Engine service.
   285  
   286  7. Run `docker plugin ls`.
   287      * If your plugin is enabled, you can push it to the
   288        registry.
   289      * If the plugin is not listed or is disabled, something went wrong.
   290        Check the daemon logs for errors.
   291  
   292  8. If you are not already logged in, use `docker login` to authenticate against
   293     the registry so that you can push to it.
   294  
   295  9. Run `docker plugin push <repo_name>/<name>` to push the plugin.
   296  
   297  The plugin can now be used by any user with access to your registry.