Static Website in Docker with OpenTofu
1 回の tofu apply で、静的 Web サイトを Docker 上の nginx から配信。設定の検証、ヘルス チェック、7 つのテスト、すべてをきれいに削除する tofu destroy 付き。
- OpenTofu
- Docker provider (kreuzwerker/docker)
- HTTP provider (hashicorp/http)
- nginx (alpine image)
- Docker Desktop
スクリーンショット
買い切り価格
- ₹299+GST(合計 ₹352.82)
- $5
- お支払いが確認されると、すぐにライブラリからダウンロードできます。
- このプロジェクトの今後のリリースもすべて、追加費用なしで含まれます。
- 最新リリース
- 1.0.0 · 2026/11/18
- ライセンス
- Source Code Licence
概要
自分のコンピューター上で、静的ファイルのフォルダーを Docker 上の nginx から配信する OpenTofu 構成の完全なソース コードです。何かを作成する前にすべての設定を検証し、apply は nginx が応答して初めて完了します。チェックでサイトが 200 を返すことを確認し、7 つのテストは Docker なしで実行できます。macOS と Docker Desktop でテスト済みです。
静的サイトをコンテナーに入れるのは、一度きりなら簡単です。しかし、間違った設定を何かが起動する前に見つけ、サイトが本当に応答するまでコマンドが待ち、1 つのコマンドですべてを再び削除できるようにするには、もっと手間がかかります。Static Website in Docker with OpenTofu® は、まさにそれを実現する構成で、macOS と Docker Desktop での実際の実行でテストしています。
できること
- Docker 上の nginx からサイトを配信。
index.htmlがあるフォルダーを指定すると、サブフォルダーも含めたすべてのファイルが nginx コンテナーにコピーされます。 - 起動も削除もコマンド 1 つ。
tofu applyは公式のnginx:1.31-alpineイメージをダウンロードしてコンテナーを起動します。tofu destroyはコンテナーとイメージを削除します。 - 既定では非公開。 サイトは
127.0.0.1:8080で待ち受けるため、開けるのは自分のコンピューターだけです。設定を 1 つ変えれば、ネットワークに公開できます。 - ファイルを変更すると更新。 ファイルを変更して
tofu applyをもう一度実行すると、コンテナーが新しいファイルを含むものに置き換わります。
ミスを早く見つける設計
- すべての設定を最初に検証。 コンテナー名、サイトのフォルダー、ポート(1024~65535)、アドレス、イメージは、何かを作成する前に検証され、それぞれにわかりやすいメッセージが表示されます。
- apply はサイトが正常になるまで待機。 コンテナー内のヘルス チェックが nginx にホーム ページを要求し、
tofu applyは nginx が応答して初めて完了します。 - apply のたびにチェック。
checkブロックがサイトにリクエストを送り、200 で応答しない場合は警告します。 - 7 つのテスト。
tofu testで、既定値、ネットワークのオプション、すべての入力チェックを確認します。プロバイダーはモック化されているため、テストに Docker は不要で、何も作成されません。
拡張しやすいコード
- 小さく読みやすい。 短い
.tfファイルが 6 つあり、それぞれが 1 つの役割(バージョン、プロバイダー、設定、リソース、チェック、出力)を担います。 - バージョン固定で再現可能。 OpenTofu と 2 つのプロバイダーのバージョン制約に加え、macOS(Apple silicon と Intel)、Linux(x86-64 と Arm)、Windows 用のチェックサムを含むロック ファイルがあります。
- すぐに Git で管理可能。 ステートとプランを除外する
.gitignoreと、改行コードをそろえる.gitattributesがあります。 - すべての設定を 1 つのサンプル ファイルに。
terraform.tfvars.exampleをコピーして、必要な部分を変更するだけです。
含まれないもの
- ホスティングは含まれません。 サイトは自分のコンピューター上で動きます。サーバーやクラウドへの配置はお任せします。
- HTTPS はありません。 nginx は、選んだポートで通常の HTTP を配信します。
- サイト ビルダーはありません。 渡されたファイルをそのまま配信します。サンプルは 1 ページのショップです。
4 つの手順で実行
- OpenTofu 1.13 以降をインストールし、Docker を起動します。Mac では Docker Desktop の既定のソケットを有効にするか、
docker_hostを設定します。 - ダウンロードしたファイルを展開し、そのフォルダーでターミナルを開きます。
tofu init、続いてtofu applyを実行し、yesと入力します。- ブラウザーで
http://127.0.0.1:8080/を開きます。
How to Set Up and Run the Static Website in Docker with OpenTofu (新しいタブで開きます)(英語)では、すべての手順をスクリーンショット付きで紹介しています。
コードの背景にある考え方は、無料の OpenTofu Tutorial for Beginners (新しいタブで開きます)(英語)で学べます。
OpenTofu® は LF Projects, LLC の登録商標です。Docker は Docker, Inc. の商標です。その他の名称は、それぞれの所有者の商標です。
無料チュートリアルシリーズ
OpenTofu Tutorial for Beginners
作りながら学びたい方へ。ブログの無料ステップガイドで、このプロジェクトの作り方を順を追って紹介しています。
手に入るもの
アーカイブの中身が一目でわかります。
- 完全なソース コードのバージョン 1.0.0: OpenTofu ファイル 6 つ、サンプル サイト、テスト ファイル
- コンテナー名、サイトのフォルダー、ポート、アドレス、nginx イメージを検証する設定
- ヘルス チェック付きの nginx コンテナーと、サイトが応答するまで待つ apply
- apply のたびに、サイトが 200 で応答することを確認する check ブロック
- モック化したプロバイダーを使い、Docker なしで実行できる 7 つのテスト
- macOS、Linux、Windows 用のプロバイダーのチェックサムを含むロック ファイル
- 設定のサンプル ファイル、.gitignore、.gitattributes ファイル
- 要件、すべての設定、出力、トラブルシューティング、クリーンアップをまとめた README
使用している技術
開発と動作確認に使った実行環境とバージョンです。
- OpenTofu1.13.9
- Docker provider (kreuzwerker/docker)4.7.0
- HTTP provider (hashicorp/http)3.6.3
- nginx (alpine image)1.31
- Docker Desktop4.99.0
アーカイブの中身
展開したあとに手元に残るファイル構成です。
static-website-docker-opentofu/
- site/
- tests/
- .gitattributes
- .gitignore
- .terraform.lock.hcl
- README.md
- checks.tf
- main.tf
- outputs.tf
- providers.tf
- terraform.tfvars.example
- variables.tf
- versions.tf
必要になるもの
動かすために必要なアカウントやサービスです。あとから驚くことはありません。
- OpenTofu 1.13 以降(2.0 未満)
- Docker: macOS または Windows では Docker Desktop、Linux では Docker Engine
- Mac の場合: 有効にした Docker Desktop の既定のソケット、または docker_host に設定した Docker のアドレス
- コンピューター上の空きポート(既定は 8080)
- 最上位に index.html がある Web サイトのファイル(サンプル サイト付属)
リリース
これまでのすべてのバージョンを新しい順に並べています。
1.0.0最新
完全なソース コードの最初のリリースです。
- 静的ファイルの任意のフォルダーを、Docker 上の nginx 1.31 から配信します(既定は 127.0.0.1:8080)。
- 何かを作成する前にすべての設定を検証し、サイトが応答するまで待ちます。
- 7 つのテスト、サンプル サイト、macOS、Linux、Windows 用のロック ファイルを含みます。
Source Code Licence
利用できる範囲
What you may and may not do with the source code you buy.
ライセンスの全文を読む
- What this licence covers
This licence governs every file included in a product you buy from Dewiride Store: source code, configuration, database scripts, sample data, documentation and any other file in the download. It takes effect when your payment is confirmed and applies to every release of that product that reaches your library. Files that reach your library through an annual updates membership are governed in exactly the same way.
It forms part of the Terms of Sale and Use. Where a product page states different or additional licence terms for that product, those terms prevail for that product. The membership page may likewise state different or additional licence terms for the files a membership carries, and those terms then prevail for those files.
- The licence we grant you
We grant you a perpetual, worldwide, non-exclusive, non-transferable licence to use, reproduce, adapt, translate and modify the files, and to include the result in your own work, on the conditions set out below.
Perpetual means it does not expire. Worldwide means it is not limited to any country. Non-exclusive means we license the same product to other customers. Non-transferable means you cannot pass it on to someone else.
The licence is granted to you — either you personally, or the organisation you identified when buying. Everyone working within that organisation may use the files for that organisation’s projects. Nobody outside it may.
- What you may do
- Use and change the files in your own projects, commercial or otherwise, with no limit on the number of projects.
- Build a product or a service on them and charge your own customers for it.
- Deploy the result for a client, as part of work you are doing for that client.
- Distribute the result in compiled, deployed or otherwise non-source form.
- Keep as many back-up copies as you reasonably need.
- Study, observe and test how the code works, and obtain the information you need to make it work with your own software.
The last two are yours by law under sections 52(1)(aa) to (ac) of the Indian Copyright Act, 1957, and nothing in this licence is intended to take them away.
- What you may not do
- Resell, sublicense, redistribute or publish the files themselves, in whole or in substantial part, in source form.
- Include them in a template, starter kit, course, tutorial repository or component library that you sell or give away, where the code is the thing being supplied.
- Share your account, your download links or your invoices with anyone outside your organisation.
- Publish the files in a public repository, or in any other place where they can be obtained without buying them.
- Remove or obscure the copyright and attribution notices in the source files.
- Use the files to build something whose main purpose is to compete with Dewiride Store as a supplier of the same code.
The distinction that matters is simple: you may sell what you build with the code; you may not sell the code.
- Third-party components
Our products depend on open-source libraries, frameworks and services that we did not write. Those remain under their own licences, which we do not and cannot change. Where a product includes or requires such a component, its licence is named in the product’s documentation, and complying with it is your responsibility.
- Releases
Every release of a product you own is covered by this same licence and appears in your library at no extra cost. A release you have already downloaded remains licensed to you even if we later stop publishing the product. A release that reached your library through an annual updates membership is licensed to you on these same terms, and stays licensed after the period that carried it has ended.
- Ownership
We keep ownership of the files and of the copyright in them. You own whatever you create with them: your changes, your additions and the product you build are yours.
- If this licence ends
This licence ends if you materially breach it and do not put the breach right within thirty days of us asking you to in writing. It also ends if your payment is reversed or refunded, because the purchase it depends on no longer stands. Where a membership period is refunded or reversed, the licence ends for the releases that period carried and for nothing else: anything you bought separately, or that another period carried, is unaffected.
If it ends, you must stop using the files and delete the copies you hold. Work you had already delivered to a client before that point may continue in use, so that ending this licence never breaks something your own customer relies on.
- No warranty beyond the description
The warranties and limits in section 19 and section 20 of the Terms of Sale and Use apply to this licence as well.
- Governing law
This licence is governed by the law of India, and section 24 of the Terms of Sale and Use applies to any dispute about it. If a translation and the English version differ in meaning, the English version governs.
Static Website in Docker with OpenTofu
- ₹299+GST(合計 ₹352.82)
- $5