我按照此链接中给出的 get_archive() link 说明进行操作 但我没有创建容器,而是尝试使用已经运行的容器“docker-nginx”作为输入字符串,以及我的内容驻留在 nginx 服务器中的目标文件夹“/usr/share/nginx/html”并得到了文件夹统计信息的输出,我想知道如何在此函数中给出目标文件夹,如果不是的话,我提取的 tar 文件在哪里?我无法弄清楚我的文件是在哪里下载的
这是我的代码
strm,stat=c.get_archive(container_name,'/usr/share/nginx/html')
我的 print(strm) 输出是 <requests.packages.urllib3.response.HTTPResponse object at 0x7fe3581e1250>
我的 print(stat) 输出是 {u'linkTarget': u'', u'mode': 2147484157, u'mtime': u'2016-10-05T09:37:17.928258508-05:00', u'name': u'html', u'size': 4096}
最佳答案
答案取决于您使用的 docker sdk 版本。
对于 docker
3.X 之前的 sdk 版本
get_archive
返回类似文件的流。因此,使用 shutdown.copyfileobj 将数据流式传输到文件:
import shutil
strm, stat = container.get_archive(container_name, path_in_container)
with open('out.html', 'w') as outfile:
shutil.copyfileobj(strm, outfile)
对于docker
sdk 3.X+
get_archive
返回文件内容的迭代器,因此只需迭代并写入:
strm, stat = container.get_archive(container_name, path_in_container)
with open('out.html', 'w') as outfile:
for d in strm:
outfile.write(d)
关于python - docker-py getarchive 目标文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903822/