[英]How to enter docker container use shell script and do something?
I want to enter my container and do something, then leave this container.我想进入我的容器并做一些事情,然后离开这个容器。
#!/bin/bash
docker exec -i ubuntu-lgx bash << EOF
echo "test file" >> /inner.txt
ls -l /inner.txt
content=`cat /inner.txt`
echo ${conent}
# do something else
EOF
when I run this script, the bash tell me the file is not exist.but the ls
can output the file's property.当我运行这个脚本时,bash 告诉我文件不存在。但是ls
可以 output 文件的属性。
cat: /inner.txt: No such file or directory
-rw-r--r--. 1 root root 58 Nov 14 11:51 /inner.txt
where am I wrong?我哪里错了? and how to fix it?以及如何解决?
The problem is that you're not protecting your "here" document from local shell expansion.问题是您没有保护您的“此处”文档免受本地 shell 扩展的影响。 When you write:当你写:
#!/bin/bash
docker exec -i ubuntu-lgx bash << EOF
content=`cat /inner.txt`
EOF
That cat /inner.txt
is run on your local system, not the remote system. cat /inner.txt
在您的本地系统上运行,而不是在远程系统上运行。 The contents of here document are parsed for variable expansion and other shell features.解析此处文档的内容以进行变量扩展和其他 shell 功能。
To prevent that, write it like this:为了防止这种情况,请这样写:
#!/bin/bash
docker exec -i ubuntu-lgx bash << 'EOF'
echo "test file" >> /inner.txt
ls -l /inner.txt
content=`cat /inner.txt`
echo ${content}
# do something else
EOF
The single quotes in 'EOF'
are a signal to the shell to interpret the here document verbatim. 'EOF'
中的单引号是向 shell 逐字解释此处文档的信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.