【问题标题】:How to use custom Dockerfile name in Jenkins pipeline?如何在 Jenkins 管道中使用自定义 Dockerfile 名称?
【发布时间】:2019-11-20 09:53:22
【问题描述】:

我正在使用 Jenkins 来构建和发布 docker 镜像。但是,我需要在一个应用程序中使用两个 docker 文件,仅用于我的用例。

要求是我可以从一个来源的两个不同 Docker 文件构建和发布两个不同的 docker 映像,还是我需要在 docker 命令中使用自定义文件名,如参数 *-f* 以便我可以在 Jenkins 中构建两个管道?

默认情况下,Jenkins 选择带有 *Dockerfile* 的文件

ex: docker build -t dockerfile -f Dockerfile-custom-name .

【问题讨论】:

  • “来自一个来源的两个不同 Docker 文件的两个不同 Docker 映像”是什么意思?
  • 我有两个 docker 文件,比如源中的 dockerfile1 和 dockerfile2

标签: jenkins docker-compose jenkins-pipeline dockerfile


【解决方案1】:
def projectImage = docker.build("imageName:tag", "-f Dockerfile-custom-name .")

这样您甚至可以在-f 之前添加--build-arg 或任何其他参数。在第二个参数的末尾加上-f. 很重要。

这是它的文档: Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.


编辑(对 cme​​ts 的回答):

是的,我的第一个解决方案提案在 Jenkins 管道中运行。

这也应该有效: sh '''docker build -t docker1-tag:latest us.gcr.io/project-name/docker1-tag:latest -f "Dockerfile1" .'''

这个唯一的缺点,就是你没有图片在变量中,所以不能使用runwithRunpush等方法。

在您的具体情况下,我将使用名称/标签us.gcr.io/project-name/docker1-tag:latest 构建图像,因此我可以调用push 方法并将图像推送到注册表。要拥有第二个标签,单独的 sh '''docker tag us.gcr.io/project-name/docker1-tag:latest docker1-tag:latest''' 调用就足够了。

【讨论】:

  • 这是在使用 Jenkins 管道吗?
  • sh '''docker build -t docker1-tag:latest us.gcr.io/project-name/docker1-tag:latest -f "Dockerfile1" .''' 这行不通吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 2023-01-01
  • 1970-01-01
  • 2018-04-16
相关资源
最近更新 更多