如何从python中的docker容器中获取文件

时间:2018-05-27 13:10:32

标签: 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?我不明白。

1 个答案:

答案 0 :(得分:1)

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)