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

* add bind_dir arg and update docs

* lint
This commit is contained in:
Gabriel Bezerra Valério 2024-04-30 17:49:04 -03:00 committed by GitHub
parent bcb6117c97
commit a0fffc815b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -45,6 +45,7 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
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,
):
@ -67,6 +68,9 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
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
@ -85,6 +89,11 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
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
@ -105,7 +114,7 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
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()
@ -132,6 +141,7 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
self._timeout = timeout
self._work_dir: Path = work_dir
self._bind_dir: Path = bind_dir
@property
def timeout(self) -> int:
@ -143,6 +153,11 @@ class DockerCommandLineCodeExecutor(CodeExecutor):
"""(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."""

View File

@ -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`."
]
},
{