Upload 3 files
Browse files- extract_docker.py +39 -0
extract_docker.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import docker
|
| 2 |
+
import io
|
| 3 |
+
import tarfile
|
| 4 |
+
|
| 5 |
+
def log_image_contents(image_name):
|
| 6 |
+
# 连接到Docker
|
| 7 |
+
client = docker.from_env()
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
# 获取镜像
|
| 11 |
+
image = client.images.get(image_name)
|
| 12 |
+
|
| 13 |
+
# 创建临时容器
|
| 14 |
+
container = client.containers.create(image)
|
| 15 |
+
|
| 16 |
+
# 提取/app目录
|
| 17 |
+
bits, stat = container.get_archive('/app')
|
| 18 |
+
|
| 19 |
+
# 将内容读取到内存
|
| 20 |
+
file_like = io.BytesIO()
|
| 21 |
+
for chunk in bits:
|
| 22 |
+
file_like.write(chunk)
|
| 23 |
+
file_like.seek(0)
|
| 24 |
+
|
| 25 |
+
# 读取并打印文件内容
|
| 26 |
+
with tarfile.open(fileobj=file_like) as tar:
|
| 27 |
+
for member in tar.getmembers():
|
| 28 |
+
if member.isfile():
|
| 29 |
+
print(f"\n=== File: {member.name} ===")
|
| 30 |
+
f = tar.extractfile(member)
|
| 31 |
+
if f:
|
| 32 |
+
print(f.read().decode('utf-8'))
|
| 33 |
+
|
| 34 |
+
finally:
|
| 35 |
+
# 清理临时容器
|
| 36 |
+
container.remove()
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
log_image_contents('hpyp/bbapi:latest')
|