Operations 5 min read

Jenkins Slave Docker Image Configuration and Startup Script

This guide explains how to configure a Jenkins slave Docker image by installing JDK, Maven, and other build tools, adding the agent.jar, setting environment variables, and providing a startup Bash script to launch the Jenkins agent within the container.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Jenkins Slave Docker Image Configuration and Startup Script

This document provides step‑by‑step instructions for building a Docker image that serves as a Jenkins slave (agent) and for launching the agent inside the container.

Prerequisites include downloading a JDK distribution, Maven (or other build tools), and the agent.jar file from the Jenkins master site.

Dockerfile used to create the image:

FROM jenkinsci/slave

ARG user=jenkins
ARG agent_workdir=/home/${user}/agent
ENV jenkins_script=/usr/local/bin/
USER root

# Replace JDK
ADD  buildtools/jdk-8u121-linux-x64.tar.gz /usr/local/

# Replace agent.jar and startup script
RUN rm -fr ${agent_workdir}/agent.jar
COPY agent.jar ${agent_workdir}/agent.jar
COPY jenkins-slave ${jenkins_script}/jenkins-slave
RUN chown ${user} ${agent_workdir}/agent.jar \
   && chown ${user} ${jenkins_script}/jenkins-slave

# Add build tools
ADD buildtools/apache-maven-3.5.0.tar.gz /usr/local/
ADD buildtools/sonar-scanner.tar.gz /usr/local/
ADD buildtools/apache-ant-1.9.9-bin.tar.gz /usr/local/
ADD buildtools/apache-jmeter-5.1.1.tgz /usr/local/

ENV JMETER_HOME=/usr/local/apache-jmeter-5.1.1/
ENV ANT_HOME=/usr/local/apache-ant-1.9.9
ENV M3_HOME=/usr/local/apache-maven-3.5.0

# JMeter plugins
COPY jmeter-plugins/ant-jmeter-1.1.1.jar ${ANT_HOME}/lib/ant-jmeter-1.1.1.jar
COPY jmeter-plugins/fastjson-1.2.9.jar  ${JMETER_HOME}/lib/ext/fastjson-1.2.9.jar
COPY jmeter-plugins/jmeter-plugins-cmn-jmeter-0.5.jar  ${JMETER_HOME}/lib/ext/jmeter-plugins-cmn-jmeter-0.5.jar
COPY jmeter-plugins/jmeter-plugins-manager-1.3.jar  ${JMETER_HOME}/lib/ext/jmeter-plugins-manager-1.3.jar
COPY jmeter-plugins/jmeter-results-detail-report_21.xsl  ${JMETER_HOME}/extras/jmeter-results-detail-report_21.xsl
COPY jmeter-plugins/jmeter-results-shanhe-me.xsl  ${JMETER_HOME}/extras/jmeter-results-shanhe-me.xsl
COPY jmeter-plugins/jmeter.properties ${JMETER_HOME}/bin/jmeter.properties
COPY jmeter-plugins/jmeter-plugins-json-2.7.jar ${JMETER_HOME}/lib/ext/jmeter-plugins-json-2.7.jar
COPY jmeter-plugins/Tools.jar ${JMETER_HOME}/lib/ext/Tools.jar

RUN touch ${JMETER_HOME}/bin/jmeter.log \
    && chmod 777 ${JMETER_HOME}/bin/jmeter.log

# Environment variables
ENV JAVA_HOME=/usr/loca/jdk1.8.0_121
ENV PATH=${PATH}:${JAVA_HOME}/bin:${ANT_HOME}/bin:${JMETER_HOME}/bin:${M3_HOME}/bin

CMD ["sh","-x","/usr/local/bin/jenkins-slave"]

The Dockerfile starts from the official jenkinsci/slave image, adds a specific JDK, replaces the default agent.jar, copies a custom launch script, installs Maven, Ant, JMeter and several JMeter plugins, and finally sets the required environment variables.

Slave startup script (to be placed inside the image):

#!/bin/bash

#env | grep "JENKINS" >>env.txt

env | grep "JENKINS"

echo "[DEBUG] ----nohup java -jar /usr/share/jenkins/agent.jar -jnlpUrl ${JENKINS_URL}/computer/${JENKINS_AGENT_NAME}/slave-agent.jnlp -secret ${JENKINS_SECRET} -workDir /home/jenkins/agent >slave.log & -----"

java -jar /home/jenkins/agent/agent.jar -jnlpUrl ${JENKINS_URL}/computer/${JENKINS_AGENT_NAME}/slave-agent.jnlp -secret ${JENKINS_SECRET} -workDir "/home/jenkins/agent "

The script prints Jenkins‑related environment variables for debugging and then launches the agent using the java -jar command with the appropriate JNLP URL, secret, and working directory.

After building the image (e.g., docker build -t jenkins‑slave .) and running a container, the agent will automatically connect to the Jenkins master, ready to execute builds that require the pre‑installed tools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdAutomationmavenJMeterJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.