Drone 的插件 - Docker 插件

Drone 插件市场
Drone 插件文档
原文地址 - Docker 插件的手册

Docker 插件可以用于构建镜像及发布镜像到 Docker registry。下面的 pipeline 配置,就使用了 Docker 插件来构建和发布镜像:

pipeline:
  docker:
    image: plugins/docker
    username: kevinbacon
    password: pa55word
    repo: foo/bar
    tags: latest

使用多个 tag 的示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
-   tags: latest
+   tags:
+     - latest
+     - 1.0.1
+     - 1.0

使用 .tag 文件(用逗号分隔的 tag 列表)的示例配置:

pipeline:
  build:
    image: golang:1.10.0-alpine
    commands:
      - [...]
+     - echo -n "5.2.6,5.2.4" > .tags
  docker:
    image: plugins/docker
    repo: foo/bar
-   tags: latest

使用 build_args 参数的示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
+   build_args:
+     - HTTP_PROXY=http://yourproxy.com

使用备用 Dockerfile 的示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
-   dockerfile: Dockerfile
+   dockerfile: path/to/Dockerfile

使用自定义 registry 的示例配置:

pipeline:
  docker:
    image: plugins/docker
-   repo: foo/bar
+   repo: index.company.com/foo/bar
+   registry: index.company.com

使用行内凭据(inline credentials)的示例配置:

pipeline:
  docker:
    image: plugins/docker
+   username: kevinbacon
+   password: pa55word
    repo: foo/bar

使用 secrets 中的凭据(通过 Drone 的 web 页面添加)的示例配置:

pipeline:
  docker:
    image: plugins/docker
-   username: kevinbacon
-   password: pa55word
    repo: foo/bar
+   secrets: [ docker_username, docker_password ]

自动打标签 Autotag

Docker 插件可以被配置成自动为你的镜像创建 tag。当开启这个特性且事件类型是 tag 时,插件会自动使用标准的 major、minor、release 惯例为镜像创建 tag。例如:

  • 1.0.0 生成 docker tag 1, 1.0, 1.0.0
  • 1.0.0-rc.1 生成 docker tag 1.0.0-rc.1

当事件类型是 push 并且目标分支是你的默认分支(例如 master)时,插件会自动为镜像添加 latest 这个 tag。所有其他的事件类型和分支都会被忽略。

示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
+   auto_tag: true
    secrets: [ docker_username, docker_password ]

使用了 tag 后缀的示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
+   auto_tag: true
+   auto_tag_suffix: linux-amd64
    secrets: [ docker_username, docker_password ]

请注意,自动标记 auto_tag 是故意简单和无法定制的,目前不接受 pull 请求以进一步定制逻辑。

多阶段构建(Multi-stage builds)

Docker 插件允许在 官方文档 中描述的 Dockerfile 中定义的特定阶段停止构建。如果未定义 target 属性,则 Docker 插件不会在任何阶段停止,此时只会构建完整的 Docker 镜像。

The Docker plugin allow to stop build at a specific stage defined in Dockerfile as described in the official docs. If the target attribute is not defined, the Docker plugin will not stop at any stage and build the full docker image.

Dockerfile 示例:

FROM golang as builder
WORKSPACE /go/src/github.com/foo/bar
RUN CGO_ENABLED=0 GOOS=linux go build -o demo main.go

FROM scratch as production
COPY --from=builder /go/src/github.com/foo/bar/demo .
CMD ["./demo"]

FROM alpine as debug
COPY --from=builder /go/src/github.com/foo/bar/demo .
CMD ["./demo"]

允许构建生产环境 Docker 镜像的示例配置:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
+   target: production
    secrets: [ docker_username, docker_password ]

下面这个会构建 debug 环境 Docker 镜像:

pipeline:
  docker:
    image: plugins/docker
    repo: foo/bar
+   target: debug
    secrets: [ docker_username, docker_password ]

Secret 手册

  • docker_username:使用此用户名进行身份验证
  • docker_password:使用此密码进行身份验证

Parameter 手册

  • registry:向这个 registry 进行验证
  • username:使用此用户名进行身份验证
  • password:使用此密码进行身份验证
  • repo:用于存储镜像的仓库名
  • tags:用于镜像的仓库的 tag
  • dockerfile:要使用的 dockerfile,默认是 Dockerfile
  • auth:registry 的身份验证 token
  • context:要使用的上下文路径,默认为 git 仓库的根目录
  • target:要使用的构建目标,必须在 dockerfile 中定义
  • force_tag=false:替换现有的匹配到的镜像的 tag
  • insecure=false:启用对此 registry 的不安全通信
  • mirror:使用 registry 镜像,而不是直接从 Docker 默认的 Hub 中获取镜像
  • bip=false:用于传递 bridge IP
  • custom_dns:为容器设置自定义 DNS 服务器
  • storage_driver:支持 aufs,overlay 或 vfs 驱动程序
  • build_args:自定义参数传递给 docker build
  • auto_tag=false:根据 git 分支和 git 标签自动生成标签名称
  • auto_tag_suffix:用这个后缀生成标签名称
  • debug, launch_debug:以详细调试模式启动 docker 守护进程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值