2

After following instructions to setup a blockchain system, my colleague moved everything into a script since it needs to be run multiple times.

However as a script it fails on the following line:

docker exec -it cli bash

When entering it into a terminal use user and path change to root@someaddress which I assume is the new docker container. Then -it cli bash should create a new bash environment to run the rest of the commands.

Is there any reason this doesn't work as a shell script?

3
  • did you use your script with sudo permission? You must have an error printed in the terminal if the script failed, can you add it in your question?
    – damadam
    Jun 28, 2018 at 9:50
  • Tried with and without sudo, I'll add the error now
    – MikeS159
    Jun 28, 2018 at 9:53
  • It doesn't actually give an error, but no further commands are run.
    – MikeS159
    Jun 28, 2018 at 10:13

1 Answer 1

-1

Bash command in docker

Welcome to Docker ;-) The command docker run -it cli bash starts the Docker image cli and than runs the bash command as login shell inside the container. This is way you get the root@someaddress prompt. So everything is as expected.

If you want to execute a command inside the container (and exit the container after that), use this:

docker run -it cli bash -c "echo hello from \$host; ls -la /"

Beware, quoting is important after the -c switch. If you need more information on the command_string checkout the bash manpage.

Run command(s) in container

There are many ways to get bash commands executed inside the container:

# mount the script into the container with -v
script=$(readlink -f myscript.sh)
docker run -it -v $script:$script cli bash $script

# use bash array to pass the commands into the container
cmd_list=()
cmd_list+=(echo cmd1)
cmd_list+=(echo cmd2)
cmd_list+=(echo done)
docker run -it cli bash -c "${cmd_list[*]}"

# google: bash here script
6
  • I think multiple commands are run inside the container. Will the -c cause the container to exit after the command in quotes?
    – MikeS159
    Jun 28, 2018 at 11:12
  • yes, the container will always exit, when the command terminates Jun 28, 2018 at 11:12
  • When you enter that command outside of a script, the container doesn't exit. Is it possible to run multiple commands like this?
    – MikeS159
    Jun 28, 2018 at 11:24
  • That is what my example does: -c "echo cmd1; echo cmd2; true && echo cmd3". You can also pipe full scripts to the bash... but that would be a bash no a docker question. Jun 28, 2018 at 11:29
  • There are quire a few commands afterwards, and some are quite long. Is it possible to separate them out onto multiple lines rather than having them in quotes?
    – MikeS159
    Jun 28, 2018 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .