Day 27 Task: Jenkins Declarative 
     Pipeline with DockerπŸ› οΈπŸ³

Day 27 Task: Jenkins Declarative Pipeline with DockerπŸ› οΈπŸ³

Β·

3 min read

let's integrate Docker and your Jenkins declarative pipeline

Jenkins Declarative Pipeline with Docker means using Jenkins (a tool for automating software development processes) along with Docker (a tool for running applications in containers) to manage and execute your project's tasks.

  1. Pipeline DefinitionπŸ”§: You create a plan for your project in Jenkins. This plan is called a "pipeline" and it specifies the sequence of tasks to be performed.

  2. Using Docker Containers🐳: Instead of running your tasks directly on the Jenkins server, you run them inside Docker containers. These containers provide a consistent and isolated environment for your tasks.

  3. Efficient Resource ManagementπŸ’‘: Docker containers allow you to efficiently manage resources, isolate dependencies, and ensure consistent behavior across different environments.

  4. Flexibility and PortabilityπŸš€: By using Docker, your pipeline becomes more flexible and portable. You can easily move your pipeline to different environments without worrying about dependencies or configuration issues.

  5. Improved ReproducibilityπŸ”„: Docker containers help in making your pipeline more reproducible. You can easily recreate the exact same environment for your tasks, ensuring consistent and reliable results.

Jenkins Declarative Pipeline with Docker offers a more efficient, flexible, and reproducible way of managing and executing your project tasks.πŸ› οΈπŸ³

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 trainwithshubham/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

    Build gets failed if we run pipeline second time because port will already allocated, so to keep code robust we need to use docker compose

    Note: Remove all Images and Containers before proceeding to next step

    COPY

        #To delete all containers including its volumes
        docker rm -vf $(docker ps -aq)
    
        # To delete all the images
        docker rmi -f $(docker images -aq)
    

Task-02

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

  • Complete your previous projects using this Declarative pipeline approach

πŸ™Œ Thank you for taking the time to explore this blog!πŸ“š I hope you found the information both helpful and insightful.✨

πŸš€ Enjoy your learning journey, and don't hesitate to reach out if you have any feedback. πŸ€“ Happy exploring!

Β