dockerjenkinsjenkins-pipelinejenkins-groovyjenkins-docker

Jenkins build agent docker specify docker file name


I was following this snippet to add to docker file for jenkins build agent (https://www.jenkins.io/doc/book/pipeline/docker/),

pipeline {
agent { dockerfile true }
stages {
    stage('Test') {
        steps {
            sh 'node --version'
            sh 'svn --version'
        }
    }
}
}

'dockerfile true' looks for the default Dockerfile. Is there a way to specify the docker file name as I don't want to use the default one because my settings would be different than the one already there?


Solution

  • Yes, you can absolutely specify the dockerfile file name in the agent directive. An example of this (with other arguments for the dockerfile parameter in agent) would be:

    agent { // agent directive
      // Equivalent to "docker build -f Dockerfile.build --build-arg version=1.0.2 ./build/
      dockerfile { // dockerfile parameter
        filename              'Dockerfile.build' // filename argument and value
        dir                   'build'
        label                 'my-defined-label'
        additionalBuildArgs   '--build-arg version=1.0.2'
        args                  '-v /tmp:/tmp'
        registryUrl           'https://myregistry.com/'
        registryCredentialsId 'myPredefinedCredentialsInJenkins'
      }
    }
    

    In the documentation for pipeline agent parameters, you can scroll down to the dockerfile parameter to view the arguments for the parameter and how they allow agent customization.