#90daysofdevops #27:Dockerizing Your Jenkins Declarative Pipeline
Day 26 was all about a Declarative pipeline, now its time to level up things, let’s integrate Docker and your Jenkins declarative pipeline.
What is 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.
Use your Docker Build and Run Knowledge
docker build — you can use sh 'docker build . -t <tag>'
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>'
in your pipeline stage block to build the container.
How will the stages look:
stages {
stage('Build') {
steps {
sh 'docker build -t trainwithshubham/django-app:latest'
}
}
}
Here is today’s Tasks:
01.Create a docker-integrated Jenkins declarative pipeline
- Open jenkins.
2. Click on “New Item”
3. Provide desired name and select “Pipeline” and click on “Ok” button.
4. Start typing the pipeline groovy script for ract-django app.
- Use
sh
inside the stage block
pipeline {
agent any
stages{
stage('code'){
steps{
git url: 'https://github.com/devkrgoutam/django_jenkins.git', branch: 'master'
}
}
stage('Build'){
steps{
sh 'docker build . -t react-django-docker-img:latest'
}
}
stage('Test'){
steps{
echo "Testing"
}
}
stage('Deploy'){
steps{
sh "docker run -d --name react-django-docker-jenkins -p 8000:8000 react-django-docker-img:latest"
}
}
}
}
5. Click on Save.
6. Click on “Build Now”.
- Build pipeline run successfully done.
7. Try Again to Build pipeline
- You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2.
Error will pop up when we run same job twice.
For solve this error do the Task 02
02. Create a docker-integrated Jenkins declarative pipeline using the docker
groovy syntax inside the stage block.
pipeline {
agent any
stages {
stage ('Build'){
agent{
docker{
image 'react-django-docker-img:latest'
reuseNode true
}
}
steps {
echo "Building code"
sh 'python --version'
}
}
}
}
Click on Save and then click on Build Now.
That’s all about todays task of DevOps journey.
Thankyou for reading.
If you liked this article then click on clap do follow for more interesting and helpful stories.