Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用多个Docker容器在Jenkins管道中设置Jenkins代理_Docker_Jenkins_Docker Compose_Jenkins Pipeline_Dockerfile - Fatal编程技术网

如何使用多个Docker容器在Jenkins管道中设置Jenkins代理

如何使用多个Docker容器在Jenkins管道中设置Jenkins代理,docker,jenkins,docker-compose,jenkins-pipeline,dockerfile,Docker,Jenkins,Docker Compose,Jenkins Pipeline,Dockerfile,下面的代码片段是Cypress提供的一个示例,Cypress是我正在使用的Javascript测试框架。下面是Github页面的链接 pipeline { agent { // this image provides everything needed to run Cypress docker { image 'cypress/base:10' } } stages { // first stage installs node depe

下面的代码片段是Cypress提供的一个示例,Cypress是我正在使用的Javascript测试框架。下面是Github页面的链接

pipeline {
  agent {
    // this image provides everything needed to run Cypress
    docker {
      image 'cypress/base:10'
    }
  }

  stages {
    // first stage installs node dependencies and Cypress binary
    stage('build') {
      steps {
        // there a few default environment variables on Jenkins
        // on local Jenkins machine (assuming port 8080) see
        // http://localhost:8080/pipeline-syntax/globals#env
        echo "Running build ${env.BUILD_ID} on ${env.JENKINS_URL}"
        sh 'npm ci'
        sh 'npm run cy:verify'
      }
    }

    stage('start local server') {
      steps {
        // start local server in the background
        // we will shut it down in "post" command block
        sh 'nohup npm run start:ci &'
      }
    }

    // this stage runs end-to-end tests, and each agent uses the workspace
    // from the previous stage
    stage('cypress parallel tests') {
      environment {
        // we will be recording test results and video on Cypress dashboard
        // to record we need to set an environment variable
        // we can load the record key variable from credentials store
        // see https://jenkins.io/doc/book/using/using-credentials/
        CYPRESS_RECORD_KEY = credentials('cypress-example-kitchensink-record-key')
        // because parallel steps share the workspace they might race to delete
        // screenshots and videos folders. Tell Cypress not to delete these folders
        CYPRESS_trashAssetsBeforeRuns = 'false'
      }

      // https://jenkins.io/doc/book/pipeline/syntax/#parallel
      parallel {
        // start several test jobs in parallel, and they all
        // will use Cypress Dashboard to load balance any found spec files
        stage('tester A') {
          steps {
            echo "Running build ${env.BUILD_ID}"
            sh "npm run e2e:record:parallel"
          }
        }

        // second tester runs the same command
        stage('tester B') {
          steps {
            echo "Running build ${env.BUILD_ID}"
            sh "npm run e2e:record:parallel"
          }
        }
      }

    }
  }

  post {
    // shutdown the server running in the background
    always {
      echo 'Stopping local server'
      sh 'pkill -f http-server'
    }
  }
}
我的目标是创建一个与上面非常相似的Jenkins文件,因为我希望进行并行Cypress测试,如上面的代码片段所示。在上面的示例中,Jenkins代理只是官方的Cypress Docker图像
Cypress/base:10

  agent {
    // this image provides everything needed to run Cypress
    docker {
      image 'cypress/base:10'
    }
  }
然而,为了使用自己的数据库运行所有测试,我需要启动两个单独的Docker容器。一个容器包含我的web应用的前端部分,另一个容器包含我的web应用的后端部分

下面是我的前端容器的Dockerfile,它位于
my app/docker/combined/Dockerfile

FROM cypress/included:3.4.1

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5000

RUN npm install -g history-server nodemon

RUN npm run build-test

EXPOSE 8080
下面是我的后端容器的Dockerfile,它位于
my app/docker/db/Dockerfile
中。它所做的只是将一些本地数据复制到Docker容器中,然后用这些数据初始化我的MongoDB数据库

FROM  mongo:3.6.14-xenial

COPY ./dump/ /tmp/dump/

COPY mongo_restore.sh /docker-entrypoint-initdb.d/

RUN chmod 777 /docker-entrypoint-initdb.d/mongo_restore.sh
通常,我会使用
docker compose
和下面的
docker compose.yml
文件来旋转这两个容器。如您所见,称为“combined”的前端容器依赖于称为“db”的后端容器

下面是我将使用的docker compose命令

docker-compose up --build

我希望我的詹金斯代理人成为
组合的
容器;但是,我需要
组合的
容器来连接到我的
db
容器,它需要旋转起来。我的问题是,我如何在Jenkins管道中实现这一点?我读过;但是,它没有提到任何关于使用多个DockerFile创建Jenkins代理的内容。这样的事情可能吗?有人能告诉我为了实现我的目标,我的Jenkins文件应该是什么样子吗?

我认为你不能在并行版本之间进行通信,问题是,理论上,在Jenkins文件的不同
并行
阶段中运行的阶段可以在不同的Jenkins从属上运行,因此无法相互通信

我建议在同一阶段使用
&
并行运行服务器和前端应用程序,然后调用
wait
等待所有进程退出

您甚至可以在Jenkins文件中放弃使用
docker
关键字,而是在后台调用docker。

考虑运行“sidecar”容器:

感谢您的回复!我认为对于并行构建可能存在一些误解。我不介意并行构建是否不能相互通信。我只想有一个由
组合的
db
组成的代理。在这个阶段,我只想模拟Cypress提供的Jenkinsfile示例,在我的应用程序上测试并行构建。
docker-compose up --build