CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-dockerpty

Python library to use the pseudo-tty of a docker container

Pending
Overview
Eval results
Files

main-entry-points.mddocs/

Main Entry Points

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.

Capabilities

Container PTY Startup

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)

Exec Command Execution

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')

Exec Session Startup

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'])

Important Notes

  • All these functions will hijack the current terminal until the container or command exits
  • When dockerpty starts, control is yielded to the container's PTY
  • Press C-p C-q to detach from the container while keeping it running
  • These are safe operations - all resources are restored to their original states on exit
  • For non-TTY containers, dockerpty can still stream output but cannot provide interactive control

Install with Tessl CLI

npx tessl i tessl/pypi-dockerpty

docs

container-operations.md

core-pty-management.md

index.md

main-entry-points.md

stream-management.md

terminal-control.md

tile.json