Skip to main content
0 reactions

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

The sample Bookshop page open at 127.0.0.1:8080 in a browser, with the heading Welcome to the Bookshop, a New this week banner and four books with their authors and prices.
The sample site, served by nginx in Docker after tofu apply.

You have the source code of the Static Website in Docker with OpenTofu®. This guide takes it from the zip file to your website running in Docker on your own computer. Then it shows how to serve your own files, run the tests and remove everything again.

By the end, nginx serves the sample Bookshop page at http://127.0.0.1:8080/, then your own page, and one command removes the container and the image.

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 Static 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.

macOS and Linux:

Bash
unzip static-website-docker-opentofu-1.0.0.zip

Windows PowerShell (Expand-Archive (opens in a new tab)):

PowerShell
Expand-Archive -Path static-website-docker-opentofu-1.0.0.zip -DestinationPath .

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

Bash
cd static-website-docker-opentofu

The folder holds the OpenTofu files (.tf), a site folder with the sample page, a tests folder and a README that lists every setting.

Step 2: Download the Providers with tofu init

tofu init downloads the two providers the code uses: the Docker provider, which manages the container, and the HTTP provider, which checks the site. The lock file that comes with the code pins their exact versions. What does tofu init do? explains the command.

Bash
tofu init
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!

You may now begin working with OpenTofu. Try running "tofu plan" to see
any changes that are required for your infrastructure. All OpenTofu commands
should now work.

If you ever set or change modules or backend configuration for OpenTofu,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Step 3: Start the Website with tofu apply

Run tofu apply. It shows a plan of what it will create and asks you to confirm. Type yes:

Bash
tofu apply

The end of the plan:

Plain text
Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + container_name = "static-website"
  + image          = (known after apply)
  + url            = "http://127.0.0.1:8080/"
╷
│ Warning: Check block assertion known after apply
│ 
│   on checks.tf line 9, in check "site_responds":
│    9:     condition     = data.http.home.status_code == 200
│     ├────────────────
│     │ data.http.home.status_code is a number
│ 
│ The condition could not be evaluated at this time, a result will be known when this
│ plan is applied.
╵

The warning is expected on the first run. The check that the site answers can only run once the container exists, so OpenTofu runs it during the apply. After you type yes:

Plain text
docker_image.nginx: Creating...
docker_image.nginx: Creation complete after 6s [id=sha256:0759823471bfe675b4dd2e786faed562d1fcd969d2515abf2e0e4804e96612acnginx:1.31-alpine]
docker_container.site: Creating...
docker_container.site: Creation complete after 6s [id=47b80c3b3ec7ab94e18f3ab6b71c4d51c66ee5a4423caef225279c6d9c35c217]
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:

container_name = "static-website"
image = "nginx@sha256:fed7e14be83cbd2661a75cdc5b872d936cf155d46c327084d30b8df19ae88d72"
url = "http://127.0.0.1:8080/"

OpenTofu downloaded nginx, started the container and waited until nginx answered. The url output is the address of your website.

Step 4: Open the Website

Open http://127.0.0.1:8080/ in your browser. nginx serves the sample Bookshop page from the container:

The sample Bookshop page open at 127.0.0.1:8080 in a browser, with the heading Welcome to the Bookshop, a New this week banner and four books with their authors and prices.
The sample site, served by nginx in Docker after tofu apply.

Only your computer can open this address. The FAQ shows how to open it to other devices on your network.

Step 5: Serve Your Own Website

Put your own website’s files in the site folder, in place of the sample files. Keep an index.html at the top of the folder: it’s the home page, and the code checks that it’s there. Then run tofu apply again and type yes:

Bash
tofu apply

The plan says docker_container.site must be replaced, because the files are copied in when a container is created. Update in place vs replace explains why. The end of the output:

Plain text
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Outputs:

container_name = "static-website"
image = "nginx@sha256:fed7e14be83cbd2661a75cdc5b872d936cf155d46c327084d30b8df19ae88d72"
url = "http://127.0.0.1:8080/"

Refresh the browser. nginx now serves your page:

A simple page with the heading My Website and the line My own page, served by nginx in Docker, open at 127.0.0.1:8080 after the files in the site folder were replaced.
Your own files, served after a second tofu apply.

Tip: To keep the sample and serve another folder instead, set site_dir in a terraform.tfvars file. The README lists every setting, and terraform.tfvars.example has them all.

Step 6: Run the Tests

tofu test runs the seven tests that come with the code. They use mocked providers, so they create nothing and don’t need Docker:

Bash
tofu test
Plain text
tests/plan.tftest.hcl... pass
  run "defaults"... pass
  run "open_to_network"... pass
  run "rejects_privileged_port"... pass
  run "rejects_bad_container_name"... pass
  run "rejects_bad_address"... pass
  run "rejects_folder_without_index"... pass
  run "rejects_other_images"... pass

Success! 7 passed, 0 failed.

The first two tests check the defaults and the option that opens the site to your network. The other five check that wrong settings are refused before anything is created.

Clean Up

Run tofu destroy and type yes. It removes the container and deletes the nginx image from Docker:

Bash
tofu destroy
Plain text
docker_container.site: Destroying... [id=3e7974ac0045af27a18c14471c68c590258029ea8f3e03e37e92f715b7432b45]
docker_container.site: Destruction complete after 0s
docker_image.nginx: Destroying... [id=sha256:0759823471bfe675b4dd2e786faed562d1fcd969d2515abf2e0e4804e96612acnginx:1.31-alpine]
docker_image.nginx: Destruction complete after 0s

Destroy complete! Resources: 2 destroyed.

Your folder keeps the code, ready for the next tofu apply. How to remove OpenTofu resources with tofu destroy covers the command in detail.

Common Mistakes

MistakeWhat happensDo this instead
Changing a file in site without running tofu applyThe site keeps showing the old files: they’re copied in when the container is created.Run tofu apply after every change.
Removing index.html from the site foldertofu apply stops before it creates anything: Resource precondition failed … The site folder ./site has no index.html.Keep an index.html at the top of the folder.
Deleting the folder before tofu destroyThe state file goes with it, so OpenTofu no longer knows about the container and the image.Run tofu destroy first, then delete the folder.

Troubleshooting

ProblemWhyFix
Error pinging Docker server … Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?Docker isn’t running, or, on a Mac, the default Docker socket is switched off.Start Docker Desktop and wait until it’s running. On a Mac, switch on the setting from Prerequisites.
Bind for 127.0.0.1:8080 failed: port is already allocatedAnother Docker container already uses port 8080.Set host_port to a free port (FAQ), run tofu apply and open that port instead.
ports are not available: exposing port TCP 127.0.0.1:8080 … bind: address already in useAnother program on your computer already uses port 8080.Set host_port to a free port (FAQ), run tofu apply and open that port instead.

FAQ

Does it work on Windows and Linux?

The code and its lock file cover macOS, Linux and Windows, and the OpenTofu commands are the same on all three. This guide was run on macOS with Docker Desktop.

How do I use another port?

Copy terraform.tfvars.example to terraform.tfvars, set host_port to a free port such as 8081, and run tofu apply. Ports from 1024 to 65535 are allowed.

Can other devices on my network open the site?

Yes, if you set bind_address to 0.0.0.0 in terraform.tfvars. Docker then publishes the port on every network interface of your computer, so anyone on your network can open it (Docker’s port publishing docs (opens in a new tab)). Keep the default, 127.0.0.1, unless you need that.

Does it cost anything to run?

No. OpenTofu, the providers and nginx are free, and everything runs on your computer. Docker Desktop’s license terms are in the Cost note at the top.

Conclusion

You unzipped the code, started the website with tofu init and tofu apply, served your own files, ran the tests and removed everything with tofu destroy. To learn the ideas behind the code, follow the free OpenTofu Tutorial for Beginners: Part 3 builds a simpler version step by step.

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