Setting up a Jenkins instance in Docker with multiple slaves

I wanted to have a play environment when experimenting with Jenkins, but I also needed slaves for the experiments I was running.

As per usual, I wanted to set this all up in Docker so I could build and destroy instances as needed.

This is just a play setup - for real life there should be:

  • Much better automation around setup
  • plugins should be auto-installed
  • slaves should be installed in a better system (Kubernetes!)

Set up a default jenkins server

This is directly from the official LTS version

docker run \
    -p 8080:8080 \
    -p 50000:50000 \
    --name jenkins \
    --net=jenkins \
    --net-alias=master \
    jenkins/jenkins:lts

Note that there will be output in the log files saying what the master password is for setting up the server. You will need this to get started.

Building the slaves

The slaves need to be linux boxes with the following installed:

  • sshd
  • jdk
  • maven
  • jenkins user created
  • port 22 exposed
  • root login enabled

Building such a slave in Docker:

Jenkins slave Dockerfile

FROM maven:3.5-jdk-8-alpine as build
Maintainer Andrew Monkhouse
RUN apk --update add git openssh-server openssh tar vim net-tools subversion \
    && mkdir /var/run/sshd \
    && sed -ri 's/^PermitRootLogin\s+.*/PermitRootLogin yes/' \
               /etc/ssh/sshd_config \
    && sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config \
    && addgroup jenkins \
    && adduser -D -G jenkins -s /bin/bash jenkins \
    && echo 'jenkins:jenkins01' |chpasswd \
    && rm -rf /var/lib/apt/lists/* \
    && rm /var/cache/apk/* \
    && ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa \
    && ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa 

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

To build it:

docker build -t amonkhouse/linuxsshjavamvn .

To use as a Jenkins slave

Set up a network that we can use

docker network create jenkins

Create a couple of slaves using the image we created

Note that there are two slaves, both on the same network, different hostnames that we can connect to

docker run -d --name slave1 --net jenkins --net-alias slave1 amonkhouse/linuxsshjavamvn
docker run -d --name slave2 --net jenkins --net-alias slave2 amonkhouse/linuxsshjavamvn

set up the ssh connectivity from jenkins master to slaves

Run these commands for a shell within the master

mkdir /var/jenkins_home/.ssh/
ssh slave1 # ...
ssh slave2 # ...

TBH I’m not sure if the ssh connection needs to be completed, as long as the ssh key gets stored in the known_hosts file

Configure the slave within Jenkins

Important things to remember:

  • You may want to set the home directory for the slave to /home/jenkins/ (since it already exists). If you want to use something more standard (such as /opt/jenkins) you will need to set up the base directory first.
  • If you need to define where java lives, you should be able to use /usr/bin/java
  • You will need to set up credentials to log into the slaves. Per the Dockerfile, this is jenkins/jenkins01

ToDo:

I should be able to automate this so the manual steps can be removed