Day 26 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline
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'
}
}
}
Task-01: Docker-Integrated Jenkins Declarative Pipeline
Create a docker-integrated Jenkins declarative pipeline
Use the above-given syntax using
sh
inside the stage blockYou will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2
Click on "New Item" to create a new Jenkins job.
Enter Job Details:
Provide a name for the job
Choose "Pipeline" as the job type.
- Add a description
- Set your stages, steps, and parameters in the pipeline script section of the configuration.
pipeline {
agent any
stages{
stage ('Code Clone') {
steps {
git url : 'https://github.com/Sompandey01/django-todo-cicd.git' , branch : 'develop'
}
}
stage ('Build') {
steps {
sh 'docker build . -t django-todo-cicd:latest'
}
}
stage ('Testing') {
steps {
echo 'testing'
}
}
stage ('Deploy') {
steps {
sh 'docker run -d -p 8000:8000 django-todo-cicd:latest'
}
}
}
}
"Save" and click on "Build Now" to start the pipeline.
-
Check the "Console Output"
- Check whether the application is working on port 8000 or not.
You will face errors in case of running a job twice, as the docker container will be already created.
Task-02: Enhancing Docker-Integrated Jenkins Declarative Pipeline
Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.Complete your previous projects using this Declarative pipeline approach.
Simply, modify the script using docker-compose down
and docker-compose up
command
Click on "Save" and then click on "Build Now".
When the build is successful check whether the application is working on port 8000 or not.
"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.
Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/ for the latest updates and discussions.