Tuesday, August 16, 2022

How to setup Docker Containers as Build Agents | Setup dynamic Docker Slave and Integrate with Jenkins Master | Jenkins Build Agent Setup using Docker

Jenkins has powerful feature of master slave architecture which enables distributed builds. This article we will learn how to setup slave nodes using Docker and integrate with Jenkins Master.

Advantages of using Docker Containers as Jenkins Build Agents
  • Ephemeral 
  • Better resource utilization
  • Customized agents as it can run different builds like Java 8, Java 11 
  • Scalability 
Let us see how to configure slave nodes dynamically using Docker. If you like to learn how to setup Jenkins Master on Ubuntu EC2 instance, click here.

Watch this Lab in YouTube channel:


Step 1 - Configure Docker Host with Remote API

Login to Docker host machine. Open docker service file. Search for ExecStart and replace that line with the following.
sudo vi /lib/systemd/system/docker.service

You can replace with below line:
ExecStart=/usr/bin/dockerd -H tcp:// -H unix:///var/run/docker.sock

Restart Docker service
   sudo systemctl daemon-reload
   sudo service docker restart

Validate API by executing below curl command
curl http://localhost:4243/version

Step 2 - Build Jenkins slave Docker image

Download Dockerfile from below repo.
git clone https://github.com/akannan1087/jenkins-docker-slave; cd jenkins-docker-slave

Build Docker image

sudo docker build -t my-jenkins-slave .

Perform below command to see the list of docker images:

sudo docker images

Step 3 - Configure Jenkins Server with Docker plug-in

Now login to Jenkins Master. Make sure you install Docker plug-in in Jenkins.

Now go to Manage Jenkins -> Configure Nodes Cloud

Click on Docker Cloud Details
Enter docker host dns name or ip address
Make sure Enabled is selected
Now click on Test Connection to make sure connecting with docker host is working. 

Step 4 - Configure Docker Agent Templates

Now click on Docker Agent templates:
Enter label as "docker-slave" and give some name
Click on Enabled
Now enter the name of the docker image you have built previously in docker host.
enter /home/jenkins as Remote file system root

Choose Connect with SSH as connection method:
Enter SSH credentials per your Dockerfile - jenkins/password

choose Never Pull as pull strategy as we have already image stored in DockerHost.
Click on Save.

Step 5 - Create build job in Jenkins

Now Create a pipeline job in Jenkins with below pipeline code:

pipeline {
    agent { 
        label "docker-slave"
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'

Click Apply and Save. 
Now build the job. Now you will see output like below:

Create a free style job

Choose Restrict where this project can be run and enter docker-slave as label

Friday, August 5, 2022

Install Jenkins using Docker | Run Jenkins using Docker Compose on Ubuntu 22.0.4 | Install Jenkins using Docker-Compose

Jenkins is an open source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool written in the Java programming language. It is used to implement CI/CD workflows, called pipelines.

How to setup Jenkins using Docker Compose?

Jenkins is Continuous integration server. It is open source and Java based tool. Jenkins can be setup using Docker Compose with less manual steps.

What is Docker Compose?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Since Docker Compose lets you configure related containers in a single YAML file, you get the same Infrastructure-as-Code abilities as Kubernetes. But they come in a simpler system that’s more suited to smaller applications that don’t need Kubernetes’ resiliency and scaling.
The purpose of docker-compose is to function as docker cli but to issue multiple commands much more quickly. To make use of docker-compose, you need to encode the commands you were running before into a docker-compose.yml file
Run docker-compose up and Compose starts and runs your entire app.


  • New Ubuntu EC2 up and running with at least t2.small
  • Port 8080 is opened in security firewall rule
Perform system update
sudo apt update

Install Docker-Compose
sudo apt install docker-compose -y

Create docker-compose.yml
this yml has all configuration for installing Jenkins.
sudo vi docker-compose.yml 

(Copy the below code high-lighted in yellow color)
version: '3'
    image: jenkins/jenkins:lts
    restart: always
    privileged: true
    user: root
      - 8080:8080
      - 50000:50000
    container_name: jenkins
      - /home/ubuntu/jenkins_compose/jenkins_configuration:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock

Save the file by entering :wq!

Execute Docker compose command:
sudo docker-compose up -d 

Make sure Jenkins is up and running by checking the logs
sudo docker-compose logs --follow

Once you see the message, that's it. Jenkins is been installed successfully. press control C and enter.
Now access Jenkins UI by going to browser and enter public dns name with port 8080
Now to go to browser --> http://your_jenkins_publicdns_name:8080/

You can get the Admin password from above command as well.

Enter Admin Password

Enter the password and Click on continue.
Then click on install suggested plug-ins. 
Also create user name and password.
enter everything as admin. at least user name as admin password as admin
Click on Save and Finish. Click on start using Jenkins. Now you should see a screen like below:

That's it. You have setup Jenkins successfully using Docker Compose.

Click here to learn how to create a FreeStyle job in Jenkins to automate Build and Deployment of Java Web App.
Click here to learn how to create a Pipeline job in Jenkins to automate Build and Deployment of Java Web App.