Home > database >  Jenkins-Pipeline: How to use multiple docker containers, each one being specified by a Dockerfile?
Jenkins-Pipeline: How to use multiple docker containers, each one being specified by a Dockerfile?

Time:09-21

I have multiple stages defined inside my Jenkinsfile (pipeline mode).

I would like to run one stage (the local build) inside one Docker container, and some other stages (the cross-build) inside another Docker container.

However, I would also like the Docker containers to be specified by corresponding Dockerfiles instead of just by an image name, since I need some customisation in the respective containers.

The Jenkins documentation states that both these things are possible.

However, it also states that Dockerfile will always be used as the filename for the docker container specification.

How can I create two separate Dockerfiles and tell Jenkins to use one of them for one stage, and the other one for some other stage(s)?

From my understanding, the Jenkinsfile should look something like this:

pipeline {
    agent None
    stages {
        stage('Local Build') {
            agent { dockerfile true }
            steps {
                sh 'mvn --version'
            }
        }
        stage('Cross-build') {
            agent { dockerfile true }   // <--- How do I specify *which*
                                        //      Dockerfile to use here?
            steps {
                sh 'node --version'
            }
        }
    }
}

CodePudding user response:

You can add the following details to the Dockerfile agent. So you can simply use filename and dir options.

agent {
    // Equivalent to "docker build -f Dockerfile.build --build-arg version=1.0.2 ./build/
    dockerfile {
        filename 'Dockerfile.build'
        dir 'build'
        label 'my-defined-label'
        additionalBuildArgs  '--build-arg version=1.0.2'
        args '-v /tmp:/tmp'
    }
}
  • Related