|
| 1 | +# ✅ **Resource Documentation: `portainer_check`** |
| 2 | + |
| 3 | +# portainer_check |
| 4 | + |
| 5 | +The `portainer_check` resource validates that one or more containers (in standalone mode) or services (in swarm mode) are running with the **expected image revision (tag)** and **desired runtime state** in a Portainer-managed environment. |
| 6 | + |
| 7 | +> You can use it in both **Docker Standalone** and **Docker Swarm** deployments. |
| 8 | +> It’s especially useful for CI/CD pipelines to verify that a deployment or update has completed successfully before proceeding to the next step. |
| 9 | +
|
| 10 | +--- |
| 11 | + |
| 12 | +## 🚀 Example Usage |
| 13 | +- [Example on GitHub](https://github.com/portainer/terraform-provider-portainer/tree/main/examples/deployment) |
| 14 | + |
| 15 | +### ✅ Check service status in Docker Swarm |
| 16 | + |
| 17 | +```hcl |
| 18 | +resource "portainer_check" "swarm_check" { |
| 19 | + endpoint_id = 1 |
| 20 | + stack_name = "my-swarm-stack" |
| 21 | + services_list = "web,api" |
| 22 | + revision = "1.29" |
| 23 | + desired_state = "running" |
| 24 | + max_retries = 3 |
| 25 | + wait = 10 |
| 26 | + wait_between_checks = 5 |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### ✅ Check container status in Docker Standalone |
| 31 | + |
| 32 | +```hcl |
| 33 | +resource "portainer_check" "standalone_check" { |
| 34 | + endpoint_id = 1 |
| 35 | + stack_name = "nginx" |
| 36 | + services_list = "web" |
| 37 | + revision = "1.29" |
| 38 | + desired_state = "running" |
| 39 | + wait = 10 |
| 40 | + max_retries = 3 |
| 41 | + wait_between_checks = 5 |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## ⚙️ Lifecycle & Behavior |
| 48 | + |
| 49 | +This resource is **stateless** — it performs runtime verification during `terraform apply` (or `tofu apply` for OpenTofu) and does **not** persist state in Portainer. |
| 50 | + |
| 51 | +When executed: |
| 52 | + |
| 53 | +1. It waits for the optional `wait` period before starting. |
| 54 | +2. It determines whether the target environment is **Swarm** or **Standalone**. |
| 55 | +3. It checks the matching services or containers for: |
| 56 | + |
| 57 | + * Correct **image tag** (`revision`) |
| 58 | + * Correct **state** (e.g., `running`) |
| 59 | +4. If all targets match → ✅ success. |
| 60 | + Otherwise → ❌ fails after `max_retries`. |
| 61 | + |
| 62 | +> 💡 **Pro Tip:** Combine `portainer_check` after a `portainer_deploy` or `portainer_container_exec` to ensure deployment integrity. |
| 63 | +
|
| 64 | +--- |
| 65 | + |
| 66 | +## 📥 Arguments Reference |
| 67 | + |
| 68 | +| Name | Type | Required | Description | |
| 69 | +| --------------------- | ------ | --------------------------------- | ------------------------------------------------------------------------------------- | |
| 70 | +| `endpoint_id` | int | ✅ yes | ID of the Portainer environment (endpoint) where the stack or containers are located. | |
| 71 | +| `stack_name` | string | ✅ yes | Name of the stack to which the containers or services belong. | |
| 72 | +| `revision` | string | ✅ yes | Expected image tag (e.g., `"1.29"`) that should be currently running. | |
| 73 | +| `services_list` | string | ✅ yes | Comma-separated list of service names (without stack prefix). Example: `"web,api"`. | |
| 74 | +| `desired_state` | string | 🚫 optional (default `"running"`) | Desired container/service state. Usually `"running"`. | |
| 75 | +| `wait` | int | 🚫 optional (default `30`) | Seconds to wait before performing the first check (useful after deploy). | |
| 76 | +| `wait_between_checks` | int | 🚫 optional (default `30`) | Delay (in seconds) between each retry attempt. | |
| 77 | +| `max_retries` | int | 🚫 optional (default `3`) | Number of retry attempts before failing the check. | |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 📤 Attributes Reference |
| 82 | + |
| 83 | +| Name | Description | |
| 84 | +| -------- | --------------------------------------------------------------------------------------------------------------- | |
| 85 | +| `id` | Auto-generated ID of the check execution (stateless). | |
| 86 | +| `output` | The complete textual output of the verification process, including matched containers, retries, and debug info. | |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## 🧩 Example with Outputs |
| 91 | + |
| 92 | +```hcl |
| 93 | +output "check_result" { |
| 94 | + value = portainer_check.standalone_check.output |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +This will show you a detailed report like: |
| 99 | + |
| 100 | +``` |
| 101 | +Docker Standalone detected — using container check logic. |
| 102 | +DEBUG: checking container="nginx-web-1" (image="nginx:1.29", state="running") |
| 103 | +Container "nginx-web-1" OK — revision "1.29", state "running" |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +## 🧠 Summary |
| 109 | + |
| 110 | +| Feature | Description | |
| 111 | +| ----------- | -------------------------------------------------------------------- | |
| 112 | +| Mode | Works in **Standalone** and **Swarm** environments | |
| 113 | +| Purpose | Ensures containers/services run with the correct image tag and state | |
| 114 | +| Behavior | Stateless verification (no Portainer state change) | |
| 115 | +| Typical Use | Post-deployment validation in CI/CD pipelines | |
0 commit comments