如何在docker容器中执行命令作为bash shell脚本的一部分 [英] How to execute commands in docker container as part of bash shell script

查看:1651
本文介绍了如何在docker容器中执行命令作为bash shell脚本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写自动执行以下操作的bash脚本:

I would like to write bash script which automates the following:

获取内部运行容器

docker exec -it CONTAINER_NAME /bin/bash

执行一些命令:

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

有问题的部分是当docker exec被执行时,会创建新的shell并且不执行其他命令

The problematic part is when docker exec is executed, new shell is created and other commands are not executed

有没有办法解决它?

推荐答案

您可以使用 heredoc docker exec 命令:

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

要使用变量:

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

然后使用:

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

这篇关于如何在docker容器中执行命令作为bash shell脚本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆