Python library to use the pseudo-tty of a docker container
—
High-level functions that provide the primary interface to dockerpty's functionality. These convenience functions wrap the core PseudoTerminal class and handle the most common use cases for Docker container PTY management.
Start a PTY session with a Docker container, taking over the current process' TTY until the container's PTY is closed.
def start(client, container, interactive=True, stdout=None, stderr=None, stdin=None, logs=None):
"""
Present the PTY of the container inside the current process.
This is a wrapper for PseudoTerminal(client, RunOperation(...)).start()
Parameters:
- client: Docker client instance
- container: Container dict or ID
- interactive: bool, whether to enable interactive mode (default: True)
- stdout: file-like object for stdout (default: sys.stdout)
- stderr: file-like object for stderr (default: sys.stderr)
- stdin: file-like object for stdin (default: sys.stdin)
- logs: int, whether to include logs (default: None, shows deprecation warning)
Returns:
None - function takes over terminal until container exits
"""Usage example:
import docker
import dockerpty
client = docker.Client()
container = client.create_container(
image='ubuntu:latest',
stdin_open=True,
tty=True,
command='/bin/bash',
)
# This will hijack the current terminal
dockerpty.start(client, container)Execute a command in an existing container with PTY support.
def exec_command(client, container, command, interactive=True, stdout=None, stderr=None, stdin=None):
"""
Run provided command via exec API in provided container.
This is a wrapper for PseudoTerminal(client, ExecOperation(...)).start()
Parameters:
- client: Docker client instance
- container: Container dict or ID
- command: str or list, command to execute
- interactive: bool, whether to enable interactive mode (default: True)
- stdout: file-like object for stdout (default: sys.stdout)
- stderr: file-like object for stderr (default: sys.stderr)
- stdin: file-like object for stdin (default: sys.stdin)
Returns:
None - function takes over terminal until command completes
"""Usage example:
import docker
import dockerpty
client = docker.Client()
container_id = 'running_container_id'
# Execute bash in the running container
dockerpty.exec_command(client, container_id, '/bin/bash')Start a previously created exec instance with PTY support.
def start_exec(client, exec_id, interactive=True, stdout=None, stderr=None, stdin=None):
"""
Start an exec session using exec_id.
Parameters:
- client: Docker client instance
- exec_id: Exec instance ID (from client.exec_create())
- interactive: bool, whether to enable interactive mode (default: True)
- stdout: file-like object for stdout (default: sys.stdout)
- stderr: file-like object for stderr (default: sys.stderr)
- stdin: file-like object for stdin (default: sys.stdin)
Returns:
None - function takes over terminal until exec completes
"""Usage example:
import docker
import dockerpty
client = docker.Client()
container_id = 'running_container_id'
# Create exec instance
exec_id = client.exec_create(container_id, '/bin/sh', tty=True, stdin=True)
# Start the exec session with PTY
dockerpty.start_exec(client, exec_id['Id'])C-p C-q to detach from the container while keeping it runningInstall with Tessl CLI
npx tessl i tessl/pypi-dockerpty