Running multiple programs in a Docker container from the command line

With Docker, you can specify the command to run inside the container on the command line. But what if you want to run multiple commands? You can’t escape the && syntax, or wrap the command in quotes, as Docker won’t recognise it. The trick here is to use

sh -c '<command1 && command2 [&& command3 [...]]>'

For example, to run date and whoami in a vanilla ubuntu container, we would run the following:

$ docker run ubuntu:latest sh -c 'date && whoami'
Wed Feb 18 00:42:53 UTC 2015
root

This works because sh -c '<date && whoami>' is one call; the commands will be split and executed from within sh.

Leave a Reply

Your email address will not be published. Required fields are marked *