Terraform on Your Laptop (Part 1)
Neil Haddley โข November 29, 2026
I wrote one small Terraform file that pulls the public nginx image and runs it in Docker Desktop, then used plan, apply, a port change, a deleted container and destroy to see how Terraform works.
Terraform is usually shown with a cloud account, a credit card and a long list of permissions. I wanted to see how it works with none of that. Docker Desktop is already on my Mac, and Docker has a Terraform provider, so I used my own machine as the "cloud".
In this post I write one Terraform file. It pulls the public nginx image from Docker Hub, starts a container, and serves a web page. Then I change it, break it, and tear it down, to see what Terraform does each time.
I ran everything below on a Mac with Docker Desktop and Terraform 1.17.2. You can download main.tf and follow along.
What you need
- Docker Desktop, running. On my Mac, Terraform's Docker provider found it with no configuration.
- The Terraform command-line tool. I used the macOS Arm build of version 1.17.2 from HashiCorp's download site. HashiCorp's install page lists the other ways to get it. Check it works with
terraform version. - An empty folder, with the
main.tfbelow saved in it. Every command in this post runs in that folder.
What Terraform does in one sentence
I write down the things I want, and Terraform makes the real world match. If the real world already matches, it does nothing. If it does not, Terraform works out what to create, change or remove.
The file
This is all of main.tf:
HCL
1terraform { 2 required_providers { 3 docker = { 4 source = "kreuzwerker/docker" 5 version = "~> 3.0" 6 } 7 } 8} 9 10provider "docker" {} 11 12variable "external_port" { 13 type = number 14 default = 8080 15} 16 17resource "docker_image" "nginx" { 18 name = "nginx:alpine" 19 keep_locally = true 20} 21 22resource "docker_container" "web" { 23 name = "terraform-demo-web" 24 image = docker_image.nginx.image_id 25 26 ports { 27 internal = 80 28 external = var.external_port 29 } 30 31 upload { 32 file = "/usr/share/nginx/html/index.html" 33 content = "<h1>Hello from Terraform</h1>\n" 34 } 35} 36 37output "url" { 38 value = "http://localhost:${var.external_port}" 39}
There are only five kinds of block:
| Block | What it does |
|---|---|
terraform | Says which provider I need. A provider is the plug-in that knows how to talk to one system, here Docker. |
provider | Configures that plug-in. Empty means "use the Docker Desktop on this machine". |
variable | A setting I can change without editing the rest of the file. |
resource | A thing I want to exist: an image, and a container. |
output | A value Terraform prints when it finishes. |
The container refers to docker_image.nginx.image_id. That one line tells Terraform that the container depends on the image, so it creates the image first. I never write the order down.
Step 1: terraform init
PROMPT
1terraform init
Terraform read my file, saw that I need the kreuzwerker/docker provider, and downloaded version 3.9.0 into a hidden .terraform folder. It also wrote .terraform.lock.hcl, which records exactly which provider version I used, so the next run gets the same one.
I only run init once per folder, or again when I add a provider.
Two cheap checks
Before the first plan, two commands catch most typing mistakes without touching Docker:
PROMPT
1terraform fmt 2terraform validate
terraform fmt rewrites my files into Terraform's standard layout and prints the names of any files it changed. My file was already tidy, so it printed nothing. When I made it untidy on purpose, it printed main.tf and fixed it. terraform validate checks that the file makes sense, and it said Success! The configuration is valid. To see it earn its place, I changed image_id to imageid, a typo, and ran it again:
CODE
1Error: Unsupported attribute 2 3 on main.tf line 24, in resource "docker_container" "web": 4 24: image = docker_image.nginx.imageid 5 6This object has no argument, nested block, or exported attribute named 7"imageid". Did you mean "image_id"?
Terraform named the line, the typo and the fix. I put the typo back. I do not repeat these two checks in the later posts, but they are worth running before any plan.
Step 2: terraform plan
PROMPT
1terraform plan
Plan changes nothing. It compares my file with what exists, and lists what it would do. The ending was:
CODE
1Plan: 2 to add, 0 to change, 0 to destroy. 2 3Changes to Outputs: 4 + url = "http://localhost:8080"
Every line starting with + is something that will be created. Values marked (known after apply), such as the container ID, are things Docker decides, so Terraform cannot know them yet.
Step 3: terraform apply
PROMPT
1terraform apply -auto-approve
I used -auto-approve because this is a throwaway demo. Normally Terraform shows the plan and asks me to type yes. The result:
CODE
1docker_image.nginx: Creating... 2docker_image.nginx: Creation complete after 0s 3docker_container.web: Creating... 4docker_container.web: Creation complete after 1s 5 6Apply complete! Resources: 2 added, 0 changed, 0 destroyed. 7 8Outputs: 9 10url = "http://localhost:8080"
The image came first and the container second, as the dependency required. I checked it two ways:
CODE
1$ curl localhost:8080 2<h1>Hello from Terraform</h1> 3 4$ docker ps --filter name=terraform-demo 5terraform-demo-web 0.0.0.0:8080->80/tcp
I also looked at Docker Desktop. The container Terraform created was listed there, published on port 8080 and using the nginx:alpine image:
Terraform also wrote a file called terraform.tfstate. That file is Terraform's memory. It lists the two resources it created and their IDs, and it is how Terraform knows next time what already exists.
CODE
1$ terraform state list 2docker_container.web 3docker_image.nginx
Step 4: change one thing
I changed the port from 8080 to 8081 without touching the file, by passing a variable on the command line:
PROMPT
1terraform plan -var external_port=8081
The key lines were:
CODE
1# docker_container.web must be replaced 2~ ports { 3 ~ external = 8080 -> 8081 # forces replacement 4} 5 6Plan: 1 to add, 0 to change, 1 to destroy.
Docker cannot change the published port of a running container, so Terraform cannot update it in place. It said must be replaced: destroy the old container and create a new one. The image was not touched. This is Terraform being honest about what a change really costs. Then I applied it:
CODE
1docker_container.web: Destruction complete after 0s 2docker_container.web: Creating... 3docker_container.web: Creation complete after 0s 4 5Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
curl localhost:8081 returned the page, and docker ps showed 0.0.0.0:8081->80/tcp. Docker Desktop showed the same thing: a different container ID, now on port 8081.
Step 5: break it by hand
Terraform compares my file with the real world every time. To see this, I deleted the container with Docker directly, behind Terraform's back:
CODE
1$ docker rm -f terraform-demo-web
Then I ran plan again:
CODE
1# docker_container.web will be created 2Plan: 1 to add, 0 to change, 0 to destroy.
Terraform refreshed its picture from Docker, saw the container was gone, and planned to put it back. One apply later the page was back at localhost:8081. This is why people use Terraform instead of clicking: if someone changes or deletes something by hand, the next plan shows it.
Step 6: terraform destroy
PROMPT
1terraform destroy -var external_port=8081
CODE
1docker_container.web: Destruction complete after 0s 2docker_image.nginx: Destruction complete after 0s 3 4Destroy complete! Resources: 2 destroyed.
The container was gone. I had set keep_locally = true on the image, so Terraform removed its own record of the image but left nginx:alpine in my local Docker, which saves a download next time. I did not test it, but without that setting the provider removes the image on destroy as well.
What I learned
Where to go next
This was the smallest useful Terraform project: one file, one container. The rest of the series builds on it:
- Part 2 runs a C# web site, changes it, and scales it to three copies.
- Part 3 adds SQL Server with T-SQL stored procedures, and shows how to protect the data.
- Part 4 puts the site on Kubernetes and asks who fixes what, Terraform or Kubernetes.
- Part 5 runs dev, UAT and prod from one shared module, and moves a change through them.
- Part 6 moves the state into a shared, locked database.
- Part 7 collects the odds and ends:
for_each, tests, a pipeline, data sources and import.
The same five blocks work for cloud resources. Only the provider changes: for Azure I would use the azurerm provider, and a resource group, a container app or a storage account would replace the Docker image and container. I did not run Terraform against a cloud in this series. Everything here ran on my laptop, so I could learn the workflow without a bill.
Everything in this post ran against Docker Desktop on my own machine. The public image is nginx:alpine from Docker Hub, and the Terraform provider is kreuzwerker/docker.

