How to Use Jenkins with Docker: A Step-by-Step Guide
Jenkins is one of the most popular open-source automation tools for Continuous Integration and Continuous Deployment (CI/CD) pipelines. Combined with Docker, Jenkins can build, test, and deploy applications in a highly scalable and efficient environment. In this article, we will guide you on how to use Jenkins with Docker and set up an example pipeline.
Prerequisites:
Basic knowledge of Jenkins and Docker.
Docker and Jenkins installed on your machine (or you can use Dockerized Jenkins).
A simple project (e.g., a Java or Node.js app) to build using Jenkins.
Step 1: Install Docker and Jenkins
You will need to install both Docker and Jenkins if you haven't done so already.
Install Docker: For Linux, Windows, or macOS, follow Docker’s official installation guide: Docker Installation.
Install Jenkins: For Linux, Windows, or macOS, follow Jenkins' official installation guide: Jenkins Installation.
Step 2: Running Jenkins with Docker
Option 1: Running Jenkins as a Docker Container
You can run Jenkins inside a Docker container to avoid manual installation. This setup is easier to maintain and more portable.
Pull the Jenkins Docker image: Open your terminal and run the following command to pull the official Jenkins Docker image:
Run Jenkins Container: Use this command to run the Jenkins container:
This will expose Jenkins on and bind it to port 50000 for agent connections.
The volume ensures your Jenkins data is persisted.
Access Jenkins: Open your browser and navigate to . You'll need the initial admin password, which can be found by running:
Option 2: Using Docker Inside Jenkins
To use Docker within Jenkins, we need to ensure that Jenkins has access to the Docker daemon.
Install Docker on the Jenkins Host: Install Docker on the same machine that is running Jenkins.
Add Jenkins to the Docker Group: Run the following command to allow Jenkins to use Docker without needing root privileges:
Restart Jenkins: Restart Jenkins so that the group change takes effect.
Step 3: Create a Jenkins Pipeline Using Docker
Example: Building a Dockerized Application
Let’s assume we have a simple Node.js application that we want Jenkins to build and package as a Docker image.
Create a Jenkins Pipeline Job:
Go to Jenkins Dashboard -> New Item -> Pipeline.
Name the job (e.g., "Dockerized Node.js Build").
Select the Pipeline option and click OK.
Set Up a Git Repository:
In your pipeline script, make sure your project is hosted on GitHub or any other Git-based repository. In our example, we’ll clone a simple Node.js application from a GitHub repository.
Write the Jenkinsfile: Jenkins will automate the build and push of the Docker image to Docker Hub (or any private Docker registry). Here is an example for a Node.js app:
Docker Image: In the pipeline, we use the official Docker image as our build environment.
Credentials: Ensure you set up your Docker Hub credentials in Jenkins under "Manage Credentials," and give it an ID ( in this example).
Docker Build and Push: The script builds the Docker image and pushes it to Docker Hub.
Run the Pipeline:
Go to the Jenkins job you created, and click Build Now.
Jenkins will pull the repository, run , and package the application into a Docker image. The image will then be pushed to your Docker Hub account.
Step 4: Verify the Build
Once the build process is completed, check your Docker Hub repository to ensure the new image has been pushed. You can now pull and run the image on any Docker host:
Jenkins Login screen
Conclusion
Integrating Jenkins with Docker enables you to streamline your CI/CD pipelines by automating the build and deployment of your applications. By running Jenkins itself inside Docker, you benefit from an easily maintainable and portable environment. Additionally, using Docker within Jenkins pipelines allows for isolated and reproducible builds, ensuring consistency across different environments.
This simple example demonstrates how to use Jenkins and Docker together to build, test, and deploy a Node.js application. You can further expand this pipeline to fit your specific requirements, such as adding more stages for testing, deploying to production, or integrating with other tools.
#Jenkins
#Docker
#CICD
#DevOps
#ContinuousIntegration
#ContinuousDeployment
#Automation
#Pipeline
#DockerJenkins
#SoftwareDevelopment
#BuildAutomation
#DockerContainer
#DevOpsTools
#JenkinsPipeline
#DockerizedApps
#TechGuide
#Nodejs
#Containerization
#CloudComputing
#ITInfrastructure