docker插件开发示例

官方示例文档

官方以开发一个sshfs的volume plugin为例。

  1. $ git clone https://github.com/vieux/docker-volume-sshfs
  2. $ cd docker-volume-sshfs
  3. $ go get github.com/docker/go-plugins-helpers/volume
  4. $ go build -o docker-volume-sshfs main.go
  5. $ docker build -t rootfsimage .
  6. $ id=$(docker create rootfsimage true) # id was cd851ce43a403 when the image was created
  7. $ sudo mkdir -p myplugin/rootfs
  8. $ sudo docker export "$id" | sudo tar -x -C myplugin/rootfs
  9. $ docker rm -vf "$id"
  10. $ docker rmi rootfsimage

我们可以看到sshfs的Dockerfile是这样的:

  1. FROM alpine
  2. RUN apk update && apk add sshfs
  3. RUN mkdir -p /run/docker/plugins /mnt/state /mnt/volumes
  4. COPY docker-volume-sshfs docker-volume-sshfs
  5. CMD ["docker-volume-sshfs"]

实际上是讲编译好的可执行文件复制到alpine linux容器中运行。

编译rootfsimage镜像的过程。

  1. docker build -t rootfsimage .
  2. Sending build context to Docker daemon 11.71 MB
  3. Step 1/5 : FROM alpine
  4. ---> 4a415e366388
  5. Step 2/5 : RUN apk update && apk add sshfs
  6. ---> Running in 1551ecc1c847
  7. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
  8. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
  9. v3.5.2-2-ge626ce8c3c [http://dl-cdn.alpinelinux.org/alpine/v3.5/main]
  10. v3.5.1-71-gc7bb9a04f0 [http://dl-cdn.alpinelinux.org/alpine/v3.5/community]
  11. OK: 7959 distinct packages available
  12. (1/10) Installing openssh-client (7.4_p1-r0)
  13. (2/10) Installing fuse (2.9.7-r0)
  14. (3/10) Installing libffi (3.2.1-r2)
  15. (4/10) Installing libintl (0.19.8.1-r0)
  16. (5/10) Installing libuuid (2.28.2-r1)
  17. (6/10) Installing libblkid (2.28.2-r1)
  18. (7/10) Installing libmount (2.28.2-r1)
  19. (8/10) Installing pcre (8.39-r0)
  20. (9/10) Installing glib (2.50.2-r0)
  21. (10/10) Installing sshfs (2.8-r0)
  22. Executing busybox-1.25.1-r0.trigger
  23. Executing glib-2.50.2-r0.trigger
  24. OK: 11 MiB in 21 packages
  25. ---> 1a73c501f431
  26. Removing intermediate container 1551ecc1c847
  27. Step 3/5 : RUN mkdir -p /run/docker/plugins /mnt/state /mnt/volumes
  28. ---> Running in 032af3b2595a
  29. ---> 30c7e8463e96
  30. Removing intermediate container 032af3b2595a
  31. Step 4/5 : COPY docker-volume-sshfs docker-volume-sshfs
  32. ---> a924c6fcc1e4
  33. Removing intermediate container ffc5e3c97707
  34. Step 5/5 : CMD docker-volume-sshfs
  35. ---> Running in 0dc938fe4f4e
  36. ---> 0fd2e3d94860
  37. Removing intermediate container 0dc938fe4f4e
  38. Successfully built 0fd2e3d94860

编写config.json文档

  1. {
  2. "description": "sshFS plugin for Docker",
  3. "documentation": "https://docs.docker.com/engine/extend/plugins/",
  4. "entrypoint": ["/go/bin/docker-volume-sshfs"],
  5. "network": {
  6. "type": "host"
  7. },
  8. "interface" : {
  9. "types": ["docker.volumedriver/1.0"],
  10. "socket": "sshfs.sock"
  11. },
  12. "linux": {
  13. "capabilities": ["CAP_SYS_ADMIN"]
  14. }
  15. }

该插件使用host网络类型,使用/run/docker/plugins/sshfs.sock接口与docker engine通信。

创建plugin

使用docker plugin create <plugin_name> /path/to/plugin/data/命令创建插件。

具体到sshfs插件,在myplugin目录下使用如下命令创建插件:

  1. docker plugin create jimmysong/sshfs:latest .

现在就可以看到刚创建的插件了

  1. docker plugin ls
  2. ID NAME DESCRIPTION ENABLED
  3. 8aa1f6098fca vieux/sshfs:latest sshFS plugin for Docker true

push plugin

先登录你的docker hub账户,然后使用docker plugin push jimmysong/sshfs:latest即可以推送docker plugin到docker hub中。

目前推送到harbor镜像仓库有问题,报错信息:

  1. c08c951b53b7: Preparing
  2. denied: requested access to the resource is denied

已给harbor提issue-1532