简体   繁体   English

Docker 容器在项目内部重建

[英]Docker container rebuild inside of a project

I have a project with the following structure:我有一个具有以下结构的项目:

  • docker/Dockerfile码头工人/码头工人文件

  • linux/*.xz (kernel sources) linux/*.xz(内核源代码)

  • *.sh *.sh

  • Jenkinsfile詹金斯文件

Basically this pipeline is building a linux kernel inside of a docker container.基本上,这个管道是在 docker 容器内构建一个 linux 内核。

We are using bitbucket in combination with jenkins and artifactory for CI/CD.我们将 bitbucket 与 jenkins 和 artifactory 结合用于 CI/CD。

Currently the building of the docker image is manual: a developr builds it locally and pushes it to artifactory.目前 docker 镜像的构建是手动的:开发人员在本地构建它并将其推送到工件。

The pipeline is only pulling prebuilt docker images for execution.管道仅提取预构建的 docker 映像以供执行。

As this manual interaction is annoying this should be automated.由于这种手动交互很烦人,因此应该自动化。

Typical scenario could look like this: a developer changes the Dockerfile and some other ressource.典型场景可能如下所示:开发人员更改 Dockerfile 和其他一些资源。

This requires us to first rebuild the container, push it to artifactory and afterwards start the build.这要求我们首先重建容器,将其推送到工件,然后开始构建。

Is there a straightforward way to do this using the mentioned technologies?有没有使用上述技术的直接方法来做到这一点?

My approach would be (which is mainly a own implementation) to tag docker images based on their git revision.我的方法是(主要是自己的实现)根据 git 版本标记 docker 图像。

When the above mentioned pipeline starts it queries the git-rev of the Dockerfile of the last change and tries to pull the当上述管道启动时,它会查询上次更改的 Dockerfile 的 git-rev 并尝试拉取

container.容器。 If this is possible - fine.如果这是可能的 - 很好。 Otherwise run a 'docker build' and push the new container.否则运行“docker build”并推送新容器。 Afterwards run the kernel build inside.然后在里面运行内核构建。

Is this a proper way to do that?这是一个正确的方法吗?

Possible options are below.可能的选项如下。

Option 1选项1

In order to determine whether you have to build a new image, you can rely on Jenkins Changeset .为了确定是否必须构建新映像,您可以依赖 Jenkins Changeset You can check whether the docker/Dockerfile file has changed from the last build, if so you can execute the build step or just skip it.您可以检查docker/Dockerfile文件是否与上次构建相比发生了变化,如果是,您可以执行构建步骤或跳过它。

Also, when tagging the image you can create two image tags.此外,在标记图像时,您可以创建两个图像标记。 One with the revision(Or any other unique identifier) and also replace the latest tag with the latest image built.一个带有修订版(或任何其他唯一标识符),并将latest标签替换为最新构建的图像。 This will allow you to have a simple logic in consecutive steps of the pipeline since we know that the latest build image is in the latest tag.这将允许您在管道的连续步骤中拥有一个简单的逻辑,因为我们知道最新的构建映像位于最新的标签中。 Sample pipeline below.下面的示例管道。

pipeline {
    agent any

    stages {
        stage('BuildImage') {
            when { changeset "**/Dockerfile" }
            steps {
                script {
                 def image = docker.build("my-image:REVISION", "docker/Dockerfile")
                 image.push() // Pushes the tagged image with revision
                 image.push('latest') // Change the tag and push
                }
            }
        }
        
        stage('Other Build') {
            steps {
                 docker.image('my-image:latest').inside {
                    // Do other stuff within the Docker image
                }
            }
        }
        
        stage('Other Build option 2') {
            agent {
                docker { image 'node:16.13.1-alpine' }
            }
            steps {
                // Do other stuff within the Docker image
            }
        }
    }
}

Option 2选项 2

Another option would be to always start a new Jenkins Agent/Container with your Docker file or always build the image and then execute your build commands with the container.另一种选择是始终使用 Docker 文件启动新的 Jenkins 代理/容器,或者始终构建映像,然后使用容器执行构建命令。 This may be efficient if you do frequent changes to the Dockerfile.如果您经常更改 Dockerfile,这可能会很有效。 Sample pipeline below.下面的示例管道。

pipeline {
    agent { dockerfile { dir 'docker' } }

        stage('Other Build') {
            steps {
                // Do other stuff within the Docker image
            }
        }
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM