How to use nested docker.image('test_image').inside() in a Jenkins pipeline

Viewed 657

I have a jenkins container which triggers a maven container to build a java code. Now I want to test the same code with selenium. I have another container for selenium server. But the command to run the test using selenium is a mvn command which needs Maven. Since I am executing this inside the selenium container, mvn is not recognized. How can I use maven container inside the selenium container ? Is nesting possible ? I don't want to install everything in the same container. This is the Jenkinsfile :

    node {
     stage('checkout') {
                git credentialsId: 'Gitlab_cred', url: 'urlhere'
         }

         stage('Build') {
                    docker.image('mvn_custom_image').inside('-u root'){
                      //mvn commands to build the project
                    }
          }

         stage('Archive and Packaging') {
            archiveArtifacts '**/*.war'
          }

         stage('Integration Test') {
            docker.image('selenium/node-firefox:3.4.0-dysprosium').inside{
                 //I want to run mvn command using selenium here
                // This image doesn't have mvn

            }
        }
     }

Can I use "docker.image('mvn_custom_image').inside" inside another container (in my case selenium) ?

Or any other way which has a similar result.

1 Answers

There is good example on https://www.jenkins.io/doc/book/pipeline/docker/

node {
    checkout scm
    docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
        docker.image('mysql:5').inside("--link ${c.id}:db") {
            /* Wait until mysql service is up */
            sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
        }
        docker.image('centos:7').inside("--link ${c.id}:db") {
            /*
             * Run some tests which require MySQL, and assume that it is
             * available on the host name `db`
             */
            sh 'make check'
        }
    }
}
Related