mirror of
https://github.com/jupyter/docker-stacks.git
synced 2025-10-07 10:04:03 +00:00
Improve logs around running docker (#2261)
This commit is contained in:
@@ -22,11 +22,10 @@ class DockerRunner:
|
||||
self.docker_client: docker.DockerClient = docker_client
|
||||
|
||||
def __enter__(self) -> Container:
|
||||
LOGGER.info(f"Creating container for image {self.image_name} ...")
|
||||
LOGGER.info(f"Creating a container for the image: {self.image_name} ...")
|
||||
default_kwargs = {"detach": True, "tty": True}
|
||||
self.container = self.docker_client.containers.run(
|
||||
image=self.image_name,
|
||||
command=self.command,
|
||||
detach=True,
|
||||
image=self.image_name, command=self.command, **default_kwargs
|
||||
)
|
||||
LOGGER.info(f"Container {self.container.name} created")
|
||||
return self.container
|
||||
@@ -48,6 +47,6 @@ class DockerRunner:
|
||||
exec_result = container.exec_run(cmd)
|
||||
output = exec_result.output.decode().rstrip()
|
||||
assert isinstance(output, str)
|
||||
LOGGER.info(f"Command output: {output}")
|
||||
LOGGER.debug(f"Command output: {output}")
|
||||
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
||||
return output
|
||||
|
@@ -29,7 +29,7 @@ def http_client() -> requests.Session:
|
||||
def docker_client() -> docker.DockerClient:
|
||||
"""Docker client configured based on the host environment"""
|
||||
client = docker.from_env()
|
||||
LOGGER.info(f"Docker client created: {client.version()}")
|
||||
LOGGER.debug(f"Docker client created: {client.version()}")
|
||||
return client
|
||||
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
[pytest]
|
||||
addopts = -ra --color=yes
|
||||
log_cli = 1
|
||||
log_cli_level = DEBUG
|
||||
log_cli_level = INFO
|
||||
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
|
||||
log_cli_date_format=%Y-%m-%d %H:%M:%S
|
||||
markers =
|
||||
|
@@ -41,8 +41,6 @@ class CondaPackageHelper:
|
||||
|
||||
def __init__(self, container: TrackedContainer):
|
||||
self.container = container
|
||||
|
||||
LOGGER.info(f"Starting container {self.container.image_name} ...")
|
||||
self.container.run_detached(command=["bash", "-c", "sleep infinity"])
|
||||
|
||||
self.requested: dict[str, set[str]] | None = None
|
||||
@@ -101,7 +99,7 @@ class CondaPackageHelper:
|
||||
)
|
||||
# Keeping command line output since `mamba search --outdated --json` is way too long ...
|
||||
self.available = CondaPackageHelper._extract_available(
|
||||
self.container.exec_cmd("mamba search --outdated --quiet")
|
||||
self.container.exec_cmd("conda search --outdated --quiet")
|
||||
)
|
||||
return self.available
|
||||
|
||||
|
@@ -44,12 +44,15 @@ class TrackedContainer:
|
||||
Keyword arguments to pass to docker.DockerClient.containers.run
|
||||
extending and/or overriding key/value pairs passed to the constructor
|
||||
"""
|
||||
LOGGER.info(f"Running {self.image_name} with args {kwargs} ...")
|
||||
LOGGER.info(
|
||||
f"Creating a container for the image: {self.image_name} with args: {kwargs} ..."
|
||||
)
|
||||
default_kwargs = {"detach": True, "tty": True}
|
||||
final_kwargs = default_kwargs | kwargs
|
||||
self.container = self.docker_client.containers.run(
|
||||
self.image_name, **final_kwargs
|
||||
)
|
||||
LOGGER.info(f"Container {self.container.name} created")
|
||||
|
||||
def get_logs(self) -> str:
|
||||
assert self.container is not None
|
||||
@@ -65,13 +68,14 @@ class TrackedContainer:
|
||||
def exec_cmd(self, cmd: str, **kwargs: Any) -> str:
|
||||
assert self.container is not None
|
||||
container = self.container
|
||||
|
||||
LOGGER.info(f"Running cmd: `{cmd}` on container: {container.name}")
|
||||
default_kwargs = {"tty": True}
|
||||
final_kwargs = default_kwargs | kwargs
|
||||
exec_result = container.exec_run(cmd, **final_kwargs)
|
||||
output = exec_result.output.decode().rstrip()
|
||||
assert isinstance(output, str)
|
||||
LOGGER.info(f"Command output: {output}")
|
||||
LOGGER.debug(f"Command output: {output}")
|
||||
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
||||
return output
|
||||
|
||||
@@ -109,7 +113,7 @@ class TrackedContainer:
|
||||
def remove(self) -> None:
|
||||
"""Kills and removes the tracked docker container."""
|
||||
if self.container is None:
|
||||
LOGGER.info("No container to remove")
|
||||
LOGGER.debug("No container to remove")
|
||||
else:
|
||||
LOGGER.info(f"Removing container {self.container.name} ...")
|
||||
self.container.remove(force=True)
|
||||
|
Reference in New Issue
Block a user