Jenkins nodes on Docker containers

Jenkins nodes on Docker containers

Jenkins is the most popular open-source automation server written in Java. It has many interesting plugins and features. Today, I’m going to show you one of them – how to set up Jenkins master server with one slave instance connected to master. So that we will be able to run distributed builds using few docker containers. For that sample we use docker images of Jenkins (jenkins) and Jenkins slave (jenkinsci/jnlp-slave). Let’s start from running Jenkins docker container.

$ docker run -d --name jenkins -p 50000:50000 -p 50080:8080 jenkins

Go to management console (http://192.168.99.100:50080) and select Manage Jenkins -> Manage Nodes and then click New Node. In the next page you have to put the slave name – for that sample is slave-1. After clicking OK you will see new node on the list. Now, you can configure it by clicking setting button and display node details by clicking node name on the list.

jenkins-slave

New node is created by is still disabled. After clicking node you will see the page with details. The important information is in command secret line property. Copy that token.

jenkins-slave1

Now, we are going to run the docker image with the JNLP agent. In the docker run command we paste Jenkins master URL, secret token and chosen node name (slave-1). If you would like to set up it without docker container you should download the slave agent JAR file by clicking Launch button and run agent from command line like in the picture above.

$ docker run -d --name jenkins-slave1 jenkinsci/jnlp-slave -url http://192.168.99.100:50080 5d681c12e9c68f14373d62375e852d0874ea9daeca3483df4c858ad3556d406d slave-1

After running slave container you should see name slave-1 in the Build Executor Status below master node.

jenkins-slave2

Now, we could configure the sample Jenkins pipeline to test our new slave. Pipeline builds could be run on the master node or on the slave node. Here sample pipeline fragment. For trying that sample you need to have Pipeline Plugininstalled on your Jenkins server.

node() {
   stage('Checkout') {
      ...
   }

   stage('Build') {
      ...
   }
}

You can select the node for running your pipeline by providing a node name. Now, build always run on slave-1 node.

node('slave-1') {
   stage('Checkout') {
      ...
   }

   stage('Build') {
      ...
   }
}

0 COMMENTS

comments user
Gramtech

Thanks for this. Its the first post I managed to put all the peices together to enable me to build the node instances via docker.

comments user
Sergey

I got java -jar agent.jar -jnlpUrl xxxxxxxx. Should I change a jenkins-slave file?

    comments user
    Piotr Mińkowski

    No, you can pass some envs to the container to customize it. Here’s path to slave jar: $JENKINS_URL/computer/$JENKINS_SLAVE_NAME/slave-agent.jnlp

Leave a Reply