Automate Docker Build & Push to Azure Container Registry (ACR)

S3CloudHub - Sep 6 - - Dev Community

Introduction

In the world of modern software development, continuous integration and delivery (CI/CD) are essential. Docker simplifies containerization, while Azure Container Registry (ACR) offers a secure place to manage Docker images. This guide will walk you through automating the Docker build and push process to Azure Container Registry using Packer and GitHub Actions.

Image description
Why Automate Docker Builds and Pushes?

  • Efficiency: Automates repetitive tasks, saving time and reducing human error.
  • Consistency: Ensures that Docker images are built and pushed consistently across environments.
  • Scalability: Simplifies managing Docker images at scale, especially in large projects or teams.

Prerequisites

  • Basic knowledge of Docker and Azure.
  • An Azure account with access to Azure Container Registry.
  • A GitHub repository to store your configuration files.
  • Basic understanding of GitHub Actions.

Step 1: Set Up Azure Container Registry

1. Create an Azure Container Registry

  • Log in to the Azure portal.
  • Navigate to “Create a resource” and select “Container Registry.”
  • Fill in the required details and create the registry.

2. Obtain Registry Credentials

  • In your Azure Container Registry, navigate to the “Access keys” section.
  • Copy the login server URL, username, and password; you’ll need these later.

Step 2: Configure Packer for Docker

1. Install Packer

  • Download and install Packer on your local machine.

2. Create a Packer Template

  • Define a Packer template for building your Docker image. Save this as packer-template.json in your GitHub repository.
{
  "builders": [
    {
      "type": "docker",
      "image": "ubuntu:latest",
      "commit": true,
      "changes": [
        "RUN apt-get update -y",
        "RUN apt-get install -y <your-dependencies>"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "echo 'Hello, Packer!'"
      ]
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up GitHub Actions for CI/CD

1. Create a GitHub Actions Workflow
In your GitHub repository, navigate to “Actions” and create a new workflow file, build-and-push.yml, under .github/workflows/.

name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Cache Docker layers
        uses: actions/cache@v3
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Build Docker image
        run: |
          docker buildx build --file Dockerfile --tag ${{ secrets.REGISTRY_NAME }}/my-app:latest .

      - name: Log in to Azure Container Registry
        uses: docker/login-action@v2
        with:
          registry: ${{ secrets.REGISTRY_NAME }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Push Docker image
        run: |
          docker push ${{ secrets.REGISTRY_NAME }}/my-app:latest

Enter fullscreen mode Exit fullscreen mode

2. Add GitHub Secrets

  • Navigate to your GitHub repository’s settings and add the following secrets:
  • REGISTRY_NAME – Your ACR login server URL.
  • REGISTRY_USERNAME – Your ACR username.
  • REGISTRY_PASSWORD – Your ACR password.

Step 4: Test Your Setup

1. Push Changes to GitHub

  • Commit and push your changes to the main branch.

2. Verify the Workflow

  • Navigate to the “Actions” tab in your GitHub repository to monitor the build and push process.

3. Check ACR

  • Log in to the Azure portal and verify that your Docker image appears in the Azure Container Registry.

Explore more detailed content and step-by-step guides on our YouTube channel:-

image alt text here

🔗 GitHub Repository: https://github.com/S3CloudHubRepo/packer-azure-acr-push

Connect with Us!
Stay connected with us for the latest updates, tutorials, and exclusive content:

WhatsApp:- https://www.whatsapp.com/channel/0029VaeX6b73GJOuCyYRik0i
facebook:- https://www.facebook.com/S3CloudHub
youtube:- https://www.youtube.com/@s3cloudhub
github:- https://github.com/S3CloudHubRepo

Connect with us today and enhance your learning journey!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .