Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use docker container in jenkins freestyle item?

I am able to create a "Pipeline" item in Jenkins where I am specifing a Jenkinsfile to use a docker container to run all the stages like this:

pipeline {
agent {
    docker { 
        image '<registry>:<port>/<image name>'
        registryUrl '<url>'
        registryCredentialsId 'ARTIFACTORY_CREDS'

    }
}
stages {
    stage ('Environment') {
        steps {
           sh "env | sort"
        }
    }
  }
}

This works fine. But since I am looking to make use of Jenkins release management process for a gradle project, I rather would like to use a "Freestyle project" item. I am not sure how to specify the docker image to use for the build process there. It seems like we cannot provide a Jenkinsfile as well. Any pointers will be helpful. I want to replicate the same behaviour as I am able to with the Jenkinsfile.

Jenkins version: 2.107.3

like image 646
lex Avatar asked Sep 20 '25 11:09

lex


1 Answers

Take a look at this plugin - it worked for me.

If you're running Jenkins itself inside a container using -v /var/run/docker.sock:/var/run/docker.sock there's some extra work to be done. In my case I had to:

  1. Ensure that the volume mounted as JENKINS_HOME in Jenkins could be mounted at the same path by my builder container. I was using a Docker volume and had to migrate to an external volume for that to work.
  2. Make sure that the /tmp directory - as seen from within the Jenkins container - was the same /tmp seen by my builder container. I could achieve that by starting the Jenkins container with an additional -v /tmp:/tmp. This is necessary because the plugin passes the build instructions via a temporary shell script written to that location.
  3. If you're running with selinux active enable the "Run in privileged mode" option in your build environment definition, or the generated artifacts will not be written back to the Jenkins workspace for the job.
like image 140
Danilo Fiorenzano Avatar answered Sep 22 '25 23:09

Danilo Fiorenzano