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 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?
eval
is a shell builtin , whereas docker exec
requires an external utility to be called, so using eval
is not an option.
Instead, invoke a shell executable in the container ( bash
) explicitly , and pass it the command to execute as a string , via its -c
option:
docker exec "app_$i" bash -c "echo 'server.url=$server_url' >> /home/app/.app/app.config"
By using a double-quoted string to pass to bash -c
, you ensure that the current shell performs string interpolation first, whereas the container's bash
instance then sees the expanded result as a literal , as part of the embedded single-quoted string.
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 container actually 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 , before your host's shell even attempted to execute the command ( bash
will abort command execution if an output redirection cannot be performed).
eval
, its use didn't surface as a problem until your second command, which actually did get executed. exec: "eval": executable file not found in $PATH
happened, because, as stated, eval
is not an external utility , but a shell builtin , and docker exec
can only execute external utilities.
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.
See 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
try to use heredoc:
(docker exec -i container sh -c "cat > /test/iplist") << EOF
10.99.154.146
10.99.189.247
10.99.189.250
EOF
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.