简体   繁体   English

如何作为Bash Shell脚本的一部分在Docker容器中执行命令

[英]How to execute commands in docker container as part of bash shell script

I would like to write a bash script that automates the following: 我想编写一个bash脚本来自动执行以下操作:

Get inside running container 进入正在运行的容器中

docker exec -it CONTAINER_NAME /bin/bash

Execute some commands: 执行一些命令:

cat /dev/null > /usr/local/tomcat/logs/app.log
exit

The problematic part is when docker exec is executed. 问题部分是何时执行docker exec The new shell is created, but the other commands are not executed. 创建新的外壳程序,但不执行其他命令。

Is there a way to solve it? 有办法解决吗?

You can use heredoc with docker exec command: 您可以在docker exec命令中使用heredoc

docker exec -i CONTAINER_NAME bash <<'EOF'
cat /dev/null > /usr/local/tomcat/logs/app.log
exit
EOF

To use variables: 要使用变量:

logname='/usr/local/tomcat/logs/app.log'

then use as: 然后用作:

docker exec -i CONTAINER_NAME bash <<EOF
cat /dev/null > "$logname"
exit
EOF

您可以简单地启动

docker exec -it container_id cat /dev/null > /usr/local/tomcat/logs/app.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM