capabilities.work

Run a Command Inside a Running Container

A 4-step recipe with Docker. We can help you get access.

Recipe · 4 steps

Run a Command Inside a Running Container

with Docker

Execute a command in a running container and capture its exit code.

  1. Confirm the container is running and not paused before creating the exec instance. Exec against a stopped or paused container is rejected with a 409.
  2. Create the exec instance with the supplied command, attaching stdout and stderr so the output is streamed back on start. This call only registers the command; nothing runs yet.
  3. Run the exec instance in attached (non-detached) mode. The response streams the command output but carries no exit status, which is why the flow inspects the exec instance afterwards.
  4. Inspect the exec instance to recover the exit code and confirm the command has finished running. This is the only way to learn whether the command succeeded.
What you'll need to have ready
  • containerId: ID or name of the running container to exec into.
  • cmd: Command to execute, as an argv array (e.g. ["sh", "-c", "ls -la /"]).
  • user: User (and optionally group) to run the command as, e.g. "root" or "1000:1000".
  • workingDir: Working directory for the command inside the container.
  • env: Environment variables for the exec, in "KEY=value" form.
For your developer

Arazzo workflow docker:exec-command-in-container, 4 operations: ContainerInspect → ContainerExec → ExecStart → ExecInspect. Specs on APIs.io.