Put drift right without losing data

Infrastructure changesAbout 45 minDraft

Not started.

What you need

Windows 11 with Docker Desktop running, and either PowerShell or a WSL terminal. The verifier runs inside a throwaway Node container, so nothing else needs installing.

Steps

  1. Save main.tf and verify.mjs into an empty folder. OpenTofu runs from its official image, so nothing needs installing: in this folder, tofu <command> below means the command shown under "Run OpenTofu", followed by that command.
  2. Run tofu init, then tofu apply -auto-approve, then the verifier. It fails: the app writes to /data inside the container, which a replacement loses.
  3. Run tofu destroy -auto-approve. Change main.tf so /data is a docker_volume that OpenTofu manages apart from the container, and protect it with prevent_destroy. Apply again and run the verifier until it passes; it writes a check file into /data.
  4. Cause drift: delete the container by hand with docker rm -f skilltree-iac-app. Run tofu plan and read what it proposes.
  5. Run tofu apply -auto-approve, then the verifier with --after. The container must be new, the volume the same, and the check file still there. Paste its last line below.
  6. Apply once more: OpenTofu should report no changes.

Why it matters

OpenTofu compares three things: the configuration, the state file and what really exists. When someone changes the real thing by hand, that is drift, and plan shows how OpenTofu would put it right.

Anything that holds data should not share a lifecycle with something you are happy to replace. A separate volume resource means replacing the container never touches the data, and prevent_destroy makes OpenTofu refuse any plan that would delete it.

The state file says which real objects OpenTofu manages. It can contain secrets and must not be edited by hand; in a team it lives in a remote backend with locking. Here it is a local file, which the verifier reads.

When you are done

  • Remove the prevent_destroy block from main.tf, then run tofu destroy -auto-approve
  • Delete the folder, including terraform.tfstate and .skilltree-iac.json

Files

Show main.tf
# Starting point for the tofu-drift task. OpenTofu manages a network and an app container through Docker. The app
# writes to /data, but /data lives inside the container, so replacing the container loses it. Fix that, so a
# re-apply after drift brings the container back without losing or recreating the data.
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0"
    }
  }
}

provider "docker" {}

resource "docker_network" "app" {
  name = "skilltree-iac"
}

resource "docker_image" "alpine" {
  name         = "alpine:3.20"
  keep_locally = true
}

resource "docker_container" "app" {
  name    = "skilltree-iac-app"
  image   = docker_image.alpine.image_id
  command = ["sleep", "infinity"]

  networks_advanced {
    name = docker_network.app.name
  }

  labels {
    label = "skilltree.task"
    value = "tofu-drift"
  }
}

It asks Docker for what it needs and changes nothing else; read it before you run it. Mounting the Docker socket gives it the same control over Docker that you have.

Run OpenTofu

In the folder with your files, in PowerShell or in WSL, followed by the OpenTofu command:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "${PWD}:/work" -w /work ghcr.io/opentofu/opentofu:1.10.3

Run the verifier

In the folder with your files, in PowerShell or in WSL:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "${PWD}:/work" -w /work node:22-alpine node verify.mjs tofu-drift

The last step adds --after to the end.

The verifier runs on your machine, so this page cannot see the result for itself: what you paste is what it records.

Checks

First run

  • OpenTofu state records the containernot yet checked
  • The db service is runningnot yet checked
  • It is on the skilltree-iac networknot yet checked
  • /data is a volume OpenTofu managesnot yet checked
  • A check file was writtennot yet checked

The final run (--after)

  • OpenTofu state records the containernot yet checked
  • The db service is runningnot yet checked
  • It is on the skilltree-iac networknot yet checked
  • /data is a volume OpenTofu managesnot yet checked
  • The first run was recordednot yet checked
  • The container was replacednot yet checked
  • OpenTofu applied againnot yet checked
  • The volume was not recreatednot yet checked
  • The check file survivednot yet checked
Esc
↑ ↓ to move↵ to open