首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用docker exec_run python docker客户端将文件传递到容器

使用docker exec_run python docker客户端将文件传递到容器
EN

Stack Overflow用户
提问于 2019-04-29 21:54:07
回答 1查看 492关注 0票数 1

我正在使用docker sdk for python。如何使用exec_run函数将文件传递给容器。

我想复制以下docker exec命令:

docker exec -i -u postgres <Insert the id find above> pg_restore -C -d postgres < filename

上面的命令加载postgres备份。filename是文件的名称,驻留在运行exec命令的主机上。

我正在尝试这样做:

代码语言:javascript
代码运行次数:0
运行
复制
containers[0].exec_run("/bin/bash -c 'pg_restore -C -d postgres <'" + filename, stdout=True, stderr=True, user='postgres')
print(exec_log[1])

在这里,文件驻留在另一个docker容器中,在该容器中运行一个使用python docker客户端的python应用程序。

我得到了这个:b'/bin/bash: 2019-04-29-postgres_db.dump: No such file or directory\n'

我已经研究了put_archive,但这需要提取容器内的文件。有没有使用exec_run或其他更简单的方法来实现这一点的方法?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-05-24 20:20:19

作为一种解决办法,您可以在docker映像中装载包含该文件的卷。然后你就可以在那里使用它了。

代码语言:javascript
代码运行次数:0
运行
复制
container = context.client.containers.run(
    image="ipostgres",
    auto_remove=True,
    detach=True,
    volumes={"/host/machine/store: {'bind': '/opt/whatever', 'mode': 'ro'}, },
)

然后

代码语言:javascript
代码运行次数:0
运行
复制
exec_run("/bin/bash -c 'pg_restore -C -d postgres < /opt/whatever/filename'", stdout=True, stderr=True, user='postgres')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55904858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档