How to run multiple commands with ‘docker run’ command

How to upgrade terraform to v0.12 version
May 25, 2019
Bottlerocket – an open-source operating system for containers
March 11, 2020

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"

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"

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"