Documentation
PLEASE NOTE: This document applies to latest version and not to the latest stable release v2.21
Documentation for other releases can be found by using the version selector in the top right of any doc page.docker_exec
Execute commands inside running Docker containers.
Attributes
check_mode:
support: none
Parameters
| Parameter | Required | Type | Values | Description |
|---|---|---|---|---|
| container | true | string | Container name or ID. | |
| command | true | string | Command to execute inside the container. | |
| user | string | User to run the command as (e.g., “user”, “uid”, “user:group”, “uid:gid”). | ||
| workdir | string | Working directory inside the container. | ||
| env | object | Environment variables as a dictionary. | ||
| detach | boolean | Run command in the background (detached mode). | ||
| tty | boolean | Allocate a pseudo-TTY. | ||
| stdin | boolean | Keep STDIN open even if not attached. |
Example
- name: Run a simple command in a container
docker_exec:
container: myapp
command: ls /app
- name: Run script in container as specific user
docker_exec:
container: myapp
command: /scripts/update.sh
user: appuser
workdir: /app
- name: Run command with environment variables
docker_exec:
container: webapp
command: env
env:
DEBUG: "true"
LOG_LEVEL: info
- name: Run interactive command with TTY
docker_exec:
container: myapp
command: /bin/bash
tty: true
stdin: true
- name: Run command in background
docker_exec:
container: myapp
command: /scripts/background_task.sh
detach: true