Table of contents
Let's integrate Docker and your Jenkins declarative pipeline.
Use your Docker Build and Run
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 stage look,
stages {
stage('Build') {
steps {
sh 'docker build -t ojasjawale/django-app:latest'
}
}
}
Task-1
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.
- Create a new pipeline project and named it.
- Sample script for testing,
- Now in the
"Project Configuration"
section, we have to define a pipeline script in the definition.
pipeline{
agent any
stages{
stage("Source Code"){
steps{
git url: "https://github.com/ojasjawale/django-notes-app.git", branch: "main"
}
}
stage("Build Code"){
steps{
echo "Image build started......"
sh 'docker build -t notes-app:v1 .'
echo "Image build Successfully......"
}
}
stage("Testing"){
steps{
echo "Testing started......."
echo "Testing done......."
}
}
stage("Deploy"){
steps{
echo "Deployment started......"
sh 'docker run -d --name notes-app -p 8000:8000 notes-app:v1'
echo "Deployment successfully completed........"
}
}
}
}
- Click on Save and Build the application.
- Our application can be viewed from a browser.
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.
Run the pipeline again by clicking on the “Build Now” button.
Task-2
Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.You won't face errors, you can Follow this documentation
Complete your previous projects using this Declarative pipeline approach.
To solve this issue we can use a docker image to build the application and for that, we have to install Docker Pipeline & Docker in Jenkins.
- Now we have to add a docker image in the Build stage.
pipeline {
agent any
stages {
stage("Source Code") {
steps {
git url: "https://github.com/ojasjawale/django-notes-app.git", branch: "main"
}
}
stage("Build Code") {
steps {
echo "Image build started......"
script {
docker.build('notes-app:v1')
}
echo "Image build Successfully......"
}
}
stage("Testing") {
steps {
echo "Testing started......."
echo "Testing done......."
}
}
stage("Deploy") {
steps {
sh 'docker run -d --name notes-app -p 8000:8000 notes-app:v1'
}
}
}
}
- Click on Save and then click on Build Now.
Connect With Me
Thank you for reading. I hope you were able to understand and learn something new from my blog.
Happy Learning!