Burnt Out Patching Worker Nodes? Let Docker Be Your Jenkins Agent


Are you tired of constantly patching worker nodes, installing dependencies, and dealing with different OS distributions every other day?
Well, I’ve been there. And I’m here to help you out!
To be precise — I’m talking about configuring Docker containers as Jenkins worker nodes. Sounds interesting, right?
Let’s get started.
Step 1: Launch an EC2 Instance and Install Jenkins
First, launch a fresh Ubuntu EC2 instance (t2.micro works fine for learning).
Jenkins is a Java-based application, so the first prerequisite is Java.
Install Java
sudo apt update
sudo apt install openjdk-17-jre -y
Now that Java is installed, you're ready to install Jenkins.
Install Jenkins
sudo apt update
sudo apt install jenkins -y
Step 2: Allow Port 8080 (Security Group Change)
By default, Jenkins listens on port 8080. Go to your EC2 security group and add an inbound rule to allow TCP traffic on port 8080.
Then open Jenkins in your browser:
http://<your-ec2-public-ip>:8080
Step 3: Unlock Jenkins and Install Plugins
SSH into your instance and get the admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Paste it into the browser when prompted.
Click Install Suggested Plugins.
Create your first admin user or skip it if you're experimenting.
Jenkins is now up and running!
Step 4: Install Docker and Give Jenkins Access
Install Docker:
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
Jenkins runs as a non-root user and won’t be able to access Docker unless you add it to the docker
group.
Grant Access:
sudo usermod -aG docker jenkins
sudo usermod -aG docker ubuntu
sudo systemctl restart docker
Tip: If you're doing master and agent on the same machine for testing, you're all set. If you're using a second EC2 for the agent, repeat this setup there too.
Step 5: Install Docker Pipeline Plugin in Jenkins
Go to Manage Jenkins > Manage Plugins
Under the Available tab, search for Docker Pipeline
Check it, click Install, and restart Jenkins
What Does This Plugin Actually Do?
When Jenkins sees a pipeline with agent { docker { ... } }
, it doesn't know how to run that in Docker — until this plugin is installed.
The Docker Pipeline plugin helps Jenkins to:
Pull the specified Docker image from Docker Hub
Start a container from that image
Mount the Jenkins workspace inside the container
Run your pipeline steps inside the container
Clean up the container after the job is done
All this — automatically.
Step 6: Test It All in Action
Now, let's create a real pipeline and verify that Docker is working as a Jenkins agent.
Create a GitHub Repo
Make a repo named, for example: jenkins-project
Inside the repo root, create a file named: Jenkinsfile
Paste This Pipeline Code
pipeline {
agent {
docker { image 'node:16-alpine' }
}
stages {
stage('Test') {
steps {
sh 'node --version'
}
}
}
}
This code:
Tells Jenkins to spin up a container using the
node:16-alpine
imageRuns the
node --version
command inside the container
Configure Jenkins Job
Go back to Jenkins
Click New Item → Choose Pipeline
Name it something like
docker-agent-test
Under Pipeline > Definition, choose Pipeline script from SCM
Select Git, paste your repo URL
Set branch as
main
Set script path as
Jenkinsfile
Save and click Build Now
You should see the Node.js version printed in the console output.
✅ If yes, then boom — your Docker-based Jenkins worker is working!
You're Done — Cheers!
That’s it! No more patching VMs endlessly or dealing with "it works on my machine" chaos.
You now have a powerful, clean Jenkins setup using Docker containers as agents — ready to build anything in isolated, reproducible environments.
Hope you like it:)
Subscribe to my newsletter
Read articles from Sravya Bolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
