Skip to content

Commit f30b798

Browse files
committed
Update utils.py to print container log and docker run cmd for debugging.
1 parent 9378978 commit f30b798

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

ads/opctl/utils.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import os
1111
import subprocess
1212
import sys
13+
import shlex
1314
import tempfile
1415
import urllib.parse
1516
from distutils import dir_util
1617
from subprocess import Popen, PIPE, STDOUT
1718
from typing import Union, List, Tuple, Dict
18-
import shlex
1919
import yaml
2020

2121
import ads
@@ -321,27 +321,47 @@ def run_container(
321321
logger.info(f"command: {command}")
322322
logger.info(f"entrypoint: {entrypoint}")
323323

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+
324339
client = get_docker_client()
325340
try:
326341
client.api.inspect_image(image)
327342
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....")
329344
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

Comments
 (0)