DEV Community

Cover image for Deploying a Dockerized Web Application to Azure Using Terraform (Manual Docker Build and Push)
Theodora
Theodora

Posted on

Deploying a Dockerized Web Application to Azure Using Terraform (Manual Docker Build and Push)

We will go through the process of deploying a Dockerized Node.js application to Azure Container Instances (ACI) using Terraform. Along the way, we’ll leverage a manual approach to build and push the Docker image to Azure Container Registry (ACR) deployed by Terraform, and then automate the deployment of that image to ACI using Terraform.

Prerequisites

Before diving into the deployment, make sure you have the following tools installed and configured:

  1. Step 1: Set Up Azure Container Registry (ACR)
  2. Step 2: Manually Build and Push the Docker Image
  3. Step 3: Deploy to Azure Container Instances (ACI) Using Terraform
  4. Step 4: Apply Terraform and Deploy
  5. Step 5: Access Your Application
  6. Troubleshooting Tips
  7. Conclusion

Step 1: Set Up Azure Container Registry (ACR)

To get started, we first need to create an Azure Container Registry (ACR), which will store our Docker images before deploying them to Azure Container Instances (ACI).

In your Terraform configuration, create the following resources:

main.tf (Terraform Configuration)

This creates:

  • rg-theodora-container: The resource group in Azure.
  • hostcr2025: The Azure Container Registry where we will push our Docker image.

set up variables.tf

Create terraform.tfvars

Apply Terraform to Deploy Azure Container Registry

Once your main.tf, variables.tf, and terraform.tfvars files are ready, use the following commands to deploy your Azure resources:

Initializes Terraform, downloads the required providers (like AzureRM), and prepares your workspace.

Checks that all Terraform files are syntactically correct and the configuration is valid.

Generates and shows an execution plan, detailing what Terraform will create, modify, or destroy.

Executes the deployment based on your plan.
When prompted, type yes to confirm and start creating the Azure resources.

Step 2: Manually Build and Push the Docker Image

Now that we have our ACR setup, we need to manually build the Docker image and push it to ACR.

Dockerfile

Here’s the Dockerfile for a basic Node.js application:

This Dockerfile:

  • Starts from a Node.js base image.
  • Installs dependencies.
  • Runs the app using serve on port 3000.

Manually Build and Push Docker Image

To build and push the Docker image to ACR manually, follow these steps:

  1. Login to ACR:

  1. Build the Docker image:

  1. Push the Docker image to ACR:

This step ensures that your Docker image is available in ACR, and Terraform can reference it during the deployment to Azure Container Instances (ACI).

Step 3: Deploy Docker Image to Azure Container Instances (ACI) with Terraform

Now that the Docker image is in ACR, we’ll use Terraform to deploy the container to Azure Container Instances (ACI).

Create your ACI.tf with the following configuration to deploy the image:

Terraform Configuration for ACI Deployment

This configuration:

  • Uses the image we pushed to ACR (hostcr2025.azurecr.io/electrot:v1).
  • Exposes port 3000 (matching the app’s internal port).
  • Creates a public IP and a DNS label (theocontainer2025) for external access.

Step 4: Apply Terraform to Deploy the Container

Now that everything is set up, we’ll use Terraform to deploy the container.
Follow the terraform deployment process above

Step 5: Access Your Application

After deploying the container to Azure Container Instances (ACI), you should be able to access your application.

  • DNS Name: http://theocontainer2025.eastus.azurecontainer.io:3000
  • Public IP: You can also access the app using the public IP address of your ACI container.

For example, the public IP can be retrieved using this Azure CLI command:

Troubleshooting Tips

  • Inaccessible Image: If you encounter the InaccessibleImage error, it typically means there’s an issue with image permissions in ACR. Double-check that the image exists in ACR and that ACI has the correct credentials to pull the image.
  • Port Binding: Ensure that your application is listening to the right port.
  • Container Logs: You can check the logs of your container to make sure it's running correctly using:

Conclusion

In this tutorial, we:

  1. Set up an Azure Container Registry (ACR) using Terraform.
  2. Manually built and pushed the Docker image to ACR.
  3. Used Terraform to deploy the image to Azure Container Instances (ACI).
  4. Exposed the application via public IP and DNS for external access.

While the manual Docker build and push process can seem tedious, it gives you full control and debugging capabilities, especially when things don’t work perfectly with automation. Using Terraform for deployment makes managing and automating the infrastructure side seamless.

Top comments (0)