Day 27: Utilizing Docker in Jenkins Declarative Pipelines

Day 27: Utilizing Docker in Jenkins Declarative Pipelines

ยท

3 min read

Day 26 was all about a Declarative pipeline, now it is time to level up things, let's integrate Docker and your Jenkins declarative pipeline

Use your Docker 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.

This is how will the stage look

stages {
        stage('Build') {
            steps {
                sh 'docker build -t <docker_hub_name>/django-app:latest'
            }
        }
    }

Task 01

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

Steps:

  • First, set up your Jenkins in your system.

  • Go to your Jenkins server, create a new job, write the job name and select pipeline as you did before.

  • write the description for your job, and select GitHub if the project is on GitHub.

  • Then go pipeline, and write the pipeline with groovy syntax as told in the day 26 blog.

  • Before you use docker in the pipeline, make sure that docker is installed on your system.

  • We're using the same node app from previous tasks. Lets start using the pipeline with the docker.

    This is how the pipeline will look like with docker. Use sh before the docker command. It's a groovy syntax to write commands in the pipeline.

  • Click on Save and Build Now to run the pipeline.

    This means your pipeline is correct and your application deploys successfully with docker.

  • If you click again on Build Now, it might give some error. So let's try.

    See the failed pipeline, it's because of running a job twice even though the container already existed.

  • let's solve the error in Task 02.


Task 02

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

You won't face errors, if you use docker-compose instead of docker.

steps:

  • First, kill the existing container by using the docker kill command.

  • Go to the Jenkins server and click on configure in your node-app pipeline.

  • Go to the pipeline section and write the docker-compose command.

    Now the pipeline will look like this.

  • Click on Save and Build Now to check your pipeline.

    The pipeline is built successfully and your application is up and running.

  • If you click on Build Now again, there will be no error because we are using the docker-compose. Let's try to build again.

  • Congratulations, you learned a lot today. Keep practising the hard things.


<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>

ย