docker run <IMAGE> <MULTIPLE COMMANDS> – Dev

The best answers to the question “docker run <IMAGE> <MULTIPLE COMMANDS>” in the category Dev.


I’m trying to run MULTIPLE commands like this.

docker run image cd /path/to/somewhere && python

But this gives me “No such file or directory” error because it is interpreted as…

"docker run image cd /path/to/somewhere" && "python"

It seems that some ESCAPE characters like “” or () are needed.

So I also tried

docker run image "cd /path/to/somewhere && python"
docker run image (cd /path/to/somewhere && python

but these didn’t work.

I have searched for Docker Run Reference but have not find any hints about ESCAPE characters.


You can do this a couple of ways:

  1. Use the -w option to change the working directory:

    -w, –workdir=”” Working directory inside the container–w

  2. Pass the entire argument to /bin/bash:

    docker run image /bin/bash -c "cd /path/to/somewhere; python"


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

docker run image_name /bin/bash -c "cd /path/to/somewhere; python"

In case we need command2 (python) will be executed if and only if command1 (cd) returned zero (no error) exit status, use && instead of ;

docker run image_name /bin/bash -c "cd /path/to/somewhere && python"


bash -c works well if the commands you are running are relatively simple. However, if you’re trying to run a long series of commands full of control characters, it can get complex.

I successfully got around this by piping my commands into the process from the outside, i.e.

cat | docker run -i <image> /bin/bash


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

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

But, without invoking the shell in the remote the output will be redirected to the local terminal.