From Commands to a Description
In the previous lesson you built the practice app with commands. To change its
message you had to remove the container and type the whole docker run
command again, remembering its name, network, port and image. Nothing recorded
what existed or what it was supposed to look like.
Terraform works the other way round. You write a description of what should exist, and Terraform works out which commands to run. This is called a declarative approach: you declare the result, not the steps. For the practice app, the description is a block like this one. You will write the complete file in the next lesson; this is an illustrative fragment:
resource "docker_container" "app" {
name = "tfp-first-app"
image = docker_image.app.image_id
command = ["-text=hello from Terraform"]
}
It says: there should be a Docker container, which Terraform will call
docker_container.app, named tfp-first-app, running the practice app with
this reply text. It does not say "create" or "replace". If the container is
missing, Terraform creates it. If the text changes, Terraform works out that
the container must be replaced. If it already matches, Terraform does nothing.
The Pieces Terraform Works With
Four things take part in every run.
- Configuration: your
.tffiles, describing what should exist. - Providers: plugins that know how to talk to one platform. Terraform itself knows nothing about containers; the Docker provider turns its requests into calls to the Docker engine.
- State: Terraform's record of the objects it manages. For each resource
in your configuration, state holds the real object's ID and its settings as
last seen. This mapping is how Terraform knows that
docker_container.appis the container with a particular ID, and not some other container that happens to look similar. - Real infrastructure: what actually exists right now, here the containers, networks and volumes in Docker.
State deserves a closer look later in the course. For now, the key idea is that Terraform never guesses which real object belongs to a block in your configuration; it looks the answer up in state.
Plan: Working Out the Difference
terraform plan answers the question "what would it take to make the real
infrastructure match the configuration?" It does three things:
- It reads the current settings of every real object recorded in state, through the provider. This catches changes made outside Terraform, such as a container someone deleted.
- It compares the configuration with that up-to-date state.
- It proposes actions that would make the real objects match the configuration.
A plan changes nothing. It is a proposal for you to check. Each resource gets one of these actions:
| Symbol | Action | When |
|---|---|---|
+ | Create | In the configuration but not in state, or the real object has gone |
~ | Update in place | A setting changed that the platform can change on the existing object |
-/+ | Replace | A setting changed that cannot be changed in place: destroy, then create |
- | Destroy | In state but no longer in the configuration |
| (none) | No change | Configuration, state and the real object agree |
Whether a change is an update or a replacement is decided by the provider, based on what the platform allows. For a Docker container, the restart policy can be updated in place, but the command, including the app's reply text, cannot, so changing it means a replacement.
Apply: Carrying Out the Plan
terraform apply makes a fresh plan, shows it, and asks you to approve it.
After you type yes, it carries out each action through the provider and
records the results in state.
Terraform does not work through the actions in the order you wrote them.
It builds a dependency graph from the references between resources. The
container block above refers to docker_image.app.image_id, so Terraform
knows the image must exist before the container can be created, and that the
container must be destroyed before its image. Actions that do not depend on
each other run at the same time, up to 10 by default.
If an action fails part way through, the actions that already succeeded stay done and stay recorded in state. Terraform does not roll them back. A later lesson covers recovering from a partly applied change.

Follow the diagram from the top. State and configuration both feed the plan, and the plan also reads the real objects (the grey arrow). The proposed actions go to you for approval. Apply then changes the real infrastructure through the provider (the green arrow on the right) and records what it did. That updated state is where the next plan starts, shown by the dashed arrow.
One Container Through Its Life
Here is what Terraform proposes at each stage of one container's life. The plan lines are illustrative excerpts from Terraform 1.16.5 with the Docker provider; IDs differ on your computer.
1. The first apply. The configuration has an image and a container; state is empty. Terraform proposes to create both:
# docker_container.app will be created
Plan: 2 to add, 0 to change, 0 to destroy.
Run terraform plan straight after the apply and it reports
No changes. Your infrastructure matches the configuration. That repeatable
"nothing to do" is the normal resting state.
2. A setting Docker can change. You add a restart policy, restart = "unless-stopped". Docker can update an existing container's restart policy, so
the plan updates it in place:
# docker_container.app will be updated in-place
~ restart = "no" -> "unless-stopped"
Plan: 0 to add, 1 to change, 0 to destroy.
3. A setting Docker cannot change. You change the reply text in command.
The plan marks the argument # forces replacement and replaces the container:
# docker_container.app must be replaced
-/+ resource "docker_container" "app" {
~ command = [ # forces replacement
~ "-text=hello from Terraform" -> "-text=hello again",
4. Someone deletes the container outside Terraform. When the next plan reads the real objects, the container has gone, so the plan creates it again:
# docker_container.app will be created
Plan: 1 to add, 0 to change, 0 to destroy.
5. Someone stops the container outside Terraform. The provider's
must_run setting is true by default, meaning the container must be running.
Reading the real container finds it stopped. A stopped container reports no
published ports, so for this container, which publishes one, provider 4.6
plans a replacement (a container without published ports is simply started
again, in place):
# docker_container.app must be replaced
~ must_run = false -> true
6. You remove the block from the configuration. The container is in state but no longer described, so the plan destroys it, and says why:
# docker_container.app will be destroyed
# (because docker_container.app is not in configuration)
Plan: 0 to add, 0 to change, 1 to destroy.
Deleting a block is therefore an instruction to delete the real object.
7. Someone creates another tfp- container by hand. Terraform has no record
of it in state, so plans ignore it completely, however similar it looks. Later
lessons show how to bring such objects under Terraform's management.
What Terraform Does Not Do
- Watch continuously. Terraform compares only when you run
planorapply. Between runs, nothing notices changes. - Manage what it has no record of. Objects that are not in state are invisible to it.
- Undo a partly failed apply. Successful actions stay done.
- Understand your application. It manages the container's settings; it
cannot tell whether the app inside works. You still check that with
curl.
Check Your Understanding
For each change, predict the action Terraform proposes for the practice app's container: create, update in place, replace, destroy or nothing.
- You add a memory limit to the container block. (Docker can change a running container's memory limit.)
- You change the published port from 18080 to 18081. (Published ports are fixed when a container is created.)
- Someone runs
docker rm -f tfp-first-app, then you runterraform plan. - Someone starts a container called
tfp-extraby hand. You change nothing in the configuration and runterraform plan. - You delete the whole
docker_containerblock, keeping the image block.
Answer and Reasoning
- Update in place. The memory limit is a setting Docker can change on an existing container, so the provider updates it without replacing it.
- Replace. The published port cannot change on an existing container, so
the plan shows
-/+and marks the port setting as forcing replacement. - Create. The plan first reads the real container, finds it gone, and proposes creating it again to match the configuration.
- No change.
tfp-extrais not in state, so Terraform does not know it exists. Itstfp-name makes no difference. - Destroy the container. It is still recorded in state but no longer in the configuration. The image is still described, so it stays.