Runs a Docker container with specified command, volume mounts, and environment variables. Returns the container ID.
(
self,
image_name: str,
command: list[str],
volumes: Optional[dict[str, dict[str, str]]] = None,
environment: Optional[dict[str, str]] = None,
detach: bool = False,
name: Optional[str] = None,
)
| 225 | print(f"Image {image_name}:{tag} pulled successfully.") |
| 226 | |
| 227 | def run_container( |
| 228 | self, |
| 229 | image_name: str, |
| 230 | command: list[str], |
| 231 | volumes: Optional[dict[str, dict[str, str]]] = None, |
| 232 | environment: Optional[dict[str, str]] = None, |
| 233 | detach: bool = False, |
| 234 | name: Optional[str] = None, |
| 235 | ) -> str: # Return container ID as string |
| 236 | """ |
| 237 | Runs a Docker container with specified command, volume mounts, and environment variables. |
| 238 | Returns the container ID. |
| 239 | """ |
| 240 | print( |
| 241 | f"Running container from image: {image_name} with command: {' '.join(command)}" |
| 242 | ) |
| 243 | docker_cmd = ["run", "--rm"] # --rm to automatically remove container on exit |
| 244 | if detach: |
| 245 | docker_cmd.append("-d") |
| 246 | if name: |
| 247 | docker_cmd.extend(["--name", name]) |
| 248 | if volumes: |
| 249 | for host_path, container_path_info in volumes.items(): |
| 250 | mode = container_path_info.get("mode", "rw") |
| 251 | docker_cmd.extend( |
| 252 | ["-v", f"{host_path}:{container_path_info['bind']}:{mode}"] |
| 253 | ) |
| 254 | if environment: |
| 255 | for key, value in environment.items(): |
| 256 | docker_cmd.extend(["-e", f"{key}={value}"]) |
| 257 | |
| 258 | docker_cmd.append(image_name) |
| 259 | docker_cmd.extend(command) |
| 260 | |
| 261 | result = self._run_docker_command(docker_cmd) |
| 262 | container_id = result.stdout.strip() |
| 263 | print(f"Container {container_id} started.") |
| 264 | return container_id |
| 265 | |
| 266 | def run_container_streaming( |
| 267 | self, |
no test coverage detected