我正在使用 Jenkins 构建和发布 docker 镜像。但是,我需要在一个应用程序中使用两个 docker 文件来满足我的用例。
要求是我可以从一个来源的两个不同的 Docker 文件构建并发布两个不同的 docker 镜像,或者我需要在 docker 命令中使用自定义文件名,例如参数 *-f*
,以便我可以在 Jenkins 中构建两个管道吗?
默认情况下,Jenkins 选择带有 *Dockerfile*
的文件
例如:docker build -t dockerfile -f Dockerfile-custom-name 。
最佳答案
def projectImage = docker.build("imageName:tag", "-f Dockerfile-custom-name .")
这样您甚至可以添加 --build-arg
或 -f
之前的任何其他参数。拥有 -f
非常重要和.
在第二个参数的末尾。
这是它的文档: Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.
编辑(回复评论):
是的,我的第一个解决方案提案在 Jenkins 管道中运行。
这也应该有效:
sh '''docker build -t docker1-tag:latest us.gcr.io/project-name/docker1-tag:latest -f "Dockerfile1" .'''
这个的唯一缺点是变量中没有图像,因此不能使用 run
, withRun
, push
以及其他方法。
在您的具体情况下,我将使用名称/标签 us.gcr.io/project-name/docker1-tag:latest
构建图像。 ,这样我就可以调用 push
方法并且图像将被推送到注册表。要拥有第二个标签,需要单独的 sh '''docker tag us.gcr.io/project-name/docker1-tag:latest docker1-tag:latest'''
打电话就足够了。
关于jenkins - 如何在 Jenkins 管道中使用自定义 Dockerfile 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58951383/