docker - 在 Jenkinsfile 中使用 Dockerfile 创建的 docker 容器内运行命令

标签 docker jenkins jenkins-pipeline

我创建了一个 Jenkins 管道文件并创建了一个 Jenkinsfile。在 Jenkinsfile 中,我使用了 Dockerfile 代理,然后运行了一些步骤。这些步骤应该在 docker 内部运行,但它们在主机上运行。
她的代码。

#!/usr/bin/env groovy
@Library(['abc-jenkins','xyz-jenkins-library']) _
pipeline {
agent {
    dockerfile{
        dir 'TEST'
        filename 'dockerfile'
        label 'docker'
    }
}
stages {
    stage('Build Stage') {
        steps {
            echo 'testing stage running'
            sh "ls"
        }
    }
}   
}

最佳答案

你是根据ls的输出误会了.

对于 Jenkins 管道 docker DSL,它将 jenkins 作业工作区挂载到 docker 容器并更改 docker WORKDIR到 Jenkins 工作区。

因此,当您执行 ls 时在容器内,它将在您的 jenkins 作业工作区下打印文件和文件。

您可以尝试其他 cmd 或 ls主机中不存在的其他文件夹来批准它实际上是在容器内而不是主机内执行的。

关于docker - 在 Jenkinsfile 中使用 Dockerfile 创建的 docker 容器内运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486231/

相关文章:

java - Docker - 将 Apache Tomcat Web 服务器连接到 MySQL 服务器

python - django.core.exceptions.ImproperlyConfigured : Could not find the GDAL library

hudson - 批量禁用 Jenkins 工作

scala - 如何将环境变量传递给 Jenkins 中的 sbt 测试构建步骤?

jenkins - 将新的本地用户添加到已配置 Active Directory 的 jenkins

jenkins - println 在 "call"的 "vars/foo.groovy"方法中有效,但不在类中的方法中

powershell - 如何在上游作业中获取下游作业的控制台输出?

linux - docker-compose up 非常慢,但 docker-compose 在 Linux 上运行非常快

linux - Docker 镜像/容器里面有什么?

linux - 无法在 Jenkins Execute Shell 的 docker 容器内执行命令