Day 27 Task: Jenkins Declarative Pipeline with Docker
Docker is a platform for developing, shipping, and running applications in containers
Dockerfile: Start by creating a Dockerfile, which is a text file that contains instructions for building a Docker image. The Dockerfile specifies a base image and a series of commands to set up your application and its dependencies.
Build the Image: Navigate to the directory containing the Dockerfile and run the docker build
command to create a Docker image.
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.
๐ถ Task-01: Create a docker-integrated Jenkins declarative pipeline.
Creating a Docker-Integrated Jenkins Declarative Pipeline
1. Set Up Jenkins:- Install and configure Jenkins on your server or cloud platform.
2. Install Docker:
- Ensure that Docker is installed on the same machine as Jenkins.
3. Create a New Item:
Go to your Jenkins dashboard and click on "New Item."
Enter a name for your project, select "Pipeline," and click "OK."
4. Configure Pipeline:
In the project configuration, scroll down to the "Pipeline" section.
Select "Pipeline script" from the "Definition" dropdown.
Use the above-given syntax using
sh
inside the stage blockpipeline { agent any stages{ stage ('Code Clone') { steps { git url : 'https://github.com/niluflip/django-todo-cicd.git', branch : 'develop' } } stage ('Build') { steps { sh 'docker build . -t django-todo-cicd-jenkins:latest' } } stage ('Testing') { steps { echo 'testing' } } stage ('Deploy') { steps { sh 'docker run -d -p 8000:8000 django-todo-cicd-jenkins:latest' } } } }
Now Save and build to start the pipeline.
Console output.
-
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
๐ถ Task-02: Create a docker-integrated Jenkins declarative pipeline using the docker
groovy syntax inside the stage block.
pipeline {
agent any
stages{
stage ('Code Clone') {
steps {
git url : 'https://github.com/niluflip/django-todo-cicd.git', branch : 'develop'
}
}
stage ('Build') {
steps {
sh 'docker build . -t django-todo-cicd-jenkins:latest'
}
}
stage ('Testing') {
steps {
echo 'testing'
}
}
stage ('Deploy') {
steps {
sh 'docker-compose down'
sh 'docker-compose up -d --no deps'
}
}
}
}
Now save and Build Now start the pipeline.
-
- Console output.
- You won't face errors, you can Follow this documentation.
In conclusion, creating a Docker-integrated Jenkins Declarative Pipeline empowers you to seamlessly incorporate Docker into your CI/CD workflow. By combining the power of Jenkins and Docker, you can automate the building and running of Docker containers as part of your software deployment process. This approach streamlines the development lifecycle, allowing you to efficiently manage containerized applications with consistency and reliability. Docker-integrated pipelines offer a flexible and effective way to harness the benefits of containerization while leveraging the capabilities of Jenkins for automated and controlled deployments.