简体   繁体   中英

Jenkins. Running docker containers in parallel (declarative)

I want to run two docker container in declarative Jenkins pipeline, because I have container with backend which utilises Selenium server container for test. I know that there is a scripted example but I wonder if there is a declarative option.

Scripted looks like this:

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'
    }
}

}

In the end I used description from here . withRun - executes commands on the host inside - inside the container

stage ('Test') {
            steps {
                // Create network where I will connect all containers
                sh 'docker network create test'
                script {
                    //withRun command starts the container and doesn't stop it untill all inside is executed.
                    //Commands inside are executed on HOST machine
                    docker.image('selenium/standalone-chrome').withRun("-p 4444:4444 --name=selenium -itd --network=test") {
                        docker.image("$CONTAINER_NAME:front").withRun("-p 3001:80 --name=front -itd --network=test") {
                            //We start backend container...
                            docker.image("$CONTAINER_NAME:back").withRun("-p 8001:80 --name=back -itd --network=test") {
                                //...and with inside command execute commands *surprise* inside the container
                                docker.image("$CONTAINER_NAME:back").inside("-itd --network=test") {
                                    //execute commands inside the container
                                }
                            }
                        }
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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