Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bind_dir arg to DockerCommandLineExecutor + docs update #2309

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
jackgerrits marked this conversation as resolved.
Show resolved Hide resolved

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
Loading