bash Docker exec - 将文本写入容器中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35703317/
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
Docker exec - Write text to file in container
提问by jwi
I want to write a line of text to a textfile INSIDE a running docker container. Here's what I've tried so far:
我想将一行文本写入正在运行的 docker 容器内的文本文件。这是我迄今为止尝试过的:
docker exec -d app_$i eval echo "server.url=$server_url" >> /home/app/.app/app.config
Response:
回复:
/home/user/.app/app.config: No such file or directory
Second try:
第二次尝试:
cfg_add="echo 'server.url=$server_url' >> /home/user/.app/app.config"
docker exec -i app_$i eval $cfg_add
Response:
回复:
exec: "eval": executable file not found in $PATH
Any ideas?
有任何想法吗?
回答by mklement0
eval
is a shell builtin, whereas docker exec
requires an external utilityto be called, so using eval
is notan option.
eval
是一个外壳内置,而docker exec
需要一个外部工具被调用,所以使用eval
是不一种选择。
Instead, invoke a shell executable in the container (bash
) explicitly, and pass it the command to execute as a string, via its -c
option:
相反,显式调用容器 ( bash
) 中的 shell 可执行文件,并通过其 选项传递命令以作为字符串执行:-c
docker exec "app_$i" bash -c "echo 'server.url=$server_url' >> /home/app/.app/app.config"
By using a double-quotedstring to pass to bash -c
, you ensure that the currentshell performs string interpolation first, whereas the container's bash
instance then sees the expanded result as a literal, as part of the embedded single-quotedstring.
通过使用双引号字符串传递给bash -c
,您可以确保当前shell 首先执行字符串插值,而容器的bash
实例然后将扩展结果视为文字,作为嵌入的单引号字符串的一部分。
As for your symptoms:
至于你的症状:
/home/user/.app/app.config: No such file or directory
was reported, because the redirection you intended to happen in the containeractually happened in your host's shell- and because dir./home/user/.app
apparently doesn't exist in your host's filesystem, the command failed fundamentally, beforeyour host's shell even attempted to execute the command (bash
will abort command execution if an output redirection cannot be performed).- Thus, even though your first command also contained
eval
, its use didn't surface as a problem until your second command, which actually didget executed.
- Thus, even though your first command also contained
exec: "eval": executable file not found in $PATH
happened, because, as stated,eval
is not an external utility, but a shell builtin, anddocker exec
can only execute external utilities.
/home/user/.app/app.config: No such file or directory
被报告了,因为您打算在容器中发生的重定向实际上发生在您主机的 shell 中- 并且因为 dir./home/user/.app
显然,您的主机的文件系统中不存在该命令,在您的主机的 shell 甚至尝试执行该命令之前,该命令从根本上失败了(如果无法执行输出重定向,将中止命令执行)。bash
- 因此,即使你的第一个命令中还含有
eval
,其用途并没有表面,直到你的第二个命令,实际上问题没有得到执行。
- 因此,即使你的第一个命令中还含有
exec: "eval": executable file not found in $PATH
发生了,因为如前所述,eval
它不是外部实用程序,而是内置的 shell,并且docker exec
只能执行外部实用程序。
回答by Risadinha
Additionally:
此外:
If you need to write text from outside the container, this also works:
如果您需要从容器外部写入文本,这也适用:
(docker exec -i container sh -c "cat > c.sql") < c.sql
This will pipe you input into the container. Of course, this would also work for plain text (no file). It is important to leave off the -t
parameter.
这将通过管道将您输入到容器中。当然,这也适用于纯文本(无文件)。离开-t
参数很重要。
See https://github.com/docker/docker/pull/9537
见https://github.com/docker/docker/pull/9537
UPDATE (in case you just need to copy files, not parts of files):
更新(以防您只需要复制文件,而不是文件的一部分):
Docker v17.03 has docker cp
which copies between the local fs and the container: https://docs.docker.com/engine/reference/commandline/cp/#usage
Docker v17.03docker cp
在本地 fs 和容器之间有哪些副本:https: //docs.docker.com/engine/reference/commandline/cp/#usage