3

I want to implement an automatic bash script which enters a running docker container, and do some stuffs:

# cat docker.sh
#!/bin/bash -x

docker exec -it hammerdb_net8 bash
cd /data/oracle/tablespaces/
pwd

Executing the script on terminal:

# ./docker.sh
+ docker exec -it hammerdb_net8 bash
[root@npar1 /]#

The output shows only login the docker container, but won't do other operations.

Is there any method to automate entering docker container and doing other things?

1 Answer 1

5

You can use bash -c:

docker exec -it hammerdb_net8 bash -c 'cd /data/oracle/tablespaces/; pwd; ls'

For running a series of commands use here-doc in BASH:

docker exec -i hammerdb_net8 bash <<'EOF'
cd /data/oracle/tablespaces/
pwd
ls
EOF
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any difference between "docker ... bash <<'EOF'" and "docker ... bash <<EOF"?
Using 'EOF' makes all the lines not subjected to parameter expansion so e.g. you can use p=$PWD and $PWD will not be expanded in current shell.
Could you give an example?

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.