10

I would like to write a bash script that automates the following:

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. The new shell is created, but the other commands are not executed.

Is there a way to solve it?

2 Answers 2

31

You can use heredoc with docker exec command:

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This works. How can I pass parameters from outside to heredoc?
To pass parameters use EOF instead of 'EOF' at first line and then use variables inside heredoc
For those wondering, I successfully got this to work with the docker run command as well.
@ndmeiri how?? I am trying with docker run --detach-keys="ctrl-p" -it busybox and I can't make it work
-3

You can simply launch

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.