diff --git a/cli/Notes.md b/cli/Notes.md index b6b1ca4e..c9123d19 100644 --- a/cli/Notes.md +++ b/cli/Notes.md @@ -15,4 +15,8 @@ #### 如何获取所有已占用的端口号? -基于 psutil 模块打印所有端口信息,然后匹配关键词 \ No newline at end of file +基于 psutil 模块打印所有端口信息,然后匹配关键词 + +#### 是否可以直接使用 Linux 命令? + +为了提高操作系统的兼容性,尽量减少 Linux 命令使用,可以多使用 Python 包处理 Docker \ No newline at end of file diff --git a/cli/model.py b/cli/model.py index 0f4814d9..53cdb4e1 100644 --- a/cli/model.py +++ b/cli/model.py @@ -1,5 +1,5 @@ -import os, io, sys, platform, psutil, json, secrets, string +import os, io, sys, platform, psutil, json, secrets, string, docker from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union import urllib.request @@ -157,6 +157,17 @@ class DockerComposeOp: class DockerOp: ''' Docker operation ''' def __init__(self): - pass + self.client = docker.from_env() - \ No newline at end of file + def lsContainer(self): + container_list = [] + + for container in self.client.containers.list(all): + container_list.append(container.name) + return container_list + + def lsProject(self): + project_dict = {} + + for name in self.lsContainer(): + print(self.client.containers.get(name).labels) \ No newline at end of file diff --git a/cli/requirements.txt b/cli/requirements.txt index 96421bb6..3e83ce07 100644 --- a/cli/requirements.txt +++ b/cli/requirements.txt @@ -1,5 +1,5 @@ typer psutil -jq +pyjq docker docker-compose \ No newline at end of file diff --git a/cli/test.py b/cli/test.py index eb7f8317..c04d66aa 100644 --- a/cli/test.py +++ b/cli/test.py @@ -1,5 +1,4 @@ -import docker +import model +docker = model.DockerOp() -client = docker.from_env() -for image in client.images.list(): - print(image.id) \ No newline at end of file +docker.lsProject() \ No newline at end of file