简体   繁体   English

如何在 jenkinsfile 中为 docker.build 设置自定义上下文

[英]How to set custom context for docker.build in jenkinsfile

I'm trying to adapt a docker build for jenkins.我正在尝试为 jenkins 调整 docker 版本。 I'm following our docker-compose file and I'm creating a Jenkinsfile that is creating each container and linking them together.我正在关注我们的 docker-compose 文件,我正在创建一个 Jenkinsfile 来创建每个容器并将它们链接在一起。 The problem I'm running into is that the docker-compose files declare a context that is not where the Dockerfile is.我遇到的问题是 docker-compose 文件声明了一个不是 Dockerfile 所在的上下文。 As far as I understand, jenkins will set the context to where the Dockerfile is, which puts files to be copied in a different relative location depending on whether the jenkinsfile or docker-compose file is building.据我了解,jenkins 会将上下文设置为 Dockerfile 所在的位置,这将根据 jenkinsfile 或 docker-compose 文件是否正在构建将文件复制到不同的相对位置。

The folder structure is:文件夹结构为:

workspace
    |-docker
        |-db
           |-Dockerfile
           |-entrypoint.sh

This is how the Dockerfile declares the COPY instruction for the file in question这就是 Dockerfile 如何为相关文件声明 COPY 指令

COPY docker/db/entrypoint.sh /

This is how my jenkinsfile builds the file.这就是我的 jenkinsfile 构建文件的方式。 Which to my knowledge puts the context at that directory据我所知,哪个将上下文放在该目录中

docker.build("db", "${WORKSPACE}/docker/db")

the docker-compose file declares it like: docker-compose 文件声明如下:

db:
build:
  context: .
  dockerfile: docker/db/Dockerfile

which puts the context at the root of the project.它将上下文放在项目的根目录下。

Is there any way to tell a jenkinsfile to use the same context as the docker-compose file so that the Dockerfile's COPY instruction can remain unchanged and valid for both Jenkins and docker-compose?有没有办法告诉 jenkinsfile 使用与 docker-compose 文件相同的上下文,以便 Dockerfile 的 COPY 指令可以保持不变并对 Jenkins 和 docker-compose 都有效? If that's not possible, does anyone know of any alternative solutions?如果这是不可能的,有没有人知道任何替代解决方案?

It turns out that I was pulling a previous version (1.6) of the docker-pipeline-plugin.事实证明,我正在拉取 docker-pipeline-plugin 的先前版本 (1.6)。 The function in question has been since updated (1.7) to allow the second parameter to designate a Dockerfile location outside the context.有问题的函数已经更新 (1.7) 以允许第二个参数指定上下文之外的 Dockerfile 位置。

The updated statement in my Jenkinsfile is:我的 Jenkinsfile 中更新的语句是:

return docker.build("db", "-f docker/db/Dockerfile .")

And this allows my container to build without modifying the expected context of the developer's docker-compose or Dockerfiles.这允许我的容器在不修改开发人员的 docker-compose 或 Dockerfiles 的预期上下文的情况下构建。

The build() method builds the Dockerfile in the current directory by default. build() 方法默认在当前目录中构建 Dockerfile。 This can be overridden by providing a directory path containing a Dockerfile as the second argument of the build() method, for example:这可以通过提供包含 Dockerfile 作为 build() 方法的第二个参数的目录路径来覆盖,例如:

node {
    checkout scm
    def testImage = docker.build("test-image", "./dockerfiles/test") 

    testImage.inside {
        sh 'make test'
    }
}

To specify the path of the dockerfile the dir{} block worked for me要指定 dockerfile 的路径,dir{} 块对我有用

stage('docker build') {
            steps {
                dir('/path/to/docker/file/'){
                    script{
                        dockerImage = docker.build("imageName")
                    }
                }
            }
        }

The best way is to use the dir block because you'll need to perform actions like COPY, where you must be in the location最好的方法是使用 dir 块,因为您需要执行 COPY 之类的操作,您必须在该位置

So Use this Code所以使用这个代码

stage('Build') {
        steps {
            dir('/path/to/docker/file/'){ // put the path of Dockerfile
                script{
                    my_images = docker.build("Your_Image")
                }
            }
        }
    }

只需在 docker docker-compose上 Omnit context项,它就会默认为 Dockerfile 的位置。

以下语法应该有效: docker.build('ContainerTag''-f DockerFilePath Context' 示例:docker.build('quay.io/AppContianer:1.0.0' '-f dockerfileDir/Dockerfile.test dockerfileDir')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker.build() in Jenkins pipeline with two tags - docker.build() in Jenkins pipeline with two tags 将 docker buildx 功能与 Jenkins docker.build 一起使用 - Use docker buildx feature with Jenkins docker.build 需要哪个插件才能让 docker.build 在 Jenkins 中工作? - Which plugin is required to have docker.build work in Jenkins? Jenkinsfile - 如何将构建参数传递给 docker - Jenkinsfile - how to pass build argument to docker 如何从 jenkinsfile 构建多个 docker 容器? - How to build multiple docker containers from jenkinsfile? Docker SDK 用于 Python:如何使用自定义 Z3254677A7917C6C01FZC2 AND 自定义上下文构建图像 - Docker SDK for Python: how to build an image with custom Dockerfile AND custom context Jenkins 管道 docker.build() 给出错误““docker build”需要 1 个参数” - Jenkins Pipeline docker.build() gives error '“docker build” requires exactly 1 argument(s)' 尝试在jenkins管道中使用docker.build(),同时还定义了一个特定的docker文件供使用 - Attempting to use docker.build() in aj Jenkins pipeline while also defining a specific docker file for use 无法使用 Jenkinsfile 构建 docker 映像 - Unable to build docker image with Jenkinsfile 如何配置Jenkinsfile来构建docker映像并将其推送到私有注册表 - How to configure a Jenkinsfile to build docker image and push it to a private registry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM