jenkins - 如何在 Jenkins 管道中使用自定义 Dockerfile 名称?

标签 jenkins docker-compose jenkins-pipeline dockerfile

我正在使用 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/

相关文章:

docker - "Docker: command not found"来自 MacOS 上的 Jenkins

shell - 在 Jenkinsfile 的 shell 命令中为 Jenkins 管道使用参数

windows - Jenkins 不能很好地处理私有(private) GitHub 存储库 (Windows)

java - Jenkins 托管选项

jenkins - 使用 Jenkins 中的声明式管道步骤在并行步骤中分配变量

jenkins - 在 Jenkins 中手动提升管道结果

docker - 如何在Docker Compose中为容器和图像命名?

docker - 为什么PHPUnit中的Symfony URL生成器指向localhost?

python - docker 。如何运行 Flask 服务器主文件,在更深的文件结构中启动服务器

Jenkins Pipeline 执行一个闭包(如 "parallel"步骤)