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

Patrik Mihalčin

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?

anubhava

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute shell script within a docker container

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute the vim commands through shell script

From Dev

How to execute commands in gnuplot using shell script?

From Dev

how to execute compound commands in shell script?

From Dev

How to execute git commands from bash script?

From Dev

How to execute the following commands in a bash script?

From Dev

Bash Script to execute Docker container operations and extract container IP Address

From Dev

Shell out into zsh and execute commands from bash script

From Dev

GNU Make: startup a Docker Container, make it execute commands, exit container, execute a script

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

Execute gcloud commands in a bash script

From Dev

Bash script to spawn and execute commands

From Dev

How to execute linux commands in a remote machine with shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

Can a bash shell script open a text file with less and then execute less commands from the script?

From Dev

How to execute shell script?

From Dev

Make a shell script execute commands in telnet or programs

From Java

How to run shell script on host from docker container?

From Dev

bash, shell script argument to skip part of script

From Dev

How does bash execute commands

From Dev

How to execute a bash script?

From Dev

How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to execute multiple Unix commands

Related Related

  1. 1

    How to execute shell script within a docker container

  2. 2

    How to execute shell commands in a loop from within a bash script?

  3. 3

    Cannot execute shell commands in bash script

  4. 4

    How to execute the vim commands through shell script

  5. 5

    How to execute commands in gnuplot using shell script?

  6. 6

    how to execute compound commands in shell script?

  7. 7

    How to execute git commands from bash script?

  8. 8

    How to execute the following commands in a bash script?

  9. 9

    Bash Script to execute Docker container operations and extract container IP Address

  10. 10

    Shell out into zsh and execute commands from bash script

  11. 11

    GNU Make: startup a Docker Container, make it execute commands, exit container, execute a script

  12. 12

    How to execute sudo commands with Expect & send commands in bash script?

  13. 13

    Execute gcloud commands in a bash script

  14. 14

    Bash script to spawn and execute commands

  15. 15

    How to execute linux commands in a remote machine with shell script

  16. 16

    Bash script: How to execute commands consecutively without waiting for the previous one?

  17. 17

    Bash script: How to execute commands consecutively without waiting for the previous one?

  18. 18

    Can a bash shell script open a text file with less and then execute less commands from the script?

  19. 19

    How to execute shell script?

  20. 20

    Make a shell script execute commands in telnet or programs

  21. 21

    How to run shell script on host from docker container?

  22. 22

    bash, shell script argument to skip part of script

  23. 23

    How does bash execute commands

  24. 24

    How to execute a bash script?

  25. 25

    How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

  26. 26

    bash script execute commands after ssh

  27. 27

    Bash script to read from a file and execute commands

  28. 28

    Bash script to read from a file and execute commands

  29. 29

    Bash script to execute multiple Unix commands

HotTag

Archive