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

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

  1. Sam says:

    Thanks for this!

    I wanted to exec multiple commands in a running container with docker exec ( https://docs.docker.com/reference/commandline/cli/#exec ) and your trick works 🙂