| 39 | |
| 40 | |
| 41 | class DockerImageInspector: |
| 42 | def __init__(self): |
| 43 | self.image_info = None |
| 44 | self.image_id = None |
| 45 | self._container_lock = BoundedSemaphore(1) |
| 46 | self._active_containers = set() |
| 47 | |
| 48 | def open_image_by_id(self, image_id): |
| 49 | """打开指定ID的镜像""" |
| 50 | self.image_id = image_id |
| 51 | return self |
| 52 | |
| 53 | def get_image_info(self): |
| 54 | """获取镜像详细信息""" |
| 55 | try: |
| 56 | cmd = f"docker inspect {self.image_id}" |
| 57 | result = subprocess.run(cmd.split(), capture_output=True, text=True) |
| 58 | # public.print_log("|===========result:{}".format(result)) |
| 59 | if result.returncode == 0: |
| 60 | self.image_info = json.loads(result.stdout)[0] |
| 61 | return self.image_info |
| 62 | return None |
| 63 | except Exception as e: |
| 64 | print(f"获取镜像信息失败: {str(e)}") |
| 65 | return None |
| 66 | |
| 67 | def ocispec_v1(self): |
| 68 | """ |
| 69 | 获取镜像的OCI规范信息 |
| 70 | 返回包含配置和历史记录的字典 |
| 71 | """ |
| 72 | if not self.image_info: |
| 73 | self.get_image_info() |
| 74 | |
| 75 | if not self.image_info: |
| 76 | return {} |
| 77 | try: |
| 78 | # 获取配置信息 |
| 79 | config = self.image_info.get('Config', {}) |
| 80 | |
| 81 | # 构造OCI规范格式 |
| 82 | oci_spec = { |
| 83 | 'created': self.image_info.get('Created'), |
| 84 | 'architecture': self.image_info.get('Platform', 'amd64'), # 默认amd64 |
| 85 | 'os': self.image_info.get('Platform', 'linux'), # 默认linux |
| 86 | 'config': { |
| 87 | 'Env': config.get('Env', []), |
| 88 | 'Cmd': config.get('Cmd', []), |
| 89 | 'WorkingDir': config.get('WorkingDir', ''), |
| 90 | 'Entrypoint': config.get('Entrypoint', []), |
| 91 | 'ExposedPorts': config.get('ExposedPorts', {}), |
| 92 | 'Volumes': config.get('Volumes', {}), |
| 93 | }, |
| 94 | 'history': [] |
| 95 | } |
| 96 | |
| 97 | # 使用docker history命令获取历史记录 |
| 98 | try: |