Skip to main content
ConTree SDK provides multiple ways to execute commands in containers, from simple shell commands to complex workflows with file handling and custom I/O.

Basic Command Execution

You can run commands using shell syntax or by specifying command and arguments separately:
See ContreeImage.run() and ContreeSession.run() for all options.

Command Execution Mode

You can execute commands by specifying the executable path and arguments separately:
See ContreeImage.run() and ContreeSession.run() for command execution details.

Command vs Shell Mode

  • Command mode: Use command="/bin/ls" with args=["-la", "/tmp"] for direct execution without shell interpretation
  • Shell mode: Use shell="ls -la /tmp" for shell commands with pipes, redirects, and wildcards
  • Environment variables: Pass env={"VAR": "value"} to set environment for command execution

Preserving Environment Variables

By default, values passed through env are available only to the current command. Set preserve_env=True with disposable=False when those variables should be written into the resulting image and inherited by later commands:
On the ConTree side, preserve_env=True merges the image’s existing metadata/env entries with the env values from the request, with request values taking priority, then writes the merged values back to metadata/env in the resulting image. Setting a variable to an empty string removes it from the preserved environment.

Working with Files

You can upload and use files in your commands by specifying local file paths or pre-uploaded file objects:

File Upload Methods

You can provide files to commands in several ways:
  • Local file paths: files=["/path/to/local/file.txt"] - Upload files directly
  • File mapping: files={"dest.txt": "/local/source.txt"} - Upload with custom names
  • Pre-uploaded files: files={"script.sh": uploaded_file_object} - Use files uploaded via client.files.upload()

Advanced I/O Handling

You can use Python I/O objects for more sophisticated input/output handling:
See run() for I/O parameter details.

Supported I/O Types

  • StringIO: For text-based input/output
  • BytesIO: For binary data handling
  • File objects: Use open() file handles directly
  • PIPE: Capture stderr/stdout as byte streams
  • bytes type: Get output as bytes instead of strings

Subprocess-like Interface (Sync Only)

You can use a subprocess-like interface for more control over process execution:
See ContreeProcessSync for the full subprocess API.

Popen Features

  • Process control: Use wait(), communicate(), and check returncode
  • Environment variables: Pass custom env dictionary
  • Working directory: Set cwd parameter
  • Shell commands: Enable with shell=True
  • Error handling: Check returncode and stderr for failures

Command Parameters

Core Parameters

  • shell: Execute as shell command (e.g., "ls -la | grep txt")
  • command: Executable path (e.g., "/bin/ls")
  • args: Command arguments as tuple (e.g., ("-la", "/tmp"))
  • stdin: Input data (string, bytes, or I/O object)
  • env: Environment variables as dictionary

I/O Parameters

  • stdout: Redirect stdout (StringIO, BytesIO, file path, or bytes)
  • stderr: Redirect stderr (StringIO, BytesIO, PIPE, or bytes)
  • files: Upload files (list of paths or dict mapping)

Execution Parameters

  • cwd: Working directory inside container
  • disposable: Whether to persist changes (default: True for runs, False for sessions)
  • preserve_env: Whether to persist env values into the resulting image environment
  • tag: Tag to assign to the resulting image after execution (e.g. tag="myapp:v2")

Result Objects

Command execution returns result objects with:
  • stdout: Command output as string (or specified type)
  • stderr: Error output as string (or specified type)
  • exit_code: Process exit code (0 = success)
  • uuid: UUID of the resulting image state