Friday, November 15, 2024
Google search engine
HomeGuest BlogsHow to Configure Docker in Jenkins

How to Configure Docker in Jenkins

Introduction

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.

How to configure Docker in Jenkins.How to configure Docker in Jenkins.

Prerequisites

Set up Docker Host

Jenkins uses a REST API for communicating with Docker. The following configuration steps on the Docker host ensure that the Jenkins controller can connect properly.

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
Editing the docker service file to enable REST API communication.Editing the docker service file to enable REST API communication.

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:

curl http://localhost:4243/version
The output of the curl command, showing the Docker version details.The output of the curl command, showing the Docker version details.

The command outputs the Docker version details.

Create Docker Image

Docker images require configuration to work as build agents in Jenkins. When you create Docker images for this purpose, make sure they contain:

  • All the build dependencies, such as Git or Java.
  • Jenkins login credentials.
  • The sshd service 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.

Navigating to Manage Jenkins window in Jenkins.Navigating to Manage Jenkins window in Jenkins.

2. Click Manage Plugins in the Manage Jenkins window.

Navigating to Manage Plugins window in Jenkins.Navigating to Manage Plugins window in Jenkins.

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.

Selecting the Docker plugin in the Plugin Manager in Jenkins.Selecting the Docker plugin in the Plugin Manager in Jenkins.

6. When all the necessary plugin components download, select the box at the bottom of the screen to restart Jenkins.

Restarting Jenkins after the plugins are downloaded. Restarting Jenkins after the plugins are downloaded.

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.

Navigating to the Manage Nodes and Clouds section.Navigating to the Manage Nodes and Clouds section.

2. Click Configure Clouds in the menu on the left side.

Navigating to the Configure Clouds window.Navigating to the Configure Clouds window.

3. Expand the Add a new cloud list and select Docker.

Choosing Docker from the new cloud list.Choosing Docker from the new cloud list.

4. Provide the name and URI for the Docker host in the relevant fields. Enable the host by selecting the box.

Providing the name and URI for the Docker host.Providing the name and URI for the Docker host.

5. Select the Expose DOCKER_HOST box.

6. Click the Docker Agent templates button to open additional configuration options.

Exposing the Docker host and expanding the Docker Agent templates section.Exposing the Docker host and expanding the Docker Agent templates section.

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.

Configuring the Docker Agent template.Configuring the Docker Agent template.

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 configuration for the Docker Agent template.Additional configuration for the Docker Agent template.

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.

Configuring the SSH credentials for the Docker Agent template.Configuring the SSH credentials for the Docker Agent template.

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.

Navigating to the New Item section in Jenkins.Navigating to the New Item section in Jenkins.

2. Name the job, select the Freestyle project option and click OK.

Creating a Freestyle project in Jenkins.Creating a Freestyle project in Jenkins.

3. Select Restrict where this project can be run option and type the Docker host label in the field below.

Restricting where the project can be run in Jenkins.Restricting where the project can be run in Jenkins.

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.

Adding a build step in a freestyle job in Jenkins.Adding a build step in a freestyle job in Jenkins.

Jenkins creates the job and displays the project window.

5. Select Build Now in the menu on the left side of the project window.

Starting the build in Jenkins.Starting the build in Jenkins.

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.

Navigating to console output in Jenkins.Navigating to console output in Jenkins.

8. Check if the console output contains the command you scheduled for execution.

Checking the console output to confirm the successful Docker host configuration.Checking the console output to confirm the successful Docker host configuration.

The console additionally prints a SUCCESS message, indicating the build worked.

Conclusion

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.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments