Runs a Docker container with streaming output. Returns actual container exit code (0 for success, timeout is treated as success). Args: image_name: Docker image to run command: Command to execute in container volumes: Volume mounts
(
self,
image_name: str,
command: list[str],
volumes: Optional[dict[str, dict[str, str]]] = None,
environment: Optional[dict[str, str]] = None,
name: Optional[str] = None,
timeout: Optional[int] = None,
interrupt_pattern: Optional[str] = None,
output_file: Optional[str] = None,
)
| 264 | return container_id |
| 265 | |
| 266 | def run_container_streaming( |
| 267 | self, |
| 268 | image_name: str, |
| 269 | command: list[str], |
| 270 | volumes: Optional[dict[str, dict[str, str]]] = None, |
| 271 | environment: Optional[dict[str, str]] = None, |
| 272 | name: Optional[str] = None, |
| 273 | timeout: Optional[int] = None, |
| 274 | interrupt_pattern: Optional[str] = None, |
| 275 | output_file: Optional[str] = None, |
| 276 | ) -> int: |
| 277 | """ |
| 278 | Runs a Docker container with streaming output. |
| 279 | Returns actual container exit code (0 for success, timeout is treated as success). |
| 280 | |
| 281 | Args: |
| 282 | image_name: Docker image to run |
| 283 | command: Command to execute in container |
| 284 | volumes: Volume mounts |
| 285 | environment: Environment variables |
| 286 | name: Container name |
| 287 | timeout: Timeout in seconds (if exceeded, returns 0) |
| 288 | interrupt_pattern: Pattern to detect in output (informational only) |
| 289 | output_file: Optional file path to write output to |
| 290 | """ |
| 291 | print( |
| 292 | f"Running container from image: {image_name} with command: {' '.join(command)}" |
| 293 | ) |
| 294 | docker_cmd = ["run", "--rm"] # --rm to automatically remove container on exit |
| 295 | if name: |
| 296 | docker_cmd.extend(["--name", name]) |
| 297 | if volumes: |
| 298 | for host_path, container_path_info in volumes.items(): |
| 299 | mode = container_path_info.get("mode", "rw") |
| 300 | docker_cmd.extend( |
| 301 | ["-v", f"{host_path}:{container_path_info['bind']}:{mode}"] |
| 302 | ) |
| 303 | if environment: |
| 304 | for key, value in environment.items(): |
| 305 | docker_cmd.extend(["-e", f"{key}={value}"]) |
| 306 | |
| 307 | docker_cmd.append(image_name) |
| 308 | docker_cmd.extend(command) |
| 309 | |
| 310 | result = self._run_docker_command( |
| 311 | docker_cmd, |
| 312 | check=False, |
| 313 | stream_output=True, |
| 314 | timeout=timeout, |
| 315 | interrupt_pattern=interrupt_pattern, |
| 316 | output_file=output_file, |
| 317 | container_name=name, |
| 318 | ) |
| 319 | return result.returncode |
| 320 | |
| 321 | def get_container_logs(self, container_id_or_name: str) -> str: |
| 322 | """Retrieves logs from a container.""" |