From 5e048f01507909ad702c416d9f3ae3b2491a0101 Mon Sep 17 00:00:00 2001 From: DDSRem <1448139087@qq.com> Date: Sat, 9 Sep 2023 14:18:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=AE=B9=E5=99=A8id?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/system.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/utils/system.py b/app/utils/system.py index 6e266234d..36b553bde 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -311,11 +311,13 @@ def restart() -> Tuple[bool, str]: # 创建 Docker 客户端 client = docker.DockerClient(base_url='tcp://127.0.0.1:38379') # 获取当前容器的 ID - container_hostname = os.environ.get("HOSTNAME") - if not container_hostname: - return False, "无法获取容器的主机名!" - container = client.containers.get(container_hostname) - container_id = container.id + with open('/proc/self/mountinfo', 'r') as f: + data = f.read() + index_resolv_conf = data.find("resolv.conf") + if index_resolv_conf != -1: + index_second_slash = data.rfind("/", 0, index_resolv_conf) + index_first_slash = data.rfind("/", 0, index_second_slash) + 1 + container_id = data[index_first_slash:index_second_slash] if not container_id: return False, "获取容器ID失败!" # 重启当前容器