Skip to content

Commit

Permalink
feat: add bind_dir arg to DockerCommandLineExecutor + docs update (#2309
Browse files Browse the repository at this point in the history
)

* add bind_dir arg and update docs

* lint
  • Loading branch information
gbrvalerio authored Apr 30, 2024
1 parent bcb6117 commit a0fffc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 16 additions & 1 deletion autogen/coding/docker_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(
container_name: Optional[str] = None,
timeout: int = 60,
work_dir: Union[Path, str] = Path("."),
bind_dir: Optional[Union[Path, str]] = None,
auto_remove: bool = True,
stop_container: bool = True,
):
Expand All @@ -67,6 +68,9 @@ def __init__(
timeout (int, optional): The timeout for code execution. Defaults to 60.
work_dir (Union[Path, str], optional): The working directory for the code
execution. Defaults to Path(".").
bind_dir (Union[Path, str], optional): The 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.
auto_remove (bool, optional): If true, will automatically remove the Docker
container when it is stopped. Defaults to True.
stop_container (bool, optional): If true, will automatically stop the
Expand All @@ -85,6 +89,11 @@ def __init__(

work_dir.mkdir(exist_ok=True)

if bind_dir is None:
bind_dir = work_dir
elif isinstance(bind_dir, str):
bind_dir = Path(bind_dir)

client = docker.from_env()

# Check if the image exists
Expand All @@ -105,7 +114,7 @@ def __init__(
entrypoint="/bin/sh",
tty=True,
auto_remove=auto_remove,
volumes={str(work_dir.resolve()): {"bind": "/workspace", "mode": "rw"}},
volumes={str(bind_dir.resolve()): {"bind": "/workspace", "mode": "rw"}},
working_dir="/workspace",
)
self._container.start()
Expand All @@ -132,6 +141,7 @@ def cleanup() -> None:

self._timeout = timeout
self._work_dir: Path = work_dir
self._bind_dir: Path = bind_dir

@property
def timeout(self) -> int:
Expand All @@ -143,6 +153,11 @@ def work_dir(self) -> Path:
"""(Experimental) The working directory for the code execution."""
return self._work_dir

@property
def bind_dir(self) -> Path:
"""(Experimental) The binding directory for the code execution container."""
return self._bind_dir

@property
def code_extractor(self) -> CodeExtractor:
"""(Experimental) Export a code extractor that can be used by an agent."""
Expand Down
4 changes: 3 additions & 1 deletion website/docs/topics/code-execution/cli-code-executor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
"-v /var/run/docker.sock:/var/run/docker.sock\n",
"```\n",
"\n",
"This will allow the AutoGen container to spawn and control sibling containers on the host."
"This will allow the AutoGen container to spawn and control sibling containers on the host.\n",
"\n",
"If you need to bind a working directory to the AutoGen container but the directory belongs to your host machine, use the `bind_dir` parameter. This will allow the main AutoGen container to bind the *host* directory to the new spawned containers and allow it to access the files within the said directory. If the `bind_dir` is not specified, it will fallback to `work_dir`."
]
},
{
Expand Down

0 comments on commit a0fffc8

Please sign in to comment.