From Docker to Azure: Deploy a Free Web App Using Terraform
Azure App Service is a great platform for hosting web applications, including containerized applications. In this guide, we’ll walk through deploying a Docker image to Azure App Service using Terraform, ensuring a fully automated and repeatable deployment process. Best of all, we'll use the Free Tier (F1) to minimize costs. This guide is ideal for beginners who want to explore this process and gain practical experience.
Why Use Terraform for Azure App Service Deployment?
Terraform is an Infrastructure-as-Code (IaC) tool that allows you to define and manage cloud infrastructure in a declarative way. Using Terraform for Azure App Service deployments provides:
- Automation: No need to manually configure resources via the Azure Portal.
- Version Control: Easily track infrastructure changes.
- Repeatability: Deploy the same configuration across multiple environments.
- Scalability: Modify configurations effortlessly as your application grows.
Prerequisites
Before we begin, ensure you have:
- Azure CLI installed (Download Here)
- Terraform installed (Download Here)
- An Azure account (Sign Up for Free)
- A Docker image published on a registry (e.g., Docker Hub or Azure Container Registry). For this guide, we’ll use
vadaszgergo/ip-tools:latestas an example. This image is available here as well: https://hub.docker.com/r/vadaszgergo/ip-tools
I wrote about this ip-tools application here:
Step 1: Authenticate with Azure
First, log in to your Azure account:
az loginIf you have multiple subscriptions, set the correct one:
az account set --subscription "<your-subscription-id>"Step 2: Create Terraform Configuration
Create a new directory for your Terraform files and navigate into it:
mkdir azure-appservice-terraform && cd azure-appservice-terraformCreate a main.tf file and add the following Terraform configuration:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "xxxxxx" # Paste your Azure subscription ID here
}
# Create a Resource Group
resource "azurerm_resource_group" "rg" {
name = "free-appservice-rg"
location = "West US"
}
# Create an App Service Plan (F1 Free Tier)
resource "azurerm_service_plan" "appserviceplan" {
name = "free-appservice-plan"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
os_type = "Linux"
sku_name = "F1" # Free tier
}
# Create an App Service with a Docker Container
resource "azurerm_linux_web_app" "webapp" {
name = "ipcalculator-app" # Name your App Service
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
service_plan_id = azurerm_service_plan.appserviceplan.id
site_config {
application_stack {
docker_image_name = "vadaszgergo/ip-tools:latest" # Pick your preferred docker image based on your requirements
}
always_on = false # Free tier doesn't allow you to use always_on = true
}
depends_on = [azurerm_service_plan.appserviceplan] # Ensure App Service Plan exists first
}Step 3: Initialize and Apply Terraform
Run the following Terraform commands to deploy the resources:
Initialize Terraform (downloads provider plugins):
terraform initPreview the deployment plan:
terraform planApply the configuration (deploy the resources):
terraform applyTerraform will now provision:
- A Resource Group (
free-appservice-rg) - An App Service Plan (
free-appservice-plan) - A Linux-based Web App (
ipcalculator-app) running the specified Docker image
Step 4: Verify the Deployment
Retrieve the app's URL with (modify the name and resource group if you changed those in main.tf):
az webapp show --name ipcalculator-app --resource-group free-appservice-rg --query "defaultHostName" -o tsvThen, open it in your browser:
open https://<your-app-name>.azurewebsites.netIf everything was successful, your Dockerized app should now be running on Azure! The first load may take 20-30 seconds, but subsequent visits should be much faster (this is due to Free tier cold start).
Step 5: Managing and Destroying Resources
To update your web app with a new Docker image, change the docker_image_name in main.tf and run:
terraform applyTo delete all resources and clean up, run:
terraform destroyThis will remove all the resources Terraform created, preventing unnecessary charges.
Conclusion
In this guide, we deployed a Docker-based web application on Azure App Service using Terraform. This approach provides a scalable and repeatable way to manage cloud applications without manually configuring resources.
Key Takeaways:
- Azure App Service supports containerized applications with Linux plans
- The Free Tier (F1) allows you to run basic web applications at zero cost
- Terraform simplifies infrastructure management and deployment automation
Now you can easily modify and scale this setup for production environments by upgrading the App Service Plan, using Azure Container Registry, or adding CI/CD pipelines.
Have questions or feedback? Let me know in the comments!