2

I would like to write a little groovy-script to configure docker in my jenkins server so I can include the groovy-script to an ansible-playbook.

I found some examples how to configure maven (with the auto-installer-option) or sonarqube with groovy in jenkins. But I did not figured out how to do this for docker

Based on the things I have seen I wrote this code:

import jenkins.model.*
import hudson.plugins.*
import hudson.plugins.docker.*
import hudson.plugins.docker.model.*


def inst = Jenkins.getInstance()

def desc = inst.getDescriptor("hudson.plugins.docker.commons.tools.DockerTool")

and till this point the code runs without exception. But now I am facing the problem that I don't know how to set the fields like Path and Home Directory. I try it with:

def dinst = new DockerToolInstaller()

and

def sinst = new DockerInstaller()

but this only gives me a Cannot Resolve Class-Error.

I wouldn't like to start use the configuration as a code plugin for a single tool and that's why I am hoping someone here can give an hint how to configure docker in Jenkins with groovy

  • Can you clarify what exactly you want to achieve? Docker is already installed on the sysem, what configuration do you want to set? – Dominik Gebhart Sep 23 '19 at 18:52
  • My Jenkins is running in a docker-Container (via docker-compose) and as far as I know I have to set the path to the docker client in the global configuration tool to use it in my build-scripts –  Sep 24 '19 at 14:45
  • If you want to be able to use docker in your jenkins container, you need to expose the host system docker daemon to the container, this you'd need to set in your compose file. Maybe [this](https://stackoverflow.com/questions/27879713/is-it-ok-to-run-docker-from-inside-docker) helps? – Dominik Gebhart Sep 25 '19 at 08:26
  • Yes I know I have to mount the docker socket from the host into the container and the install the docker client (in the same version) in the container. All this will do an ansible script for me, but the problem for me is to automate the configuration in jenkins. Manually I just would set in the global configuration section settings Docker to the client-executable in the container, but how can I automate this? After searching with google it seems I have two options first use a plugin like configuration as a code or write a little groovy script with access the Jenkins API –  Sep 25 '19 at 14:39

0 Answers0