我正在使用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命令的主机上。
我正在尝试这样做:
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
或其他更简单的方法来实现这一点的方法?
谢谢
发布于 2019-05-24 20:20:19
作为一种解决办法,您可以在docker映像中装载包含该文件的卷。然后你就可以在那里使用它了。
container = context.client.containers.run(
image="ipostgres",
auto_remove=True,
detach=True,
volumes={"/host/machine/store: {'bind': '/opt/whatever', 'mode': 'ro'}, },
)
然后
exec_run("/bin/bash -c 'pg_restore -C -d postgres < /opt/whatever/filename'", stdout=True, stderr=True, user='postgres')
https://stackoverflow.com/questions/55904858
复制相似问题