Use your Docker Build and Run Knowledge

Use your Docker Build and Run Knowledge

ocker 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-01

  • Create a docker-integrated Jenkins declarative pipelineStep 1: Create a new Jenkins Pipeline:

  • Click on the "New Item" button on the left-hand side of the screen to create a new Jenkins pipeline.
  • In the "Enter an item name" field, type in a name for your new pipeline and select "Pipeline" as the project type.


  • Click on the "OK" button to continue.
  • On the next page, scroll down to the "Pipeline" section and select "Pipeline script" as the Definition.


  • This pipeline script defines two stages: Build and Run. In the Build stage, we use the docker build command to build a Docker image with the name my-app.
  • In the Run stage, we use the docker run command to run the Docker container with the image we just built and map the container's port 8001 to the host's port 8001.
  • Click on the "Save" button to create the pipeline.

Step 2: Run the Jenkins Pipeline:

  1. To run the pipeline, click on the pipeline name on the Jenkins home page.
  2. Click on the "Build Now" button to start the pipeline.
  3. Jenkins will start executing the pipeline stages, and you can see the progress in the pipeline view.
  4. Once the pipeline has completed successfully, you can access the running application by opening a web browser and navigating to https://localhost:8001/.
  5. To stop the Docker container, run the following command in a terminal window:


  • docker stop $(docker ps -aq --filter ancestor=my-docker-image)

This command stops all running containers that were created from the my-docker-image Docker image.

Conclusion:

In this tutorial, we demonstrated how to create a Docker-Integrated Jenkins declarative pipeline to build and run a sample Python application. We used a Dockerfile to define the application's environment and integrated Docker commands into the Jenkins pipeline script to build and run the Docker container. With this integration, we can significantly improve the performance and scalability of our CI/CD pipeline


}

要查看或添加评论,请登录

Nikita Parmar的更多文章

社区洞察

其他会员也浏览了