Terraform on Your Laptop (Part 2)
Neil Haddley • November 29, 2026
I put a small C# web site into Terraform on Docker Desktop, then changed the title, changed the code and scaled to three copies, and watched Terraform apply only what changed each time.
In Part 1 I used Terraform to run a public nginx image. That showed the commands, but nginx never changed, so I did not see the real benefit of Terraform: what happens when I change things.
In this post I give Terraform something that changes. It is a small C# web site. I change its title and colour, then its code, then I scale it from one copy to three. After each change I look at the plan and at the web site in a browser.
Everything ran on Docker Desktop on my Mac with Terraform 1.17.2. The only images are public ones from Microsoft: mcr.microsoft.com/dotnet/sdk:10.0 to build and mcr.microsoft.com/dotnet/aspnet:10.0 to run. You can download the files: main.tf, terraform.tfvars, site.csproj, Program.cs and Dockerfile. The first version of Program.cs is Program-v1.cs.
The web site
The site is an ASP.NET Core minimal app. It reads a title and a colour from environment variables and shows its version and the name of the container that served the page. This is version 1.0:
CSHARP
1var app = WebApplication.CreateBuilder(args).Build(); 2 3var title = Environment.GetEnvironmentVariable("SITE_TITLE") ?? "Hello"; 4var color = Environment.GetEnvironmentVariable("SITE_COLOR") ?? "#2563eb"; 5var version = "1.0"; 6 7app.MapGet("/", () => Results.Content($$""" 8<!doctype html> 9<html> 10<head> 11 <meta charset="utf-8"> 12 <title>{{title}}</title> 13 <style> 14 body { font-family: Arial, sans-serif; margin: 0; background: #f4f6f8; } 15 header { background: {{color}}; color: white; padding: 32px 40px; } 16 main { padding: 24px 40px; } 17 .card { background: white; border-radius: 8px; padding: 16px 20px; margin-bottom: 16px; } 18 </style> 19</head> 20<body> 21 <header><h1>{{title}}</h1></header> 22 <main> 23 <div class="card">Site version: <b>{{version}}</b></div> 24 <div class="card">Served by container: <b>{{Environment.MachineName}}</b></div> 25 </main> 26</body> 27</html> 28""", "text/html")); 29 30app.Run();
Inside a container, Environment.MachineName is the container ID. That lets me see which copy answered, which matters later when I run three.
The Dockerfile builds the site in one image and runs it in a smaller one:
DOCKERFILE
1FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build 2WORKDIR /src 3COPY site.csproj . 4RUN dotnet restore 5COPY Program.cs . 6RUN dotnet publish -c Release -o /out --no-restore 7 8FROM mcr.microsoft.com/dotnet/aspnet:10.0 9WORKDIR /app 10COPY /out . 11ENV ASPNETCORE_URLS=http://+:8080 12ENTRYPOINT ["dotnet", "site.dll"]
I did not need the .NET SDK on my Mac, because the build runs inside Docker.
The Terraform file
main.tf has the same five kinds of block as Part 1, with three new ideas:
HCL
1variable "site_title" { 2 type = string 3 default = "Hello from Terraform" 4} 5 6variable "site_color" { 7 type = string 8 default = "#2563eb" 9} 10 11variable "instances" { 12 type = number 13 default = 1 14} 15 16resource "docker_image" "site" { 17 name = "terraform-demo-site:latest" 18 19 build { 20 context = "${path.module}/app" 21 } 22 23 triggers = { 24 source_hash = sha1(join("", [ 25 for f in sort(fileset("${path.module}/app", "*")) : 26 filesha1("${path.module}/app/${f}") 27 ])) 28 } 29} 30 31resource "docker_container" "site" { 32 count = var.instances 33 name = "terraform-demo-site-${count.index + 1}" 34 image = docker_image.site.image_id 35 36 env = [ 37 "SITE_TITLE=${var.site_title}", 38 "SITE_COLOR=${var.site_color}", 39 ] 40 41 ports { 42 internal = 8080 43 external = 8080 + count.index 44 } 45} 46 47output "urls" { 48 value = [for c in docker_container.site : "http://localhost:${c.ports[0].external}"] 49}
The three new ideas:
buildandtriggers. Terraform builds the image from myappfolder. Thetriggersline fingerprints every file in that folder. When a file changes, the fingerprint changes, and Terraform knows the image is out of date.count.count = var.instancesmakes that many containers from one block. Instance 1 uses port 8080, instance 2 uses 8081, and so on.env. The title and colour reach the C# program as environment variables, so I can change them without touching the code.
(I find out in Step 3.) In the app folder I placed Program.cs, site.csproj and Dockerfile.
Step 1: first apply
PROMPT
1terraform init 2terraform plan 3terraform apply -auto-approve
The plan said Plan: 2 to add, 0 to change, 0 to destroy. The first time I ran this, the image took 24 seconds because Docker had to download the .NET SDK. Later builds took a second or two. Then the container started:
CODE
1docker_image.site: Creation complete after 24s 2docker_container.site[0]: Creation complete after 0s 3 4Apply complete! Resources: 2 added, 0 changed, 0 destroyed. 5 6urls = [ 7 "http://localhost:8080", 8]
I opened http://localhost:8080:
Docker Desktop listed the new container, started from the image Terraform had just built:
Step 2: change the title and colour
I wanted a different title and colour. I did not edit any C#. I changed two variables:
PROMPT
1terraform plan -var "site_title=Spring Sale" -var "site_color=#16a34a"
CODE
1# docker_container.site[0] must be replaced 2~ env = [ # forces replacement 3 - "SITE_COLOR=#2563eb", 4 - "SITE_TITLE=Hello from Terraform", 5 + "SITE_COLOR=#16a34a", 6 + "SITE_TITLE=Spring Sale", 7 ] 8 9Plan: 1 to add, 0 to change, 1 to destroy.
I read the plan before applying. It told me three things. Only the container changes, so the image is left alone. The change to env forces a replacement, because a container's environment is fixed when it starts. And the exact old and new values were on the screen.
CODE
1docker_container.site[0]: Destroying... 2docker_container.site[0]: Creation complete after 0s 3Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
Then I saved the two values in a file called terraform.tfvars, so I would not have to type them again. Terraform reads that file automatically. A plan afterwards said: "No changes. Your infrastructure matches the configuration."
Step 3: change the C# code
Now I changed the program itself. In Program.cs I set version to "2.0" and added one more card:
CSHARP
1<div class="card">New in 2.0: free delivery on orders over $50</div>
I did not run docker build. I ran terraform plan:
CODE
1# docker_image.site must be replaced 2~ triggers = { # forces replacement 3 ~ "source_hash" = "5d7eb35d2263c4f470abda967da8e59b29c27e59" -> "7541efac318c849b98a534d1de4f27919396421c" 4 } 5 6# docker_container.site[0] must be replaced 7~ image = "sha256:71e774805a83..." -> (known after apply) # forces replacement 8 9Plan: 2 to add, 0 to change, 2 to destroy.
This answers the question from earlier. The fingerprint of my files changed, so Terraform rebuilt the image. Then, because the container uses the image ID, it replaced the container too. I never told it to do that. The dependency did the work.
CODE
1docker_container.site[0]: Destruction complete after 0s 2docker_image.site: Destruction complete after 0s 3docker_image.site: Creation complete after 2s 4docker_container.site[0]: Creation complete after 0s 5Apply complete! Resources: 2 added, 0 changed, 2 destroyed.
The rebuild took a second or two, because Docker reused the layers it already had.
Step 4: scale to three copies
I added one line to terraform.tfvars:
CODE
1instances = 3
PROMPT
1terraform plan
CODE
1# docker_container.site[1] will be created 2# docker_container.site[2] will be created 3 4Plan: 2 to add, 0 to change, 0 to destroy. 5 6 ~ urls = [ 7 "http://localhost:8080", 8 + "http://localhost:8081", 9 + "http://localhost:8082", 10 ]
Look at the end of the first line: 0 to change, 0 to destroy. The container already running was not touched. Terraform added only the two that were missing. Here are the three, side by side:
Docker Desktop listed the same three containers, all from the same image, on ports 8080, 8081 and 8082:
I used count for the copies here. Part 7 shows what count does when I remove a copy from the middle, and how for_each avoids it.
Step 5: someone deletes a copy by hand
I removed the second container with Docker, as if a colleague had cleaned up:
CODE
1$ docker rm -f terraform-demo-site-2
PROMPT
1terraform plan
CODE
1# docker_container.site[1] will be created 2Plan: 1 to add, 0 to change, 0 to destroy.
Docker Desktop showed the gap straight away. The container on port 8081 had gone:
Terraform noticed that copy 2 was gone and planned to put back exactly that one. After the apply, http://localhost:8081 answered again, from a new container:
Step 6: clean up
PROMPT
1terraform destroy
CODE
1docker_container.site[1]: Destruction complete after 0s 2docker_container.site[2]: Destruction complete after 0s 3docker_container.site[0]: Destruction complete after 0s 4docker_image.site: Destruction complete after 0s 5 6Destroy complete! Resources: 4 destroyed.
Docker Desktop showed an empty Containers list:
Destroy removed the three containers and the image Terraform had built. When I checked Docker, no containers and no terraform-demo-site image were left. I did not use keep_locally here, so the built image was removed.
What Terraform gave me
| What I wanted | What Terraform did |
|---|---|
| Change the title and colour | Showed the old and new values, and replaced only the container |
| Change the C# code | Noticed the source had changed, rebuilt the image and replaced the container |
| Run three copies | Added the two missing copies and left the running one alone |
| Recover from a manual delete | Found the missing copy and recreated only that one |
| Remove everything | Destroyed exactly what it had created |
The file is also the documentation. Anyone can read main.tf and see what is running, where, and with which settings.
Where this falls short
I want to be honest about the limits of this demo:
- Replacement means a gap. Terraform destroys the old container and then creates the new one, so for a moment the site is down. A real service would use rolling updates behind a load balancer. Part 4 shows rolling updates, using Kubernetes.
- Building images is not usually Terraform's job. On a team, a build pipeline builds the image, pushes it to a registry, and Terraform deploys a specific version. I used the
buildblock here because it makes the demo small enough to read. - The state file is local. It lives in my folder. A team would keep it in a shared, locked place, and Part 6 moves it into one.
- These are copies, not a real cluster. Three containers on one laptop show the idea. They do not give me real availability.
The workflow does not change when the target does. The same plan, apply and destroy would work for an Azure container app or a set of virtual machines. Only the provider and the resource types change.
Everything in this post ran on Docker Desktop on my own machine, using Terraform 1.17.2, the kreuzwerker/docker provider 3.9.0 and the public .NET 10 images from Microsoft's container registry.








