In modern CI/CD pipelines, Jenkins remains a cornerstone for automation. While its master-agent architecture makes it highly scalable, setting up Jenkins agents can be tedious—especially in dynamic or containerized environments. Docker Compose provides a lightweight and declarative way to configure and launch multiple containers, making it an excellent tool for defining Jenkins master and agents.
In this article, we’ll walk through how to add a Jenkins agent using Docker Compose, with detailed steps, configurations, and example YAML and Dockerfiles to ensure you’re ready to integrate agents seamlessly into your CI/CD pipeline.
Understanding the Jenkins Master-Agent Model
Jenkins operates using a master-agent architecture:
-
Master: The primary Jenkins server that orchestrates jobs.
-
Agent: A node (virtual machine or container) that executes build steps.
Agents are especially useful for:
-
Isolating different environments (e.g., Node.js vs Java builds),
-
Parallelizing workloads,
-
Offloading tasks from the master for better performance.
Why Use Docker Compose for Jenkins Agents?
Docker Compose simplifies multi-container configurations and avoids manual network, volume, and dependency management. Benefits include:
-
Declarative service definitions,
-
Built-in networking between master and agent containers,
-
Persistent volumes for Jenkins home,
-
Scalability through service replication.
Prerequisites
Make sure the following tools are installed:
-
Docker Engine
-
Docker Compose (v2 or above)
-
Basic knowledge of YAML and Dockerfiles
Set Up the Project Directory
Create a directory for your Jenkins setup:
Inside this folder, create the following structure:
Create the Jenkins Master Dockerfile
In the jenkins/Dockerfile
, add the following:
This Dockerfile extends the official Jenkins LTS image and optionally installs Docker CLI for builds that need Docker-in-Docker capabilities.
Create the Jenkins Agent Dockerfile
In the agent/Dockerfile
, add:
The jenkins/inbound-agent
image is a prebuilt Jenkins agent that connects back to the master via the JNLP (Java Network Launch Protocol).
Create the Docker Compose File
In the root docker-compose.yml
, define the services:
At this stage, we leave JENKINS_SECRET
empty. It will be filled in after setting up the agent on the Jenkins UI.
Start Jenkins Master
Run:
Open Jenkins in your browser:
To unlock Jenkins:
Use the password to unlock the UI and install recommended plugins.
Create an Agent Node in Jenkins UI
-
Navigate to Manage Jenkins → Nodes → New Node
-
Enter a name (
agent01
) and select Permanent Agent -
Configure:
-
Remote root directory:
/home/jenkins/agent
-
Labels:
docker-agent
-
Launch Method: Launch agent via Java Web Start (inbound agent)
-
-
Save the configuration.
After saving, Jenkins will provide a Secret and a JNLP URL like:
Update the Docker Compose File with Secret
Edit docker-compose.yml
and update the jenkins-agent
section:
Restart the agent:
Verify Agent Connection
Go to Manage Jenkins → Nodes, and the agent agent01
should show as Online.
You can now use this agent in your pipeline jobs by adding:
Automate with Groovy Initialization Script
To fully automate agent registration, create an init.groovy.d
script:
Place this in /var/jenkins_home/init.groovy.d/
via Dockerfile or mounted volume.
Troubleshooting Tips
-
Agent not connecting: Check that the
JENKINS_SECRET
andJENKINS_AGENT_NAME
match Jenkins configuration. -
Firewall or network issues: Make sure port
50000
is open between master and agent. -
Plugin conflicts: Verify you’re using compatible versions of the inbound-agent and Jenkins core.
Jenkins Pipeline Example Using the Agent
Here’s a sample Jenkinsfile
that uses the agent:
Save this Jenkinsfile
in a Git repository and configure a pipeline job to point to it.
Scaling with Docker Compose
You can scale your agents horizontally with:
This spins up three agent containers, each needing separate names and secrets. You’ll need to create corresponding agent configurations in Jenkins or automate them via scripting.
Security Considerations
-
Limit
JNLP
agents to trusted environments. -
Use HTTPS for Jenkins communication.
-
Isolate Jenkins containers using custom networks.
-
Regularly update images to patch vulnerabilities.
Conclusion
Adding Jenkins agents using Docker Compose offers a repeatable, scalable, and efficient approach to extending your CI/CD infrastructure. Instead of manually configuring each node, you can leverage Compose to declare infrastructure as code, seamlessly spin up environments, and even scale horizontally with ease.
By using prebuilt agent containers and linking them to the master over an internal Docker network, you minimize setup friction and maximize environment parity. Integrating secrets and auto-launch mechanisms helps achieve true automation, and with a Groovy script or plugin-based registration, you can scale further into dynamic or ephemeral agents.
Whether you’re a solo developer or running enterprise-level pipelines, Docker Compose gives you the flexibility to iterate faster and deploy with confidence. Start small with a single agent, and soon you’ll find yourself orchestrating a robust Jenkins architecture with just a few YAML lines and a couple of Dockerfiles.