Bash / Docker exec:从容器内部重定向文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31438112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:20:38  来源:igfitidea点击:

Bash / Docker exec: file redirection from inside a container

bashdocker

提问by Jonathan Petitcolas

I can't figure out how to read content of a file from a Docker container. I want to execute content of a SQL file into my PGSQL container. I tried:

我不知道如何从 Docker 容器中读取文件的内容。我想将 SQL 文件的内容执行到我的 PGSQL 容器中。我试过:

docker exec -it app_pgsql psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql

My application is mounted in /usr/src/app. But I got an error:

我的应用程序安装在/usr/src/app. 但我得到了一个错误:

bash: /usr/src/app/migrations/*.sql: No such file or directory

bash: /usr/src/app/migrations/*.sql: 没有那个文件或目录

It seems that Bash interprets this path as an host path, not a guest one. Indeed, executing the command in two times works perfectly:

似乎 Bash 将此路径解释为主机路径,而不是访客路径。确实,分两次执行命令效果很好:

docker exec -it app_pgsql
psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql

I think that's more a Bash issue than a Docker one, but I'm still stuck! :)

我认为这更像是一个 Bash 问题而不是 Docker 问题,但我仍然被卡住了!:)

回答by VonC

Try and use a shell to execute that command

尝试使用 shell 来执行该命令

sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'

The full command would be:

完整的命令是:

docker exec -it app_pgsql sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'

回答by user2915097

try with sh -c "your long command"

尝试 sh -c "your long command"

回答by ivanleoncz

You can use the database client in order to connect to you container and redirect the database file, then you can perform the restore.

您可以使用数据库客户端连接到您的容器并重定向数据库文件,然后您可以执行还原。

Here is an example with MySQL: a container running MySQL, using the host network stack. Since that the container is using the host network stack (if you don't have any restriction on your MySQL or whatever database), you can connect via localhost and performing the commands transparently

这是 MySQL 的一个例子:一个运行 MySQL 的容器,使用主机网络堆栈。由于容器正在使用主机网络堆栈(如果您对 MySQL 或任何数据库没有任何限制),您可以通过 localhost 连接并透明地执行命令

mysql -h 127.0.0.1 -u user -pyour_passwd database_name < db_backup.sql

You can do the same with PostgresSQL(Restore a postgres backup file using the command line?):

您可以对 PostgresSQL 执行相同操作使用命令行恢复 postgres 备份文件?):

pg_restore --host 127.0.0.1 --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"

Seems like that "docker exec" does not support input redirection.. I will verify this and maybe open an issue for Docker Community at GitHub, if it is applicable.

似乎“docker exec”不支持输入重定向..我会验证这一点,如果适用,我可能会在 GitHub 上为 Docker 社区打开一个问题。

回答by dahe

Also working when piping backup to the mysql command:

当管道备份到 mysql 命令时也工作:

cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE

cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE