From 40b6b761786356dff240f17a72a624a9cdb49396 Mon Sep 17 00:00:00 2001 From: wunder957 Date: Wed, 29 Nov 2023 17:10:11 +0800 Subject: [PATCH] Fix docker id infer --- duetector/injectors/docker.py | 8 ++++---- tests/base/injector/test_docker.py | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/duetector/injectors/docker.py b/duetector/injectors/docker.py index 4024967..ae20fb5 100644 --- a/duetector/injectors/docker.py +++ b/duetector/injectors/docker.py @@ -15,6 +15,7 @@ class DockerInjector(ProcInjector, Inspector): def __init__(self, config: dict[str, Any] = None, *args, **kwargs): super().__init__(config, *args, **kwargs) try: + # TODO: Config docker base_url and tls self.client = docker.APIClient() if not self.client.ping(): self.client = None @@ -42,14 +43,13 @@ def _inspect(self, model: dict[str, Any]) -> dict[str, Any]: return {} container_id = None for cg in cgroups: - if "docker" in cg: - container_id = cg.split(":")[-1].split("/")[-1].lstrip("docker-").split(".")[0] - break + container_id = cg.split(":")[-1].split("/")[-1].lstrip("docker-").split(".")[0] + break if not container_id: return {} if not self.client: - return {"container_id": container_id} + return {"maybe_container_id": container_id} try: container_info = self.client.inspect_container(container_id) except Exception as e: diff --git a/tests/base/injector/test_docker.py b/tests/base/injector/test_docker.py index 95dac37..2a6aaea 100644 --- a/tests/base/injector/test_docker.py +++ b/tests/base/injector/test_docker.py @@ -1,4 +1,5 @@ import glob +import time from collections import namedtuple from pathlib import Path @@ -37,6 +38,11 @@ def test_container(docker_client: docker.DockerClient, command: str): detach=True, remove=True, ) + container.reload() + while container.status != "running": + container.reload() + time.sleep(0.5) + pid = None for p in glob.glob("/proc/[0-9]*"): p = Path(p)