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 trainwithshubham/django-app:latest'
}
}
}
Task: 1
Create a docker-integrated Jenkins declarative pipeline
Step: 1
In Jenkins, Click on New Item, create a new pipeline job, and select Pipeline as the project type.
Step: 2
Give a proper description of your project and add your GitHub project URL
Step: 3
In the job configuration, scroll down to the Pipeline section, Choose Pipeline script from the Definition dropdown
pipeline{
agent any
stages{
stage('code clone'){
steps{
git url: 'https://github.com/sutish/node-todo-cicd.git'
echo 'code cloned'
}
}
stage('build and test'){
steps{
sh 'docker build . -t node-todo-cicd:latest'
echo 'Build and test successful'
}
}
stage('deploy'){
steps{
sh 'docker run -d -p 8000:8000 node-todo-cicd:latest'
}
}
}
}
Step: 4
Save your configuration & build the job
you will face errors in case of running a job twice, as the docker container will be already created.
Task: 2
Create a docker-integrated Jenkins declarative pipeline using the docker
groovy syntax inside the stage block.
Make some changes in the docker groovy syntax inside the stage block to resolve the port is already allocated issue.
pipeline{
agent any
stages{
stage('code clone'){
steps{
git url: 'https://github.com/sutish/node-todo-cicd.git'
echo 'code cloned'
}
}
stage('build and test'){
steps{
sh 'docker build . -t node-todo-cicd:latest'
echo 'Build and test successful'
}
}
stage('deploy'){
steps{
sh 'docker-compose down'
sh 'docker-compose up -d'
}
}
}
}
If you are facing any other errors, you can Follow this documentation
๐ Conclusion :
In this blog, we cover Jenkins Declarative Pipeline with Docker & troubleshoot port already allocated issue. In the next blog, we will cover some advanced topics.
Thank you for reading!
Contact me on Linkedin ๐ค
Check out my GitHub for more resources ๐