The portainer_container_exec resource allows you to remotely execute a command inside a running container managed by Portainer.
You can target a container in a standalone or swarm environment.
resource "portainer_container_exec" "standalone" {
endpoint_id = 1
service_name = "my-nginx-container"
command = "nginx -v"
user = "root"
}resource "portainer_container_exec" "swarm_exec" {
endpoint_id = 2
service_name = "my-service-name"
command = "ls -la /etc"
user = "root"
mode = "swarm"
}This resource is stateless β it runs once when terraform apply is called.
π‘ Pro Tip: You can output the result like this:
output "exec_output" {
value = portainer_container_exec.standalone.output
}| Name | Type | Required | Description |
|---|---|---|---|
endpoint_id |
int | β yes | ID of the Portainer environment |
service_name |
string | β yes | Name of the container (for standalone) or service (for swarm) |
command |
string | β yes | Command to execute inside the container |
user |
string | π« optional | User to run the command as (default: "root:root") |
wait |
int | π« optional | Seconds to wait before executing the command (default: 0) |
mode |
string | π« optional | Deployment type: "standalone" (default) or "swarm" |
| Name | Description |
|---|---|
id |
ID of the execution instance |
output |
Output (stdout/stderr) from the executed command |