Day27/Jenkins Declarative Pipeline with Docker
In previous article was all about a Declarative pipeline, now it is time to level up things, so let's integrate Docker and your Jenkins declarative pipeline.
What is the docker-integrated Jenkins declarative pipeline?
Docker-integrated Jenkins declarative pipeline is a way of defining and executing continuous integration and continuous delivery (CI/CD) pipelines using the Jenkins automation server and the Docker containerization platform.
In this approach, the pipeline is defined in a declarative manner using a Jenkinsfile that specifies the stages, steps, and parameters of the pipeline. The pipeline can also make use of Docker containers to encapsulate the build, test, and deployment environment, ensuring consistency and reproducibility across different stages and environments.
Overall, the Docker-integrated Jenkins declarative pipeline provides a powerful and flexible approach to building, testing, and deploying software in a containerized environment, with the added benefits of automation, repeatability, and scalability.
To use Docker in Jenkins Declarative Pipeline, you need to have Docker installed on the Jenkins server or agents where your pipeline will run. Here's an example of a Jenkins Declarative Pipeline that utilizes Docker:
pipeline {
agent {
docker {
image 'your-docker-image:tag'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
stages {
stage('Build') {
steps {
sh 'docker build -t my-app .'
}
}
stage('Test') {
steps {
sh 'docker run my-app npm test'
}
}
stage('Deploy') {
steps {
sh 'docker push my-app:latest'
// Add deployment steps here
}
}
}
}
In this example,
⚙ agent
the section specifies that the pipeline should run inside a Docker container.
⚙ image
parameter specifies the Docker image to use for the container.
⚙args
parameter is optional and used to pass additional arguments to the Docker container. In this case, it mounts the Docker socket (/var/run/docker.sock
) inside the container, allowing the pipeline to interact with the Docker daemon on the host.
⚙ stages
section defines the different stages of your pipeline. In this example, there are three stages: Build
, Test
, and Deploy
. Each stage contains one or more steps
that define the actions to be performed.
⚙ Build
stage, the pipeline runs the docker build
command to build the Docker image for your application.
⚙ Test
stage, the pipeline runs the docker run
command to execute the tests within the Docker container.
⚙ Deploy
stage, you can add the necessary steps to deploy your application, such as pushing the Docker image to a container registry or deploying it to a production environment.
Docker In Jenkins pipelines is crucial to ensure the integrity and safety of your CI/CD process. Here are some best practices to consider:
Secure Jenkins Server and docker registry
Isolate Jenkins Agents
Image Scanning, Tagging and Versioning
Secrets Management and continuous management
Use your Docker Build and Run Knowledge:
docker build — you can use sh 'docker build . -t <tag>'
it in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.
docker run: you can use sh 'docker run -d <image>'
it in your pipeline stage block to build the container.
stages {
stage('Build') {
steps {
sh 'docker build -t trainwithshubham/django-app:latest'
}
}
}
🔹Task-01:
Create a Docker-Integrated Jenkins Declarative Pipeline🐳🖇
Go to your Jenkins dashboard, click on New item give a name, and select Pipeline.
Scroll down to the "Pipeline" section and choose "Pipeline script" from the dropdown.
Now, let's use the provided syntax inside the
script
block of the pipeline:pipeline { agent any stages { stage('Code Clone') { steps { git url: 'https://github.com/chandanahdam/node-todo-cicd.git', branch: 'master' } } stage('Build') { steps { sh 'docker build -t node-todo-cicd:latest .' } } stage('Testing') { steps { echo 'Testing' // You can add actual testing steps here } } stage('Deploy') { steps { sh 'docker run -d -p 8000:8000 node-todo-cicd:latest' } } } }
Save it and click on Build now to check if it's working, as you can see we have successfully built the structure.
The pipeline will execute the build and run stages, creating a Docker container with your application.
Successfully built and deployed using Jenkins declarative pipeline project.
🔹TASK2:
Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.🐳🐳
when rebuilding the project, a common issue emerges due to the previously built container still occupying the same port, leading to conflicts, We can solve the error with the help of docker-compose, by modifying the script using the docker-compose down and up commands.
stage("code Deploy"){ steps { sh 'docker-compose down' sh 'docker-compose up' } }
Click on the "Save" button to save your pipeline configuration.
Run the pipeline job by clicking on the "Build Now" button.
Below the screenshot, our container is running also our Application running Properly.
Conclusion
In this article, you have seen how to leverage Jenkins’s deployment phase and Pipeline features to help automate your deployments by constructing a Jenkinsfile.
we would always recommend you prefer creating Jenkinsfile with Declarative pipeline syntax. Jenkinsfile, since part of the application’s source code, will provide more control over CI/CD steps to developers.
That’s the best way to make the most of Jenkins CI/CD and all the features it has to offer!
Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends.
Happy learning and testing🎉🎉 !!!!
Subscribe to my newsletter
Read articles from Siri Chandana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by