How to run multiple commands with ‘docker run’ command

terraform_Logo
How to upgrade terraform to v0.12 version
May 25, 2019

How to run multiple commands with ‘docker run’ command

Sometimes we need to run multiple shell commands in docker when we start the docker container

To run multiple commands in docker, use /bin/bash -c and semicolon ;

 docker run image_name /bin/bash -c "cd /path/to/dir; sh run.sh"

Likewise, we can add multiple commands to the above line separated by a semicolon ;

Similarly, we can add && if we want to run the second command only if the first command is successful.

docker run image_name /bin/bash -c "cd /path/to/dir && sh run.sh"

You can also pipe commands inside Docker container, bash -c "<command1> | <command2>"

for example:

docker run image_name /bin/bash -c "ls -1a | wc -l"

And when bash is not available then you can use the sh -c.

docker run image_name sh -c "cd /path/to/dir && sh run.sh"
Bitnami