Skip to main content
0 reactions

How to Set Up and Run the Catalogue Website in Docker with OpenTofu

Card titled Set Up the Catalogue Website, labelled Catalogue Website Setup Guide, with four ticked steps: tofu init, tofu apply, open 127.0.0.1:8080 and tofu destroy.
From the zip to your own catalogue in Docker with tofu init and tofu apply

You have the source code of the Catalogue Website in Docker with OpenTofu®. This guide takes it from the zip file to a catalogue website running in Docker on your own computer, then shows how to replace the sample list with your own items and how to remove everything again.

By the end, nginx serves your catalogue at http://127.0.0.1:8080/, with an “in stock” or “sold out” label after every item, and one command removes it.

Every command and output on this page is real, run on OpenTofu 1.13 with Docker Desktop on macOS. The OpenTofu commands are the same in Windows PowerShell and on Linux.

Cost: free to run. Everything runs on your own computer. Docker Desktop is free for personal use, education, non-commercial open source projects and small businesses; larger organizations need a paid subscription (Docker Desktop license agreement (opens in a new tab)).

Get the Source Code

Get the complete source code of Catalogue Website in Docker with OpenTofu on Dewiride Store → (opens in a new tab)
The archive holds everything this guide sets up and runs.

Prerequisites

Step 1: Unzip the Source Code

Open a terminal in the folder that holds the zip, such as Downloads, and unzip it. On Windows, you can also right-click the zip in File Explorer and choose Extract All.

TerminalBash
unzip catalogue-website-docker-opentofu-1.0.0.zip

It creates a folder named catalogue-website-docker-opentofu. Go into it:

TerminalBash
cd catalogue-website-docker-opentofu

The folder holds the OpenTofu files (.tf), a templates folder with the page template and its style sheet, an example settings file and a README that lists every setting.

Step 2: Download the Providers with tofu init

tofu init downloads the two providers the code uses, at the exact versions recorded in its lock file (what tofu init does):

TerminalBash
tofu init
Output (the lines that matter)Plain text
Initializing the backend...

Initializing provider plugins...
- Reusing previous version of kreuzwerker/docker from the dependency lock file
- Reusing previous version of hashicorp/http from the dependency lock file
- Installing kreuzwerker/docker v4.7.0...
- Installing hashicorp/http v3.6.3...
- Installed hashicorp/http v3.6.3 (signed, key ID 0C0AF313E5FD9F80)
- Installed kreuzwerker/docker v4.7.0 (signed, key ID 0DCE698927DAF8EC)

Providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://opentofu.org/docs/cli/plugins/signing/

OpenTofu has been successfully initialized!

Step 3: Start the Website with tofu apply

Run tofu apply. It shows its plan, an nginx image and a container, and asks you to confirm. Type yes:

TerminalBash
tofu apply
Output (the lines that matter)Plain text
Plan: 2 to add, 0 to change, 0 to destroy.

docker_image.nginx: Creating...
docker_image.nginx: Creation complete after 0s [id=sha256:7f2d42982b61384c4808f31f7215d19db7ceed3056dc680b84be5790b0cb4c39nginx:1.31-alpine]
docker_container.site: Creating...
docker_container.site: Creation complete after 6s [id=bfb0625eaadc3e53453d3cedfa4bc860fb7cd5c6cf05170dde4b56815cee8674]
data.http.home: Reading...
data.http.home: Read complete after 0s [id=http://127.0.0.1:8080/]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Outputs:

available_items = [
  "The Paper Lantern Café",
  "A Map of Quiet Places",
  "Salt and Starlight",
]
container_name = "catalogue-website"
image = "nginx@sha256:7fc0fb4af7bf006aeb31aea88069976060f8f0a0a86b667126742cc17f4e31ae"
item_names = tolist([
  "The Paper Lantern Café",
  "A Map of Quiet Places",
  "The Clockmaker's Garden",
  "Salt and Starlight",
])
url = "http://127.0.0.1:8080/"

tofu apply finishes only when nginx answers inside the container, and a check then confirms that the website answers too. The outputs list every item and the ones that are available.

Step 4: Open the Website

Open http://127.0.0.1:8080/ in your browser. The sample catalogue is a small bookshop:

A web browser showing the catalogue website at 127.0.0.1 port 8080: the title Riverside Bookshop, the line New this week, and four books with their authors, three labelled in stock in green and The Clockmaker’s Garden labelled sold out in orange.
The sample catalogue, right after the first tofu apply.

Step 5: Make the Catalogue Yours

Your own items go in a file named terraform.tfvars, which OpenTofu reads by itself (.tfvars vs .auto.tfvars). Start from the example file that comes with the code. On Windows, copy it in File Explorer and name the copy terraform.tfvars:

TerminalBash
cp terraform.tfvars.example terraform.tfvars

Open it in any text editor. The example is a small bakery:

terraform.tfvars (excerpt)HCL
site_title = "Corner Bakery"
intro      = "Baked fresh every morning."

items = [
  { name = "Sourdough loaf", detail = "Slow-rised for 24 hours" },
  { name = "Lemon tart", detail = "With a crisp butter base" },
  { name = "Plum cake", detail = "Back in autumn", available = false },
]

available_label   = "available"
unavailable_label = "sold out"
  • site_title is the heading, and intro the line under it. An empty intro shows no line.
  • Each item in items needs a name. detail adds a second line, and available = false marks it as sold out.
  • available_label and unavailable_label are the two labels.

Change the items to your own, then apply again and type yes. The new page replaces the container (update in place vs replace):

TerminalBash
tofu apply
Output (the lines that matter)Plain text
Plan: 1 to add, 0 to change, 1 to destroy.

Outputs:

available_items = [
  "Sourdough loaf",
  "Lemon tart",
]
container_name = "catalogue-website"
image = "nginx@sha256:7fc0fb4af7bf006aeb31aea88069976060f8f0a0a86b667126742cc17f4e31ae"
item_names = tolist([
  "Sourdough loaf",
  "Lemon tart",
  "Plum cake",
])
url = "http://127.0.0.1:8080/"

Refresh the browser:

The same website after the example settings were applied: the title Corner Bakery, the line Baked fresh every morning, Sourdough loaf and Lemon tart labelled available, and Plum cake labelled sold out.
The catalogue built from terraform.tfvars, with your own items.

OpenTofu checks every setting first. Two items with the same name, an empty name or a port outside 1024 to 65535 stop the apply with a message that says which setting is wrong.

Step 6: Read the Lists with tofu output

The outputs stay available after the apply. To print just the available items (tofu output -raw and -json shows more):

TerminalBash
tofu output available_items
OutputPlain text
[
  "Sourdough loaf",
  "Lemon tart",
]

Clean Up

Run tofu destroy and type yes. It removes the container and the nginx image (tofu destroy):

TerminalBash
tofu destroy
Output (the lines that matter)Plain text
Plan: 0 to add, 0 to change, 2 to destroy.

Destroy complete! Resources: 2 destroyed.

Your terraform.tfvars stays in the folder, so the next tofu apply builds the same catalogue again. To keep the nginx image after a destroy, set keep_image = true.

Common Mistakes

MistakeWhat happensDo this instead
Editing terraform.tfvars.example instead of a copyOpenTofu doesn’t read the example file, so the sample books stay.Copy it to terraform.tfvars and edit the copy.
Forgetting the comma between two itemsOpenTofu stops with a syntax error and the line number.End every item with },, as in the example.
Leaving out availableThe item counts as available.Add available = false to the items that are sold out.

Troubleshooting

ProblemWhyFix
Cannot connect to the Docker daemonDocker isn’t running, or on a Mac the default socket is switched off.Start Docker Desktop. On a Mac, switch on the default socket (Prerequisites).
port is already allocated or address already in useAnother program already uses port 8080.Add host_port = 8081 to terraform.tfvars and open the address the url output prints.
Item names must be unique (ignoring upper and lower case).Two items have the same name.Rename one of them.
The page still shows the old listThe browser shows a cached copy.Reload the page without the cache.

FAQ

Can I change how the page looks?

Yes. The page is templates/index.html.tftpl and its style is templates/style.css. Edit them and run tofu apply. The page is filled in with OpenTofu’s templatefile function (opens in a new tab).

Is the website open to other computers?

No. It listens on 127.0.0.1, so only your computer can open it. Set bind_address = "0.0.0.0" to open it to your network.

Can a name contain & or <?

Yes. The code escapes &, < and > before it writes the page, so Tea & Biscuits shows as written.

Conclusion

You unzipped the code, started the catalogue with tofu init and tofu apply, replaced the sample with your own items in terraform.tfvars, and removed it all with tofu destroy. To learn how the code builds the list, read Part 5 of the OpenTofu Tutorial.

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Docker is a trademark of Docker, Inc. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

0 comments