DevOps(Day-27) : Jenkins Declarative Pipeline with Docker

I like to explore the technology in DevOps area where I write blog about my learning each day on the tools that is mostly used in Industries for DevOps practices. You can go through my blogs and reach me out in LinkedIn for any suggestions.
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 bandank/app:latest'
}
}
}
Task-1 : Using 'sh' syntax
Navigate to Jenkins and click on new item.

Create a pipeline project.

Configure the pipeline by writing the groovy script for the node app.
pipeline { agent any stages { stage('Code Fetch') { steps { git 'https://github.com/Bandank/nodejs.git/' } } stage('Build'){ steps { sh 'sudo docker build . -t bandank/nodejs-app:latest' } } stage('Run'){ steps { sh 'sudo docker run -d -p 8000:8000 bandank/nodejs-app:latest' } } } }
Save and click on Build now.

Check the output of the build.

Now, you can use the URL to check if the application is live.

Task-2 : Using 'docker' groovy syntax
Follow all the same steps to create a project as above.
In the configuration write the groovy script to integrate docker with Jenkins.

pipeline { agent any stages { stage('Build') { agent { docker { image 'bandank/nodejs-app:latest' reuseNode true } } stage{ echo "built" sh 'nodejs --version' } } stage('Run') { steps { sh 'sudo docker run -d --name nodejsapp bandank/nodejs-app:latest' } } } }

Thanks for reading my article. Have a nice day.
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/bandan-kumar-sahoo-131412203


