简体   繁体   English

如何在脚本化的 Jenkinsfile 管道中构建和运行 Docker 映像?

[英]How to build and run a Docker image in a scripted Jenkinsfile pipeline?

I'm trying to build and run a docker image on a specific build node from a scripted Jenkinsfile.我正在尝试从脚本化的 Jenkinsfile 在特定构建节点上构建和运行 docker 映像。 Switching to declarative syntax is something I would rather like to avoid.我宁愿避免切换到声明式语法。 My code is quite close to the example from the documentation .我的代码与文档中的示例非常接近。 The image builds as expected.图像按预期构建。 But running the container fails Jenkins complaining the physical machine of the node is not running inside a container and the echo and make commands from the innermost block that I would expect to run inside the container are not executed and do not appear in the log.但是运行容器失败詹金斯抱怨节点的物理机器没有在容器内运行,并且来自我希望在容器内运行的最内层块的 echo 和 make 命令没有执行并且不会出现在日志中。

As far as I understand Jenkins considers containers to be build nodes on their own and that nesting of node statements are not allowed.据我了解,Jenkins 认为容器本身就是构建节点,并且不允许嵌套节点语句。 At the same time a node is required to build and run the Docker image.同时需要一个节点来构建和运行 Docker 镜像。

What am I missing to build and run the image?我缺少什么来构建和运行图像? As Im quite new to Jenkins as well as to Docker any hints or recommendations are appreciated.由于我对 Jenkins 和 Docker 都很陌生,因此我很感激任何提示或建议。

The code:编码:

node('BuildMachine1')
{
    withEnv(envList)
    {
        dir("/some/path")
        {
            docker.build("build-image:${env.BUILD_ID}", "-f ${env.WORKSPACE}/build/Dockerfile .").inside
            {
                echo "Echo from Docker"
                sh script: 'make'
            }
        }
    }
}

The log:日志:

Successfully built 8c57cad188ed
Successfully tagged build-image:79
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . build-image:79
.
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] withDockerContainer
BuildMachine1 does not seem to be running inside a container
$ docker run -t -d -u 1004:1005 -w /data/Jenkins_Node/workspace/myFeature/buildaarch64Release -v /data/Jenkins_Node/workspace/myFeature/buildaarch64Release:/data/Jenkins_Node/workspace/myFeature/buildaarch64Release:rw,z -v /data/Jenkins_Node/workspace/myFeature/buildaarch64Release@tmp:/data/Jenkins_Node/workspace/myFeature/buildaarch64Release@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** build-image:87 cat
$ docker top 2242078968bc1ee5ddfd08c73a2e1551eda36c2595f0e4c9fb6e9b3b0af15b8b -eo pid,comm
[Pipeline] // withDockerContainer

Looks like the Entrypoint of the container was configured in a way that worked for manual usage in terminal but not inside the Jenkins pipeline.看起来容器的入口点的配置方式适用于终端中的手动使用,但不适用于 Jenkins 管道内。 It was set as ENTRYPOINT ["/usr/bin/env", "bash"] After changing it to ENTRYPOINT [ "/bin/bash", "-l", "-c" ] the resulting container is used by the Jenkinsfile as intended.它被设置为ENTRYPOINT ["/usr/bin/env", "bash"]将其更改为ENTRYPOINT [ "/bin/bash", "-l", "-c" ]之后,Jenkinsfile 使用了生成的容器如预期。

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

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