Python – Use Jenkinsfile to run two docker containers in “sidecar” mode

Use Jenkinsfile to run two docker containers in “sidecar” mode… here is a solution to the problem.

Use Jenkinsfile to run two docker containers in “sidecar” mode

I have to run two docker containers in sidecar mode. I spent days trying to implement the right pipe, but now I’m desperate because none of my ideas really worked.

First, I read the block Running sidecar containers

It implements the method in the node {} block. Here’s what my pipeline looks like now:

pipeline {
agent any

options {
    disableConcurrentBuilds()
}

stages {
    stage("Obtain images") {
        steps {
            script {
                    writeFile(file: './ui-tests/env.yaml', text: "environment:\n  # dev or stage\n  type: dev\n  lang: en\n")
                    withDockerRegistry([credentialsId: 'my-hub', url: 'https://myhub.hub']) {
                        def selena = docker.image('myhub.hub/selenium-dev:latest')
                        def uirun = docker.image('myhub.hub/ui-shell-runner:latest')
                        selena.pull()
                        uirun.pull()
                    }

}
        }
    }

stage("Run tests") {
       steps {
        node('pyrunner') { 
                docker.image('myhub.hub/selenium-dev').withRun('-e JAVA_OPTS=-Xmx1024m') { test ->
                    docker.image('myhub.hub/selenium-dev').inside("--link ${test.id}:se") {
                        sh 'pwd'
                    }

docker.image('myhub.hub/ui-shell-runner:latest').inside("--link ${test.id}:se") {
                            sh 'cd ui-tests ; pwd ; nose2 -v --attribute desktop-site,type=smoke'
                        }
                }
        }
        }
        }
    }
}

It’s an idea. The first container selenium myhub.hub/selenium-dev – runs in the background, and the second container myhub.hub/ui-shell-runner contains the nose2 utility that utilizes the first container. I tried implementing node, tried removing it completely and moving my code to steps, I tried removing everything and running my container via sh ‘docker container run…’ (although that’s not good). None of my ideas really worked as much as I expected. and lists pipeline crashes

    WorkflowScript: 28: Expected a symbol @ line 28, column 6.
                    docker.image('myhub.hub/selenium-dev').withRun('-e JAVA_OPTS=-Xmx1024m') { test ->

WorkflowScript: 28: Arguments to "error" must be explicitly named. @ line 28, column 6.
                    docker.image('myhub.hub/selenium-dev').withRun('-e JAVA_OPTS=-Xmx1024m') { test ->

I ran out of ideas. What am I going to do next?

Solution

This is the code for me. Thanks Denis Khokhryakov for his help and advice! In this example, “se” is the DNS name of the first Selenium container, which we can also use to connect in the second client container.

pipeline {
agent any

options {
    disableConcurrentBuilds()
}

stages {
    stage("Obtain images") {
        steps {
            script {
                writeFile(file: './ui-tests/env.yaml', text: "environment:\n  # dev or stage\n  type: dev\n  lang: en\n")
                withDockerRegistry([credentialsId: 'my-hub', url: 'https://myhub.hub']) {
                    def selena = docker.image('myhub.hub/selenium-dev:latest')
                    def uirun = docker.image('myhub.hub/ui-shell-runner:latest')
                    selena.pull()
                    uirun.pull()
                }
            }
        }
    }

stage("Run tests") {
        steps {
            script { 
                docker.image('myhub.hub/selenium-dev').withRun('-e JAVA_OPTS=-Xmx1024m') { test ->
                    docker.image('myhub.hub/selenium-dev').inside("--link ${test.id}:se") {
                        sh 'pwd'
                    }

docker.image('myhub.hub/ui-shell-runner:latest').inside("--link ${test.id}:se") {
                        sh 'cd ui-tests ; pwd ; nose2 -v --attribute desktop-site,type=smoke'
                    }
                }
            }
        }
    }
}

Related Problems and Solutions