@@ -16,13 +16,15 @@ class DockerExecutor(EnvironmentExecutor):
1616 preserving original exceptions with 'from' clause for debugging.
1717 """
1818
19- def __init__ (self , container_identifier : str ):
19+ def __init__ (self , container_identifier : str , debug : bool = False ):
2020 """Initialize Docker executor."""
2121 if docker is None :
2222 raise ImportError (
2323 "Docker package is required for Docker functionality. Please install it with: pip install docker"
2424 )
2525
26+ self .debug = debug
27+
2628 try :
2729 self .client = docker .from_env ()
2830 self .container = self .client .containers .get (container_identifier )
@@ -32,6 +34,9 @@ def __init__(self, container_identifier: str):
3234 f"Container '{ container_identifier } ' is not running (status: { self .container .status } )"
3335 )
3436
37+ if self .debug :
38+ print (f"Connected to Docker container: { container_identifier } " )
39+
3540 except docker .errors .NotFound as exc :
3641 raise RuntimeError (f"Container '{ container_identifier } ' not found" ) from exc
3742 except docker .errors .APIError as e :
@@ -42,6 +47,10 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
4247
4348 Returns actual command exit code on success, or 1 for execution environment failures.
4449 """
50+ if self .debug :
51+ workdir_info = f" (workdir: { working_dir } )" if working_dir else ""
52+ print (f"Executing docker command: sh -c '{ command } '{ workdir_info } " )
53+
4554 try :
4655 # First, try with sh
4756 result = self .container .exec_run (
@@ -50,6 +59,9 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
5059 stdout = result .output .decode ("utf-8" ) if result .output else ""
5160 stderr = ""
5261
62+ if self .debug :
63+ print (f"Docker command exit code: { result .exit_code } " )
64+
5365 return stdout , stderr , result .exit_code
5466
5567 except docker .errors .APIError as e :
@@ -63,16 +75,26 @@ def execute_command(self, command: str, working_dir: Optional[str] = None) -> tu
6375
6476 def _execute_command_direct (self , command : str , working_dir : Optional [str ] = None ) -> tuple [str , str , int ]:
6577 """Fallback: execute simple commands directly without shell."""
78+ if self .debug :
79+ workdir_info = f" (workdir: { working_dir } )" if working_dir else ""
80+ print (f"Fallback: executing docker command directly: { command } { workdir_info } " )
81+
6682 try :
6783 # Handle only the simple cases we actually use
6884 cmd_parts = DockerExecutor ._parse_simple_command (command )
6985 if not cmd_parts :
7086 return "" , f"Command too complex for direct execution (no shell available): { command } " , 1
7187
88+ if self .debug :
89+ print (f"Parsed command parts: { cmd_parts } " )
90+
7291 result = self .container .exec_run (cmd = cmd_parts , stdout = True , stderr = True , tty = False , workdir = working_dir )
7392 stdout = result .output .decode ("utf-8" ) if result .output else ""
7493 stderr = ""
7594
95+ if self .debug :
96+ print (f"Direct execution exit code: { result .exit_code } " )
97+
7698 return stdout , stderr , result .exit_code
7799
78100 except docker .errors .APIError as e :
0 commit comments