In this blog, I will integrate Docker with Jenkins and deploy an application.
A quick recap of a few things:
Learn how to use docker build and docker run.
An example of how to use docker-compose up & down.
An example of how to write a declarative Jenkins pipeline.
Quick Tip: If you are still learning on writing Jenkins declarative pipelines, use the Snippet Generator. That becomes a great support when you are learning, to know the syntax.
Here's the link to it: Snippet Generator!
Make great use of resources like these.
Let's complete today's tasks.
Tasks
Task 1: Create a docker-integrated Jenkins declarative pipeline.
Repo used: https://github.com/thesnehasuresh/node-todo-cicd.git
1) Connect to the Jenkins server and create a Pipeline Job named "todo-app".
2) Go to Dashboard > Configure > Pipeline > Select Pipeline Script > And run the commands below:
pipeline{
agent any
stages{
stage('Clone the repo'){
steps{
echo 'Cloning the repository:'
git 'https://github.com/thesnehasuresh/node-todo-cicd.git'
}
}
stage('Build'){
steps{
echo 'Building the ToDo application on Docker'
sh 'docker build . -t todo-app'
}
}
stage('Deploy'){
steps{
echo 'Deploying the application on Docker'
sh 'docker run -p 8000:8000 -d todo-app'
}
}
}
}
3) Click on Save and Build the job by clicking on Build Now on the Project dashboard.
4) I can see that the build was successful, below is the image of stage view.
This is the console output:
5) Let's access the application, to verify that it is deployed.
The app is up and running!
6) Let me try to build the same job again. The job has failed as the containers are already and exist for the port and the same is shown in the stage view.
The same can be confirmed from the console output:
For this, the existing containers need to be deleted manually from the host system. This issue can be solved by including the docker groovy syntax inside stage block.
That's the next task.
Task 2: Create a docker-integrated Jenkins declarative pipeline using the docker
groovy syntax inside the stage block.
To include docker in the stage block, the docker pipeline and docker plugin need to be installed.
1) Go to Dashboard > Manage Jenkins > Manage Plugins > Available Plugins > Select the required plugins > Click on Install without restart.
2) In the Download Progress page, select Restart after Installation.
Wait for the Jenkins server to restart once the installation is complete.
3) Now let us start writing the Declarative pipeline command. Create a New Pipeline Job named "sample-app" and go to the pipeline script in configuration.
Docker Hub image used:
pipeline {
agent any
stages {
stage('Clone the Repo:') {
steps {
echo 'Cloning the repository:'
git 'https://github.com/thesnehasuresh/node-todo-cicd.git'
}
}
stage('Build') {
agent {
docker {
image 'snehaks/sample_nodejs:latest'
reuseNode true
}
}
steps {
echo "Building app using Docker Image"
}
}
stage('Deploy the Application') {
steps {
sh "docker-compose down"
sh "docker-compose up -d"
}
}
}
}
Save the Code and build the job.
Let's inspect the new block in Stage.
The stage
Build
in the Jenkinsfile defines a stage in the pipeline that will build the application.The agent section of the stage specifies that the stage will be executed in a Docker container.
The image section of the agent specifies that the Docker image to use is
snehaks/sample_nodejs:latest
.The reuseNode section of the agent specifies that the Docker container should be reused if possible.
4) The build is successful. Same can be verified in Stage view and Console output as well!
5) We can deploy the same application many times, without any deletion of containers. That's what even I got when I run the pipeline again:
That's it for today's blog!
In this blog, I have discussed Jenkins Declarative Pipelines integration with Docker. If you have any questions or would like to share your experiences, feel free to leave a comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.
To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.
#Day27 #90daysofdevops