Jenkins is an open-source CI/CD tool for creating pipelines in software development. This automation server written in Java supports popular version control tools such as Git and assists in building Ant and Apache Maven projects.
Jenkins works with different build environments. The main node in the Jenkins installation, the Jenkins controller, balances parallel jobs from multiple build agents. The agents connect either locally or via the cloud.
This article will show you how to configure Docker containers to work as Jenkins build agents.
Set up Docker Host
1. Use a tool such as Nmap to check if the relevant ports are open. Docker Remote API uses port 4243, while ports 32768 to 60999 are assigned to Jenkins for connecting with Docker containers.
2. Open the
docker.service file in a text editor:
sudo nano /lib/systemd/system/docker.service
Find the line starting with
ExecStart and replace it with the following:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
3. Reload the Docker daemon:
sudo systemctl daemon-reload
4. Restart the Docker service:
sudo service docker restart
5. Test the connection with the curl command:
The command outputs the Docker version details.
Create Docker Image
- All the build dependencies, such as Git or Java.
- Jenkins login credentials.
sshdservice mapped to port 22 for SSH-based communication.
Install Docker Plugin
Jenkins has a Docker plugin that enables communication with Docker hosts. To install the plugin in Jenkins, do the following:
1. Select Manage Jenkins in the menu on the left side of the Jenkins dashboard.
2. Click Manage Plugins in the Manage Jenkins window.
3. Select the Available tab in the Plugin Manager window.
4. Type Docker in the search field, and select the box next to the Docker plugin that appears in the search results.
5. Click the Download now and install after restart button.
6. When all the necessary plugin components download, select the box at the bottom of the screen to restart Jenkins.
Jenkins restarts automatically when the installation completes.
Configure Docker Build Agent in Jenkins
Configure the Docker Build Agent to perform jobs in the Manage Jenkins window of the Jenkins dashboard.
1. Select the Manage Nodes and Clouds item in the System Configuration section.
2. Click Configure Clouds in the menu on the left side.
3. Expand the Add a new cloud list and select Docker.
4. Provide the name and URI for the Docker host in the relevant fields. Enable the host by selecting the box.
5. Select the Expose DOCKER_HOST box.
6. Click the Docker Agent templates button to open additional configuration options.
7. Provide the label for identifying the host, and enable the agent by selecting the Enabled option.
8. Name the Docker template and provide the path to the Docker image.
9. Specify the home folder for the Jenkins user you created.
10. Choose Connect with SSH from the list in the Connect method section.
Additional SSH configuration options appear.
11. Select Use configured SSH credentials in the SSH key section. Provide the credentials you set up for the image in the field that appears below.
12. Select Non verifying Verification Strategy in the Host Key Verification Strategy section.
13. Click Save when you finish configuring the host.
The Docker build agent setup is complete.
Test Docker Containers in Jenkins
Once the Docker host is fully configured in Jenkins, test it by creating a Freestyle project.
1. Select New Item from the menu on the left side of the main dashboard.
2. Name the job, select the Freestyle project option and click OK.
3. Select Restrict where this project can be run option and type the Docker host label in the field below.
4. Add a simple build step. For example, use the echo command to display a message in the shell. Click Save when you finish configuring the job.
Jenkins creates the job and displays the project window.
5. Select Build Now in the menu on the left side of the project window.
The job initiates. Track the job's progress in the Build History section on the left side.
6. When the job completes, click the item in the Build History section. The Build window appears.
7. Select Console Output in the menu on the left side.
8. Check if the console output contains the command you scheduled for execution.
The console additionally prints a SUCCESS message, indicating the build worked.
The article showed you a simple procedure for setting up Docker containers as build agents in Jenkins.
If you are new to Jenkins, read the comprehensive Jenkins tutorial for beginners to learn about configuring the tool, managing plugins, and more.