CI as Code Part II: Stateless Jenkins With Dynamic Docker Slaves

Introduction

This article continues on from part one of this series, which looks at ‘CI as code’ using Docker to set up isolated and reproducible phoenix deployments of Jenkins deployments

Here I add dynamic Docker containers as on-demand Jenkins nodes in a Docker cloud.

Code

This is now baked into v2.0 of the git repo.

Video

Here’s a video of the stateless setup of the Docker cloud, and the job ‘docker-test’ which dynamically provisions a Docker container to run as a Jenkins slave.

What it does

  1. Starts up Jenkins container with a server config config.xml preloaded
  2. ‘jenkinssetup’ container waits for Jenkins to be up and ready
  3. Sets up global credentials
  4. Updates Jenkins’ config.xml with the credentials id
  5. Restart Jenkins and wait for jenkins to be ready
  6. Kick off install of plugins
  7. Periodically restart Jenkins until plugins confirmed installed
  8. Upload job configurations

 

Details

The Docker plugins for Jenkins are generally poorly documented and fiddly to set up. And between them there’s quite a few, so the Docker options in a job available can get quite confusing. This took a little bit of trial and error before I could reliably get it to work.

To allow dynamic Docker provisioning, I used the standard docker plugin, mainly because it was the only one I ended up getting working with my Jenkins-in-docker-compose approach.

To get a dynamic on-demand Docker instance provisioned for every build, you have to set up a Docker cloud with the details of the Docker host to contact to spin up the container. This cloud is given a label, which you use in your job to specify that it should be run in a Docker container.

Currently co-authoring a book on Docker:

Get 39% off with the code 39miell

dip

 

Note: If you want to recreate this you must have an opened-up Docker daemon.
See here for a great guide on this. Once that’s done you may need to change the
docker host address in the docker.xml field to point to your opened up Docker
daemon. Usually this is with the IP address outputted from ‘ip route’ in your
running containers. The default in the git repo is fine, assuming you have opened
it up on port 4243.

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to CI as Code Part II: Stateless Jenkins With Dynamic Docker Slaves

  1. Pingback: CI as Code Part I: Stateless Jenkins Deployments Using Docker | zwischenzugs

  2. Pingback: CI as Code Part III: Dynamic Jenkins-Swarm Example | zwischenzugs

  3. Jim Lack says:

    Hi The concept is great an just what i’m looking for
    I’m unable to get the docker build job to work though.
    Significant output is :
    Building remotely on jenkinsdockerslave1 (jenkinsdockerslave)

    [docker-build] $ docker build -t test –no-cache=true –pull=true –file=/tmp/workspace/docker-build/Dockerfile /tmp/workspace
    time=”2017-04-13T08:46:15Z” level=fatal msg=”Cannot connect to the Docker daemon. Is ‘docker -d’ running on this host?”

    • zwischenzugs says:

      I think you need to install docker, or allow the user running docker to have the perms to run the client.

      • Jim Lack says:

        A bit more context here –
        I’m using your stack ootb – and assumed your jenkinsdockerslave would have the right permissions for jenkins master to run docker builds but it only works with sudo prefixing the commands consequently the docker-build job fails.
        I have tried docker commands from the command line using the jenkins user within the jenkinsdockerslave container they fail, though they run fine as root, also if i prefix any of the commands with sudo they work fine (as i notice you add jenkins to sudoers). What I’m wondering is how i get the jenkins user within this container to run docker commands without sudo.
        I have tried adding jenkins to the docker group (sudo usermod -aG docker jenkins) as many posts suggest with no luck.
        Assuming i can get jenkins to run docker with root privileges I think I will be able to use the pipeline jenkinsfile (groovy) and use the docker.withServer to run the build step. This would then allow me to bootstrap jenkins with the cd pipeline coming from git.

  4. zwischenzugs says:

    It gets a bit complicated there as the docker server is on the host, while the client is in a separate environment, and matching user ids gets tricky, hence using root via sudo is ‘simpler’.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s