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

Optimize Docker usage by enabling container reuse #2444

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 25 additions & 13 deletions autogen/coding/docker_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,32 @@ def __init__(
# Let the docker exception escape if this fails.
client.images.pull(image)

if container_name is None:
self._container = None
# Try to start the container if it exists
if container_name:
try:
self._container = client.containers.get(container_name)
if self._container.status == "running":
self._container.start()
_wait_for_ready(self._container)
except docker.errors.NotFound:
self._container = None
# Else create a new container
else:
container_name = f"autogen-code-exec-{uuid.uuid4()}"

# Start a container from the image, read to exec commands later
self._container = client.containers.create(
image,
name=container_name,
entrypoint="/bin/sh",
tty=True,
auto_remove=auto_remove,
volumes={str(work_dir.resolve()): {"bind": "/workspace", "mode": "rw"}},
working_dir="/workspace",
)
self._container.start()

if self._container is None:
# Start a container from the image, read to exec commands later
self._container = client.containers.run(
image,
name=container_name,
entrypoint="/bin/sh",
tty=True,
auto_remove=auto_remove,
volumes={str(work_dir.resolve()): {"bind": "/workspace", "mode": "rw"}},
working_dir="/workspace",
detach=True,
)

_wait_for_ready(self._container)

Expand Down
Loading