How to use docker plugin create command to build a plugin

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to create a Docker plugin using the docker plugin create command. This process involves preparing the necessary directory structure and configuration file for your plugin, and then using the command to build the plugin from these components.

The lab will guide you through preparing the plugin data directory, which includes creating a config.json file to define the plugin's properties and interface. Following this, you will execute the docker plugin create command to build the plugin based on the prepared data. Finally, you will verify that the plugin has been successfully created and is available in your Docker environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/ls -.-> lab-555187{{"How to use docker plugin create command to build a plugin"}} docker/create -.-> lab-555187{{"How to use docker plugin create command to build a plugin"}} docker/volume -.-> lab-555187{{"How to use docker plugin create command to build a plugin"}} end

Prepare the plugin data directory

In this step, we will prepare the directory structure required for our Docker plugin. A Docker plugin needs a specific directory layout to function correctly. This directory will contain the plugin's configuration and potentially other necessary files.

First, navigate to your project directory.

cd ~/project

Now, create a directory for the plugin data. We will name it my-plugin-data.

mkdir my-plugin-data

Inside the my-plugin-data directory, we need to create a configuration file named config.json. This file will contain the plugin's configuration details.

cd my-plugin-data
nano config.json

Add the following content to the config.json file. This is a basic configuration that specifies the plugin's type and description.

{
  "Description": "My first Docker plugin",
  "Types": [
    {
      "Name": "volume",
      "Description": "A simple volume plugin"
    }
  ],
  "Interface": {
    "Types": ["docker.volumedriver/1.0"],
    "Socket": "my-plugin.sock"
  },
  "Entrypoint": ["/usr/local/bin/my-plugin"]
}

Let's break down the config.json file:

  • Description: A human-readable description of the plugin.
  • Types: Specifies the types of plugins provided. In this case, it's a volume plugin.
  • Interface: Defines the plugin's interface.
    • Types: Specifies the interface type, docker.volumedriver/1.0 for a volume driver.
    • Socket: The name of the Unix domain socket the plugin will listen on. Docker will communicate with the plugin through this socket.
  • Entrypoint: The command that Docker will execute when the plugin is enabled. This should be the path to your plugin's executable within the plugin's root filesystem.

Save the file and exit the nano editor by pressing Ctrl + X, then Y, and Enter.

Finally, navigate back to the project root directory.

cd ~/project

You have now successfully created the basic directory structure and configuration file for your Docker plugin.

Create the plugin using docker plugin create

In this step, we will use the docker plugin create command to create the plugin based on the directory we prepared in the previous step. The docker plugin create command takes a plugin name and the path to the plugin data directory as arguments.

The command syntax is docker plugin create <plugin-name> <plugin-data-directory>.

We will name our plugin my-plugin and use the my-plugin-data directory we created. Make sure you are in the ~/project directory.

cd ~/project
sudo docker plugin create my-plugin ./my-plugin-data

You should see output indicating that the plugin is being created. If successful, the command will complete without errors.

The docker plugin create command reads the config.json file from the specified directory and registers the plugin with the Docker daemon. At this point, the plugin is created but not yet enabled.

Verify the created plugin

In this step, we will verify that the plugin was successfully created and is listed by Docker. We can use the docker plugin ls command to list all available plugins.

Make sure you are in the ~/project directory.

cd ~/project
sudo docker plugin ls

The output of this command should show a list of plugins. You should see an entry for my-plugin with the status disabled. This confirms that the plugin has been registered with Docker but is not yet active.

The output will look similar to this (the ID will be different):

ID             NAME        TAG       DESCRIPTION             ENABLED
a1b2c3d4e5f6   my-plugin             My first Docker plugin   false

The ENABLED column shows false, indicating that the plugin is currently disabled. To use the plugin, you would need to enable it using docker plugin enable. However, since this is just a demonstration of creating the plugin structure, we will not enable it in this lab.

You have now successfully created and verified the existence of your Docker plugin.

Summary

In this lab, we learned how to prepare the necessary directory structure and configuration file for a Docker plugin. This involved creating a dedicated directory for the plugin data and defining the plugin's characteristics, such as its description, type, interface, and entrypoint, within a config.json file. This foundational step is crucial for defining how Docker interacts with and manages the plugin.