A Jenkins Declarative Pipeline Project with Docker[Day-26 Task]

A Jenkins Declarative Pipeline Project with Docker[Day-26 Task]

Table of contents

1. 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

[Use your Docker Build and Run Knowledge]

Prerequisites :

Jenkins, Docker, and Docker-Compose must be installed in your system

Start Your already configured Jenkins instance and Log in to your Jenkins dashboard.

Step 1:

As per the task we have to create a Jenkins declarative pipeline with the help of docker. So, click on a New item/job from your Jenkins dashboard and give an item name then select pipeline and save.

Step 2:

Give a proper description of your project and add your GitHub project URL.

This is the project code.

Step 3:

Now scroll down to the same page. This is the important part, write the pipeline script properly.

Add your git URL like this, build the docker image, and run the container.

Click on save.

Step 4:

Now Build the job and check the output.

Step 5:

The pipeline is running successfully[If you do any mistake it will throw an error, write the syntax properly ]

Step 6:

Verify whether we can browse the application or not.

Step 7:

Now If you try to run the pipeline again it won't run successfully because the previous container is already created unless you delete that container it won't create a new container, which will throw an error. This is not a good practice to delete a container manually every time before creating a new one as this is an automated process, so this task also should be done automatically. Follow the Task 2

2. Task 2

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

  • You won't face errors

  • Complete your previous projects using this Declarative pipeline approach.

Step 1:

You can solve the error with the help of docker-compose.

Modify the previous script with the docker-compose command, now don't need to write the docker run command as docker-compose will up the containers, and the next time when you will run the job again it will first down the containers and then will create the next one.

Step 2:

Build the job and check the output.

Step 3:

The pipeline is also running successfully.

Step 4:

Add the port in the inbound rule and Check the application.

Thank you for reading. I hope you were able to understand and learn something new from my blog.

Happy Learning!