python - 如何从python中的docker容器获取文件
问题描述
client = docker.from_env()
data = client.containers.run("namecontainer:1.0", ['param'],detach=True)
file_json = data.get_archive(data, '/wd/out.json')
怎么弄?如何获取文件 out.json?我不明白。
解决方案
from io import BytesIO
client = docker.from_env()
data = client.containers.run("deep_analize/show_exploit:1.0", ['Apache 1.3.42'], detach=True)
sleep(5)
file_json = data.get_archive('/wd/out.json')
stream, stat = file_json
file_obj = BytesIO()
for i in stream:
file_obj.write(i)
file_obj.seek(0)
tar = tarfile.open(mode='r', fileobj=file_obj)
text = tar.extractfile('out.json')
q = text.read()
print(q)