首页 文章

强制Jenkins管道作业从Dockerhub中提取新的私有映像

提问于
浏览
3

我想使用Jenkins Docker管道插件在Docker Hub上提取具有特定标记的特定私有映像的新版本 . Docker shell命令看起来像:

docker login -u user -p password
docker pull user/foo:bar

像这样的东西似乎应该工作:

node () {
    image = docker.image('user/foo:bar')
    image.pull()
    image.inside {
        // Commands to run in the container

但是没有办法执行登录步骤,所以我总是得到错误:

Error response from daemon: pull access denied for user/foo, repository does not exist or may require 'docker login'.

我已经浏览了documentationcode,但是文档没有显示如何登录以获取私有图像 . 我可以手动编写脚本,但使用Docker管道插件的全部目的是避免直接编写Docker命令脚本 .

1 回答

  • 6

    我相信你需要的是 withRegistry 功能 . 它可以如下使用

    docker.withRegistry('<registry-url>', '<credential-id>') {
        image = docker.image('user/foo:bar')
        image.pull()
    }
    

    withRegistry 块中的所有内容都将使用该注册表和身份验证来提取图像 .

    这种情况下 <registry-url> 是Dockerhub注册表的URL . 我相信Dockerhub注册表网址是 https://registry.hub.docker.com

    <credential-id> 是存储在Jenkins中的Dockerhub凭据的ID .

    要添加这些凭据,请从Jenkins索引页面导航到 Credentials -> System -> Global credentials -> Add Credentials .

    进入此页面后,您需要选择 Kind 作为 Username with password . 范围应为 Global . usernamepassword 字段是Dockerhub的用户名和密码 .

    ID 字段是您希望成为 <credential-id> 的任何文本 . 例如,如果您使 ID 字段 docker-hub-credentials 需要是 withRegistry 的第二个参数 .

    以下是添加凭据的页面的示例 .
    enter image description here

相关问题