Creation and pushing the container in ACR (Azure Container Registery)

Creation and pushing the container in ACR (Azure Container Registery)

Installing Docker

  1. Download Docker Desktop:

    • Visit the Docker Desktop download page and download the appropriate version for your operating system (Windows or macOS).
  2. Start Docker Desktop:

    • Launch Docker Desktop and ensure it is running. You should see the Docker icon in your system tray (Windows) or menu bar (macOS).

Build Docker image using dockerfile

If you want to create a custom Docker image, you can use a Dockerfile. A Dockerfile is a text file that contains instructions for building an image.

  1. Create a Dockerfile:

    • Create a new directory and inside it, create a file named Dockerfile which should be stored in Frontend root directory with the following content:

        # Use an official Ubuntu as a parent image
        FROM node:16.15.0
      
        # Set the working directory in the container
        WORKDIR /frontend
      
        # Copy the any file starting with package in json extension from current directory contents into the container at /frontend
        COPY package*.json ./
      
        # Install any needed packages specified in package.json
        RUN npm install
      
        # Copy the all current directory contents into the container at /frontend
        COPY . ./
      
        # Make port 3000 available to the world outside this container
        EXPOSE 3000
      
        # Define environment variable
        ENV NAME World
      
        # Run the build  
        RUN npm run build
      
        # Launch the container
        CMD [ "npm", "start" ]
      
    • This is a simple example that sets up an Nodejs container with 16.15.0 version, installs NPM and runs a React script.

    • Similarly, For Backend - We write the below Dockerfile stored in Backend root directory and Run the Backend as well:

        # Use an official Ubuntu as a parent image
        FROM node:16.15.0
      
        # Set the working directory in the container
        WORKDIR /backend
      
        # Copy the any file starting with package in json extension from current directory contents into the container at /backend
        COPY package*.json ./
      
        # Install any needed packages specified in package.json
        RUN npm install
      
        # Copy the all current directory contents into the container at /backend
        COPY . ./
      
        # Make port 5001 available to the world outside this container
        EXPOSE 5001
      
        # Run and launch the application  
        CMD [ "node", "Server.js" ]
      
        # Define environment variable
        ENV NAME World
      
  2. Build the Docker Image:

    • Navigate to the directory containing the Dockerfile and run the following command:

        docker build -t backend .
      
    • The -t flag tags the image with a name (backend or frontend).

  3. Run the Custom Image:

    • Now you can run a container from your custom image:

        # Run the Frontend application
        docker run -d -p 3000:3000 frontend
      
        # Run the Backend application
        docker run -d -p 5001:5001 backend
      

Creating ACR and Pushing container images from Local to ACR

  1. Open a terminal or command prompt where you have the Azure CLI installed.

  2. Log in to your Azure account if you haven't already:

     az login
    
  3. Set the desired Azure subscription if you have multiple subscriptions:

     az account set --subscription <your-subscription-id>
    
  4. Create a resource group if you don't have one already. For example:

     az group create --name myResourceGroup --location eastus
    
  5. Create the Azure Container Registry:

     az acr create --resource-group myResourceGroup --name myContainerRegistry --sku Basic
    
     #eg:
     az acr create --resource-group aks_poc1 --name backendpoc --sku Basic
    

Here’s a detailed explanation of the command:

  • az acr create: This command is used to create a new Azure Container Registry.

  • --resource-group myResourceGroup: Specifies the resource group in which to create the registry. Replace myResourceGroup with your resource group name.

  • --name myContainerRegistry: The name of the container registry. Replace myContainerRegistry with your desired registry name. Note that the name must be globally unique within Azure.

  • --sku Basic: Specifies the SKU (pricing tier) for the registry. Options include Basic, Standard, and Premium.

  1. Log in to Azure Container Registry

    Log in to your Azure Container Registry (ACR):

     az acr login --name backendpoc
    
  2. Tag the Docker Image

    Assuming you have already built a Docker image locally, you need to tag it with the fully qualified path to your ACR. Replace your-image-name with the name of your Docker image and tag with the appropriate tag (e.g., latest):

     docker tag your-image-name backendpoc.azurecr.io/your-image-name:tag
    

    For example, if your image name is backend or frontend and you are tagging it as v1:

     docker tag backend backendpoc.azurecr.io/backend:v1
     docker tag frontend backendpoc.azurecr.io/frontend:v1
    
  3. Push the Docker Image

    Push the tagged image to your ACR:

     docker push backendpoc.azurecr.io/your-image-name:tag
    

    Using the previous example:

     docker push backendpoc.azurecr.io/backend:v1
     docker push backendpoc.azurecr.io/frontend:v1
    

    Verify the Image in ACR

    You can verify that your image has been pushed to ACR by using the Azure CLI:

     az acr repository list --name backendpoc --output table
    

    This command lists all the repositories in your ACR. You can also view specific tags of an image:

     az acr repository show-tags --name backendpoc --repository your-image-name --output table