|
| 1 | +--- |
| 2 | +title: "Interactive exec" |
| 3 | +description: "Send stdin while polling output, with application heartbeats and deadlines." |
| 4 | +--- |
| 5 | + |
| 6 | +<Note> |
| 7 | +This API is on the `codex/interactive-exec` feature branch. It is not included |
| 8 | +in client `3.1.0`. The existing blocking `execStart()` and `wait()` APIs are unchanged. |
| 9 | +</Note> |
| 10 | + |
| 11 | +Use interactive exec when a command needs stdin or your application must keep |
| 12 | +working while it runs. The session reads and writes without blocking on command |
| 13 | +output. A caller can renew a lock, check cancellation or enforce its own deadline |
| 14 | +between `poll()` calls, even if the command is silent. |
| 15 | + |
| 16 | +## Non-TTY stdin and output |
| 17 | + |
| 18 | +The default `Docker::create()` socket client supports this API. For explicit PHP |
| 19 | +configuration, use `DockerClientFactory::createInteractive()`: |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +require __DIR__ . '/vendor/autoload.php'; |
| 25 | + |
| 26 | +use Docker\API\Model\ContainersIdExecPostBody; |
| 27 | +use Docker\API\Model\ExecIdStartPostBody; |
| 28 | +use Docker\Docker; |
| 29 | +use Docker\DockerClientFactory; |
| 30 | + |
| 31 | +$docker = Docker::create(DockerClientFactory::createInteractive([ |
| 32 | + 'remote_socket' => 'unix:///var/run/docker.sock', |
| 33 | + 'api_version' => '1.45', |
| 34 | + 'timeout' => 2000, |
| 35 | +]), [], [], false); |
| 36 | + |
| 37 | +$command = new ContainersIdExecPostBody(); |
| 38 | +$command->setCmd(['timeout', '-s', 'KILL', '30', 'cat']); |
| 39 | +$command->setAttachStdin(true); |
| 40 | +$command->setAttachStdout(true); |
| 41 | +$command->setAttachStderr(true); |
| 42 | +$command->setTty(false); |
| 43 | +$exec = $docker->containerExec('my-container', $command); |
| 44 | + |
| 45 | +$start = new ExecIdStartPostBody(); |
| 46 | +$start->setTty(false); |
| 47 | +$session = $docker->execStartInteractive($exec->getId(), $start, 35000); |
| 48 | +$session->onStdout(static function (string $chunk): void { |
| 49 | + echo $chunk; |
| 50 | +}); |
| 51 | +$session->onStderr(static function (string $chunk): void { |
| 52 | + fwrite(STDERR, $chunk); |
| 53 | +}); |
| 54 | + |
| 55 | +$input = "{\"example\":true}\n"; |
| 56 | +$offset = 0; |
| 57 | +$stdinClosed = false; |
| 58 | +$nextHeartbeat = 0; |
| 59 | +try { |
| 60 | + while (!$session->isFinished()) { |
| 61 | + $now = hrtime(true); |
| 62 | + if ($now >= $nextHeartbeat) { |
| 63 | + // Renew your application lock here; throw if ownership was lost. |
| 64 | + $nextHeartbeat = $now + 10000000000; |
| 65 | + } |
| 66 | + if ($offset < strlen($input)) { |
| 67 | + $offset += $session->writeStdin(substr($input, $offset, 16384)); |
| 68 | + } elseif (!$stdinClosed) { |
| 69 | + $session->closeStdin(); |
| 70 | + $stdinClosed = true; |
| 71 | + } |
| 72 | + $session->poll(50); |
| 73 | + } |
| 74 | +} finally { |
| 75 | + $session->close(); |
| 76 | +} |
| 77 | + |
| 78 | +$result = $docker->execInspect($exec->getId()); |
| 79 | +if ($result->getRunning() !== false || $result->getExitCode() === null) { |
| 80 | + throw new RuntimeException('The exec result is not known yet; inspect it before retrying'); |
| 81 | +} |
| 82 | +if ($result->getExitCode() !== 0) { |
| 83 | + throw new RuntimeException('The command failed with exit status '.$result->getExitCode()); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Replace `my-container` with a running development container. `timeout` must exist |
| 88 | +inside its image; use the image's own timeout/supervision mechanism if it does not. |
| 89 | + |
| 90 | +`writeStdin()` returns the number of bytes accepted, at most 16 KiB per call. |
| 91 | +Zero means backpressure: poll output, then retry the unsent suffix. The library |
| 92 | +does not retain or queue stdin. Once all input has been sent, `closeStdin()` sends |
| 93 | +EOF without closing the non-TTY output channels. |
| 94 | + |
| 95 | +`poll(50)` reads at most 16 KiB and waits at most 50 milliseconds for data. It |
| 96 | +returns `true` while the connection remains open, including during idle periods; |
| 97 | +`false` means clean output EOF. Callbacks receive chunks, which can split frames |
| 98 | +or lines. Without a callback, that channel is drained and discarded. |
| 99 | + |
| 100 | +If stdin is already complete, `wait($tick, 50)` is a convenience loop. `$tick` |
| 101 | +runs between polls even with no output; it can also send input. Callbacks and |
| 102 | +ticks must return promptly. Their exceptions close the local session and propagate. |
| 103 | + |
| 104 | +## Deadlines and completion |
| 105 | + |
| 106 | +The factory's `timeout` bounds synchronous HTTP setup requests. The third argument |
| 107 | +to `execStartInteractive()` is a separate total streaming deadline in milliseconds, |
| 108 | +starting after the HTTP upgrade. It is not reset when output arrives. Expiry throws |
| 109 | +`Docker\Exception\InteractiveExecTimeoutException` and closes local I/O. |
| 110 | + |
| 111 | +<Warning> |
| 112 | +Closing the stream or reaching a local deadline does not guarantee that Docker |
| 113 | +terminated the command. Keep an in-container timeout for unattended operations. |
| 114 | +Output EOF is not an exit code: inspect `Running` and `ExitCode`, with a bounded |
| 115 | +application deadline if you need to poll for the final result. Never automatically |
| 116 | +retry a command with side effects after an ambiguous disconnect. |
| 117 | +</Warning> |
| 118 | + |
| 119 | +## Transports and TTY |
| 120 | + |
| 121 | +This feature supports the bundled Unix, TCP and TLS socket transport. Guzzle and |
| 122 | +arbitrary PSR-18 clients are rejected before sending an interactive start request. |
| 123 | +HTTP proxies must preserve the HTTP 101 connection upgrade and bidirectional traffic. |
| 124 | + |
| 125 | +TTY sessions support stdin writes and combined output on `onStdout()`. They have |
| 126 | +terminal semantics, not separate stdout/stderr pipes. `closeStdin()` deliberately |
| 127 | +rejects TTY sessions: Docker can close their output when socket stdin reaches EOF. |
| 128 | +Use terminal input or let the command exit; use `Tty=false` for JSON-input managers. |
| 129 | +The `Tty` setting must match at exec creation and start. |
0 commit comments