Sessions¶
Sessions provide persistent shell connections to a container. Unlike run(), which creates a new exec instance for each command, a session maintains a single long-lived shell process with preserved state (working directory, environment variables, shell history).
Create a Session¶
The session opens a /bin/sh shell inside the container and keeps it alive until explicitly closed or the container shuts down.
Send and Wait¶
Send a command and wait for it to complete:
result = sess.send_and_wait("echo hello")
result.stdout # "hello\n"
result.exit_code # 0
result.ok # True
result.duration_ms # 12.3
With a timeout:
Fire-and-Forget¶
Send a command without waiting for output:
State persists across commands:
Read Accumulated Output¶
Drain any output that has accumulated since the last read:
sess.send("echo line1")
sess.send("echo line2")
import time; time.sleep(0.1)
output = sess.read()
print(output) # "line1\nline2\n"
read() is thread-safe and clears the buffer.
Output Callback¶
Register a callback for session output:
The callback receives all output from the shell, including command echoes.
Close¶
Close the session without stopping the container:
After closing, any further operations raise SessionClosed:
from pocketdock import SessionClosed
sess.close()
try:
sess.send("echo hello")
except SessionClosed:
print("Session is closed")
Session Properties¶
| Property | Type | Description |
|---|---|---|
id |
str |
Exec instance ID backing the session |
Engine Support¶
Sessions work on both Podman and Docker. The exec start request asks the engine to upgrade (hijack) the connection, which Docker requires before it forwards stdin to the shell. pocketdock 1.2.6 and earlier did not do this, so sessions on Docker timed out with empty output.
Sentinel Protocol¶
pocketdock uses a sentinel protocol to reliably detect command boundaries and exit codes. After each send_and_wait() command, the session injects a sentinel marker (__PD_{uuid}_${exit_code}__) to determine where one command's output ends and the next begins. This is transparent to the user.
Automatic Cleanup¶
Sessions are automatically closed when the container shuts down: