|
10 | 10 | import os
|
11 | 11 | import subprocess
|
12 | 12 | import sys
|
| 13 | +import shlex |
13 | 14 | import tempfile
|
14 | 15 | import urllib.parse
|
15 | 16 | from distutils import dir_util
|
16 | 17 | from subprocess import Popen, PIPE, STDOUT
|
17 | 18 | from typing import Union, List, Tuple, Dict
|
18 |
| -import shlex |
19 | 19 | import yaml
|
20 | 20 |
|
21 | 21 | import ads
|
@@ -321,27 +321,47 @@ def run_container(
|
321 | 321 | logger.info(f"command: {command}")
|
322 | 322 | logger.info(f"entrypoint: {entrypoint}")
|
323 | 323 |
|
| 324 | + # Print out the equivalent docker run command for debugging purpose |
| 325 | + docker_run_cmd = [">>> docker run --rm"] |
| 326 | + if entrypoint: |
| 327 | + docker_run_cmd.append(f"--entrypoint {entrypoint}") |
| 328 | + if env_vars: |
| 329 | + docker_run_cmd.extend([f"-e {key}={val}" for key, val in env_vars.items()]) |
| 330 | + if bind_volumes: |
| 331 | + docker_run_cmd.extend( |
| 332 | + [f'-v {source}:{bind.get("bind")}' for source, bind in bind_volumes.items()] |
| 333 | + ) |
| 334 | + docker_run_cmd.append(image) |
| 335 | + if command: |
| 336 | + docker_run_cmd.append(command) |
| 337 | + logger.debug(" ".join(docker_run_cmd)) |
| 338 | + |
324 | 339 | client = get_docker_client()
|
325 | 340 | try:
|
326 | 341 | client.api.inspect_image(image)
|
327 | 342 | except docker.errors.ImageNotFound:
|
328 |
| - logger.warn(f"Image {image} not found. Try pulling it now....") |
| 343 | + logger.warning(f"Image {image} not found. Try pulling it now....") |
329 | 344 | run_command(["docker", "pull", f"{image}"], None)
|
330 |
| - container = client.containers.run( |
331 |
| - image=image, |
332 |
| - volumes=bind_volumes, |
333 |
| - command=shlex.split(command) if command else None, |
334 |
| - environment=env_vars, |
335 |
| - detach=True, |
336 |
| - entrypoint=entrypoint, |
337 |
| - user=0, |
338 |
| - # auto_remove=True, |
339 |
| - ) |
340 |
| - logger.info(f"Container ID: {container.id}") |
341 |
| - |
342 |
| - for line in container.logs(stream=True, follow=True): |
343 |
| - logger.info(line.decode("utf-8").strip()) |
344 |
| - |
345 |
| - result = container.wait() |
346 |
| - container.remove() |
347 |
| - return result.get("StatusCode", -1) |
| 345 | + try: |
| 346 | + container = client.containers.run( |
| 347 | + image=image, |
| 348 | + volumes=bind_volumes, |
| 349 | + command=shlex.split(command) if command else None, |
| 350 | + environment=env_vars, |
| 351 | + detach=True, |
| 352 | + entrypoint=entrypoint, |
| 353 | + user=0, |
| 354 | + # auto_remove=True, |
| 355 | + ) |
| 356 | + logger.info("Container ID: %s", container.id) |
| 357 | + for line in container.logs(stream=True, follow=True): |
| 358 | + print(line.decode("utf-8"), end="") |
| 359 | + |
| 360 | + result = container.wait() |
| 361 | + return result.get("StatusCode", -1) |
| 362 | + except docker.errors.APIError as ex: |
| 363 | + logger.error(ex.explanation) |
| 364 | + return -1 |
| 365 | + finally: |
| 366 | + # Remove the container |
| 367 | + container.remove() |
0 commit comments