Build and Push Docker Image to GCR using CI/CD Approach

Filed Under: Random
Build And Push Docker Image Using Cicd Approach

Hello, readers! This article talks about Building and Pushing Docker Image to GCR using the Azure CI/CD approach with practical implementation.

So, let us begin!!


Dockerfile and Docker Images – Quick Recap

Today, we have moved ahead with the provisioning of applications in an easier way with containers and functions.

In the world of containerization, Dockerfile has played an important role to provision containers altogether. A Dockerfile is a file that consists of all the configurations and libraries needed to execute the application in a running state. With this, we can provide all the requirements needed to run an application in a container.

A Docker Image is a blueprint of the Dockerfile. That is, it is a blueprint of all the set of libraries and instructions within the Dockerfile that enables us to build the containers over the cloud.

This Docker Image can be put into the deployment file to run containers over Kubernetes infrastructure.


The CI/CD Approach

The deployment of applications through containers can be automated through Azure Pipelines easily. In order to deploy containers, building and pushing images is the main block to move ahead.

A CI/CD pipeline is a Continuous Integration and Delivery Pipeline where the application can be built through stages and then made ready for delivery in the development or production servers.


Advantages of CI/CD Approach-

  • Automates the service delivery process
  • Reduces manual errors during the deployment
  • Enables fast pace iterations of the product deployment
  • We can easily monitor the process of service delivery

Section under a CI/CD Pipeline

  1. Source Stage: In this stage, the pipeline is triggered from the source code repository. It triggers the automated workflows, manually initiated tasks, etc.
  2. Build Stage: This stage enables us to encapsulate the dependencies along with the source code. With this, it builds an instance of software that provides end to end user understanding of the run.
  3. Test Stage: In this stage, the execution of the validation towards the application takes place.
  4. Deploy Stage: This stage triggers once the build stage succeeds. The deploy stage deploys the application to the live environment.

In the upcoming section, we will be making use of the CI/CD pipeline to build and push image to Google Container Registry.


Practical Implementation : Build and Push Docker Image to GCR using CI/CD Approach

In order to build and push image through the CI/CD approach, we need to follow the below steps-

  • Build a Dockerfile and push it to a repository.
  • Create a Service Connection to connect to the Container Registry.
  • Create a Pipeline (YAML) code
  • Run the build pipeline

Have a look at the below Dockerfile-

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/backup
COPY . .
ADD start.sh /usr/src/backup

RUN chmod +x start.sh

ENTRYPOINT ["bash","start.sh"] 

This Dockerfile contains instructions to include curl library and a bash script to build an image out of it.

In order to create a service connection, we will need the credentials of the service account that has permissions on the project.

We will need to include the following details-

  • The Docker Registry: This should be the URL of the container registry
  • Docker Password: The JSON credentials of the service account.
  • Service connection name
Image 7
Service Connection

Once the service connection is built in the Azure DevOps portal, we then need to use the below YAML to build the pipeline-

stages:

- stage: Build

  displayName: Build image

  jobs: 

  - job: Build

    displayName: Build

    pool:

      vmImage: 'ubuntu-latest'

    steps:   

    - task: Docker@2

      displayName: Build an image

      inputs:

        containerRegistry: 'demo_connection'

        repository: 'demo/demoapp1234'

        command: buildAndPush

        dockerfile: '$(System.DefaultWorkingDirectory)/Dockerfile'

        tags: |

          demo
  • The pipeline should be synced with the repository that contains a Dockerfile
  • command: buildAndPush, this is the command that instructs the pipeline to execute the build and push of the Dockerfile
  • repository: This is the folder within the registry where we want our image to be stored.
  • containerRegistry: We need to enter the service connection name for this key.

With the above YAML, we can build and push images to the designated container registry.


Conclusion

By this, we have approached the end of this topic. Feel free to comment below, in case of any questions.

For more such posts related to Kubernetes and Azure Pipelines, Stay tuned with us.

Till then, Happy Learning!! 馃檪

close
Generic selectors
Exact matches only
Search in title
Search in content