Automate Docker Image Deployment to GitLab, ACR, ECR, JFrog Artifactory & DockerHub with Terraform Packer & GitHub Actions

In the evolving landscape of DevOps, automating the build and deployment of containerized applications is crucial. Terraform Packer, in combination with GitHub Actions, allows you to automate the creation and deployment of Docker images to different container registries like GitLab, Azure Container Registry (ACR), AWS Elastic Container Registry (ECR), JFrog Artifactory, and DockerHub. In this guide, I’ll show you how to build and push Docker images using Terraform Packer and GitHub Actions.

Image description

1. Introduction
Packer is a powerful tool that allows you to automate the creation of images, including Docker containers. By integrating Packer with GitHub Actions, you can automate the building of Docker images and push them to different container registries. This saves time and ensures a consistent deployment process across environments.

In this article, we’ll explore how to build and push Docker images using Terraform Packer and GitHub Actions to various platforms, such as GitLab, ACR, ECR, JFrog Artifactory, and DockerHub.

2. Prerequisites
Before you get started, ensure you have the following:

  • Packer: Installed on your local machine and configured for Docker.
  • Terraform: Installed and set up.
  • GitHub Repository: A repository where you’ll configure GitHub Actions.
  • GitLab, ACR, ECR, JFrog, DockerHub Accounts: You’ll need credentials to push Docker images.
  • Access Tokens or Credentials: Store these credentials securely using GitHub Secrets.

For a visual walkthrough of the concepts covered in this article, check out my YouTube playlist:
image alt text here

3. Setting Up GitHub Actions with Terraform Packer Deploying Docker Hub
First, you’ll need to create a GitHub Actions workflow file in your GitHub repository. This workflow will automate the process of building a Docker image using Packer and then pushing the image to a container registry.

Below is an example of a GitHub Actions .yml file for building a Docker image using Packer:

name: Build and Push Docker Image using Packer on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Install Terraform and Packer run: | sudo apt-get update && sudo apt-get install -y unzip curl -LO https://releases.hashicorp.com/terraform/1.4.2/terraform_1.4.2_linux_amd64.zip unzip terraform_1.4.2_linux_amd64.zip sudo mv terraform /usr/local/bin/ curl -LO https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip unzip packer_1.7.8_linux_amd64.zip sudo mv packer /usr/local/bin/ - name: Build Docker Image using Packer run: | packer init packer-config.pkr.hcl packer build packer-config.pkr.hcl - name: Log in to DockerHub run: echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin - name: Push Docker Image run: docker push my-app:${{ github.sha }}
Enter fullscreen mode
Exit fullscreen mode

This workflow sets up Terraform and Packer, builds a Docker image, and pushes it to DockerHub. Let’s now focus on pushing to different registries.

4. Build and Push to GitLab Container Registry Using Packer
GitLab Container Registry allows you to host your Docker images. Here’s how you can automate the build and push process:

  1. Create a Personal Access Token in GitLab with api and write_registry permissions.
  2. Add the token to your GitHub Secrets as GITLAB_TOKEN.
    Update the previous workflow to push images to GitLab:
name: Push Docker to GitLab Using Packer on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Install Terraform and Packer run: | sudo apt-get update && sudo apt-get install -y unzip curl -LO https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip unzip packer_1.7.8_linux_amd64.zip sudo mv packer /usr/local/bin/ - name: Build Docker Image using Packer run: packer build packer-config.pkr.hcl - name: Log in to GitLab Container Registry run: echo "${{ secrets.GITLAB_TOKEN }}" | docker login registry.gitlab.com -u <username> --password-stdin - name: Push Docker Image to GitLab run: docker push registry.gitlab.com/<username>/<repo>:latest
Enter fullscreen mode
Exit fullscreen mode

5. Build and Push to Azure Container Registry (ACR) Using Packer
Azure Container Registry (ACR) is a fully managed private registry for Docker containers. To push Docker images to ACR using Packer, follow these steps:

  1. Log in to Azure using the Azure CLI.
  2. Add your Azure credentials to GitHub Secrets (AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID).
    Here’s an example workflow:
name: Push Docker to ACR Using Packer on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Install Terraform and Packer run: | curl -LO https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip unzip packer_1.7.8_linux_amd64.zip sudo mv packer /usr/local/bin/ - name: Log in to Azure CLI run: az login --service-principal -u ${{ secrets.AZURE_CLIENT_ID }} -p ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }} - name: Build Docker Image using Packer run: packer build packer-config.pkr.hcl - name: Push Docker Image to ACR run: docker push <ACR_REGISTRY_NAME>.azurecr.io/my-app:latest
Enter fullscreen mode
Exit fullscreen mode

6. Build and Push to AWS Elastic Container Registry (ECR) Using Packer
To push Docker images to AWS Elastic Container Registry (ECR), you’ll need to set up your AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) in GitHub Secrets.

Here’s the workflow:

name: Push Docker to ECR Using Packer on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Install Terraform and Packer run: | curl -LO https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip unzip packer_1.7.8_linux_amd64.zip sudo mv packer /usr/local/bin/ - name: Log in to AWS ECR run: | aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com - name: Build Docker Image using Packer run: packer build packer-config.pkr.hcl - name: Push Docker Image to ECR run: docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/my-app:latest
Enter fullscreen mode
Exit fullscreen mode

7. Build and Push to JFrog Artifactory Using Packer
To push Docker images to JFrog Artifactory using Packer, follow these steps:

  1. Set up API tokens in JFrog.
  2. Store credentials in GitHub Secrets.
    Here’s the workflow:
name: Push Docker to JFrog Artifactory Using Packer on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Install Packer run: | curl -LO https://releases.hashicorp.com/packer/1.7.8/packer_1.7.8_linux_amd64.zip unzip packer_1.7.8_linux_amd64.zip sudo mv packer /usr/local/bin/ - name: Build Docker Image using Packer run: packer build packer-config.pkr.hcl - name: Log in to JFrog Artifactory run: echo "${{ secrets.JFROG_PASSWORD }}" | docker login <JFROG_REGISTRY_URL> -u "${{ secrets.JFROG_USERNAME }}" --password-stdin - name: Push Docker Image to JFrog Artifactory run: docker push <JFROG_REGISTRY_URL>/my-app:latest
Enter fullscreen mode
Exit fullscreen mode

8. Conclusion
Using Terraform Packer with GitHub Actions to automate the building and pushing of Docker images to container registries streamlines the DevOps process. Whether you’re deploying to GitLab, Azure ACR, AWS ECR, JFrog Artifactory, or DockerHub, Packer provides a consistent and efficient approach.

By automating these workflows, you ensure faster delivery, minimize human error, and maintain consistent builds across all environments.

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

Connect with us today and enhance your learning journey!


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Free & easy ad network.