Table of contents
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.
Task 1:
Create a docker-integrated Jenkins declarative pipeline
Use the above-given syntax using sh inside the stage block
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
Create a new pipeline project and named it.
Now in the "Project Configuration" section, we have to define a pipeline script in the definition.
pipeline { agent any stages { stage('Clone Code') { steps { git url: 'https://github.com/Abhisek773/node-todo-cicd.git' , branch: 'master' } } stage('Build') { steps { sh 'docker build . -t node_todo_app:latest' } } stage('Test') { steps { echo "Testing" } } stage('Deploy') { steps { sh "docker run -p 8000:8000 -d node_todo_app:latest" } } } }
Click on Save and Build the application.
Once the build is completed we can check in the Console Output. We can see the pipeline is successful.
Now you check the multi-stage view by clicking on the “Full Stage View” on the project main page.
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.
We will see the error in the Full stage view.
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 Plugin in Jenkins.
Now we have to add a docker image in the Build stage.
pipeline { agent any stages { stage('Clone Code') { steps { git url: 'https://github.com/Abhisek773/node-todo-cicd.git' , branch: 'master' } } stage('Build') { agent { docker { image 'abhisek6/node_todo_app:latest' reuseNode true } } steps { echo "Building app using Docker Image" } } stage('Test') { steps { echo "Testing" } } stage('Deploy') { steps { sh "docker-compose down" sh "docker-compose up -d" } } } }
Click on Save and then click on Build Now.
Now we can run the job twice or a couple of times without killing any existing container.
Thank You,
I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.
Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.
~ Abhisek Moharana 😊