Check if a Docker container exists (by name). Args: container_name: Container name to check Returns: Container ID if exists, None otherwise
(container_name: str)
| 369 | |
| 370 | |
| 371 | def docker_container_exists(container_name: str) -> Optional[str]: |
| 372 | """Check if a Docker container exists (by name). |
| 373 | |
| 374 | Args: |
| 375 | container_name: Container name to check |
| 376 | |
| 377 | Returns: |
| 378 | Container ID if exists, None otherwise |
| 379 | """ |
| 380 | try: |
| 381 | result = subprocess.run( |
| 382 | [ |
| 383 | get_docker_command(), |
| 384 | "ps", |
| 385 | "-a", |
| 386 | "--filter", |
| 387 | f"name=^{container_name}$", |
| 388 | "--format", |
| 389 | "{{.ID}}", |
| 390 | ], |
| 391 | capture_output=True, |
| 392 | text=True, |
| 393 | timeout=10, |
| 394 | ) |
| 395 | if result.returncode == 0 and result.stdout.strip(): |
| 396 | return result.stdout.strip() |
| 397 | return None |
| 398 | except (FileNotFoundError, subprocess.TimeoutExpired): |
| 399 | return None |
| 400 | |
| 401 | |
| 402 | def docker_stop_container(container_id: str) -> bool: |
no test coverage detected