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

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

Docker exec - Write text to file in container

bashdocker

提问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

evalis a shell builtin, whereas docker execrequires an external utilityto be called, so using evalis 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 -coption:

相反,显式调用容器 ( 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 bashinstance 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 directorywas reported, because the redirection you intended to happen in the containeractually happened in your host's shell- and because dir. /home/user/.appapparently doesn't exist in your host's filesystem, the command failed fundamentally, beforeyour host's shell even attempted to execute the command (bashwill 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.
  • exec: "eval": executable file not found in $PATHhappened, because, as stated, evalis not an external utility, but a shell builtin, and docker execcan 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 -tparameter.

这将通过管道将您输入到容器中。当然,这也适用于纯文本(无文件)。离开-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 cpwhich 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