我使用码头小妞 API来处理和操作Docker容器。在API中,put_archive()函数期望data字段以字节为单位。因此,使用我拥有的tarfile库:
import tarfile
import io
container = client.create_container(image="myimage", command="/bin/bash")
source = "~/.ssh/id_rsa.pub"
tarfile = create_tar_file(path=source, name="keys.tar")
# tarfile = "keys.tar"
# How can I read the tar file was a BytesIO() object?
data = io.BytesIO()
client.put_archive(container=container, path="/tmp", data=data)API说:
put_archive(container,path,data) 使用tar存档作为源在现有容器中插入文件或文件夹。 (str) -将提取文件的容器。 Path (str) -将提取文件的容器内的路径。一定存在。True(字节)-如果调用成功,要提取的数据(字节)返回:。docker.errors.APIError返回类型: (bool) 引发:-如果服务器返回错误。
我的问题是:
我如何将tar文件读取为BytesIO(),以便将其传递给put_archive()函数?
发布于 2016-09-20 23:59:44
您可以这样做(未经测试,因为我没有安装Docker-py):
with open('keys.tar', 'rb') as fin:
data = io.BytesIO(fin.read())https://stackoverflow.com/questions/39603978
复制相似问题