DockerCommandLineCodeExecutor

DockerCommandLineCodeExecutor(
    image: str = 'python:3-slim',
    container_name: str | None = None,
    timeout: int = 60,
    work_dir: Path | str = PosixPath('.'),
    bind_dir: Path | str | None = None,
    auto_remove: bool = True,
    stop_container: bool = True,
    execution_policies: dict[str, bool] | None = None
)

(Experimental) A code executor class that executes code blocks and returns the result.

(Experimental) A code executor class that executes code through a command line environment in a Docker container.

The executor first saves each code block in a file in the working directory, and then executes the code file in the container. The executor executes the code blocks in the order they are received. Currently, the executor only supports Python and shell scripts. For Python code, use the language “python” for the code block. For shell scripts, use the language “bash”, “shell”, or “sh” for the code block.

Parameters:
NameDescription
imageDocker image to use for code execution.

Defaults to “python:3-slim”.

Type: str

Default: ‘python
container_nameName of the Docker container which is created.

If None, will autogenerate a name.

Defaults to None.

Type: str | None

Default: None
timeoutThe timeout for code execution.

Defaults to 60.

Type: int

Default: 60
work_dirThe working directory for the code execution.

Defaults to Path(”.”).

Type: Path | str

Default: PosixPath(’.’)
bind_dirThe directory that will be bound to the code executor container.

Useful for cases where you want to spawn the container from within a container.

Defaults to work_dir.

Type: Path | str | None

Default: None
auto_removeIf true, will automatically remove the Docker container when it is stopped.

Defaults to True.

Type: bool

Default: True
stop_containerIf true, will automatically stop the container when stop is called, when the context manager exits or when the Python process exits with atext.

Defaults to True.

Type: bool

Default: True
execution_policiesType: dict[str, bool] | None

Default: None

Class Attributes

DEFAULT_EXECUTION_POLICY



LANGUAGE_ALIASES



Instance Attributes

bind_dir


(Experimental) The binding directory for the code execution container.

code_extractor


(Experimental) Export a code extractor that can be used by an agent.

timeout


(Experimental) The timeout for code execution.

work_dir


(Experimental) The working directory for the code execution.

Instance Methods

execute_code_blocks

execute_code_blocks(self, code_blocks: list[CodeBlock]) -> autogen.coding.base.CommandLineCodeResult

(Experimental) Execute the code blocks and return the result.

Parameters:
NameDescription
code_blocksThe code blocks to execute.

Type: list[CodeBlock]
Returns:
TypeDescription
autogen.coding.base.CommandLineCodeResultCommandlineCodeResult: The result of the code execution.

restart

restart(self) -> None

(Experimental) Restart the code executor.


stop

stop(self) -> None

(Experimental) Stop the code executor.