Thursday, May 6, 2021

Automate Docker builds using Jenkins Pipelines | Dockerize Python App | Upload Images into Azure Container Registry (ACR)

We will learn how to automate Docker builds using Jenkins. We will use Python based application. I have already created a repo with source code + Dockerfile. We will see how to create Docker image and upload into Azure Container Registry (ACR) successfully.



- Automating builds
- Automating Docker image builds
- Automating Docker image upload into ACR
- Automating Docker container provisioning
 
Watch here for YouTube channel:
 

Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance. Click here to for integrating Docker and Jenkins
3. Docker and Docker pipelines plug-in are installed 
4. Create credentials entry for Jenkins for connecting to ACR
5. Repo created in ACR, Click here to know how to do that.
6. port 8096 is opened up in firewall rules. 

Step # 1 - Create a pipeline in Jenkins
Name as myACRDockerPipelineJob


Step # 2 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your a should be updated and repo should be updated.
pipeline {
     agent any
     
        environment {
        //once you create ACR in Azure cloud, use that here
        registryName = "myakacrregistry/my-python-app"
        //- update your credentials ID after creating credentials for connecting to ACR
        registryCredential = 'ACR'
        dockerImage = ''
        registryUrl = 'myakacrregistry.azurecr.io'
    }
    
    stages {

        stage ('checkout') {
            steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/akannan1087/myPythonDockerRepo']]])
            }
        }
       
        stage ('Build Docker image') {
            steps {
                
                script {
                    dockerImage = docker.build registryName
                }
            }
        }
       
    // Uploading Docker images into ACR
    stage('Upload Image to ACR') {
     steps{   
         script {
            docker.withRegistry( "http://${registryUrl}", registryCredential ) {
            dockerImage.push()
            }
        }
      }
    }

       // Stopping Docker containers for cleaner Docker run
     stage('stop previous containers') {
         steps {
            sh 'docker ps -f name=mypythonContainer -q | xargs --no-run-if-empty docker container stop'
            sh 'docker container ls -a -fname=mypythonContainer -q | xargs -r docker container rm'
         }
       }
      
    stage('Docker Run') {
     steps{
         script {
                sh 'docker run -d -p 8096:5000 --rm --name mypythonContainer ${registryUrl}/${registryName}'
            }
      }
    }
    }
 }
 
Step # 3 - Click on Build - Build the pipeline
Once you create the pipeline and changed values per your ACR details, click on Build now.

Steps # 4 - Check Docker images are uploaded into ACR
Login to ACR in Azure cloud, click on your registry name, Services, Repositories, now you should see the image got uploaded.



Steps # 5 - Access PythonApp in the browser which is running inside docker container
Once build is successful, go to browser and enter http://public_dns_name:8096
You should see page like below:



3 comments:

  1. I'm unable to execute docker run on windows machine using 'sh', can you help me how can run this on windows laptop

    ReplyDelete
  2. Automating Python application builds with Jenkins, Docker, and Azure Container Registry demonstrates how software development can be connected with modern DevOps practices. Building on these concepts, students can explore practical Python applications that solve real-world problems while developing their programming, debugging, and software design skills. Combining hands-on project development with containerization also provides useful exposure to the application delivery process.

    For final-year engineering students looking to apply Python concepts in academic projects, Python Projects For Final Year offers a relevant starting point for exploring project ideas and implementation approaches. Working on a well-defined project can help students connect programming fundamentals with practical problem-solving and prepare their applications for testing and deployment.

    ReplyDelete
  3. Students who want to expand their skills beyond standalone Python programs can also explore the Python Full Stack Developer Course. Full-stack development introduces the integration of frontend interfaces, backend services, and databases, while knowledge of Docker and Jenkins can help learners understand how these application components fit into a repeatable development and deployment workflow.

    ReplyDelete

How to Configure GitLab Runner with Docker Executor on Ubuntu 24.04 EC2

How to Configure GitLab Runner with Docker Executor on Ubuntu 24.04 EC2 In this tutorial, we will configure a self-hosted GitLab R...