TABLE OF CONTENTS
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:-
COPY
stages {
stage('Build') {
steps {
sh 'docker build -t biswaraj/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.
COPY
pipeline { agent any stages { stage('Code Fetch') { steps { git 'https://github.com/Biswaraj/nodejs.git/' } } stage('Build'){ steps { sh 'sudo docker build . -t Biswaraj/nodejs-app:latest' } } stage('Run'){ steps { sh 'sudo docker run -d -p 8000:8000 biswaraj/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.
COPY
pipeline { agent any stages { stage('Build') { agent { docker { image 'biswaraj/nodejs-app:latest' reuseNode true } } stage{ echo "built" sh 'nodejs --version' } } stage('Run') { steps { sh 'sudo docker run -d --name nodejsapp biswaraj/nodejs-app:latest' } } } }
Thanks for reading my article. Have a nice day.
WRITTEN BY Biswaraj Sahoo --AWS Community Builder | DevOps Engineer | Docker | Linux | Jenkins | AWS | Git | Terraform | Docker | kubernetes
Empowering communities via open source and education. Connect with me over linktree: linktr.ee/biswaraj333