8
  docker run -d ubuntu:14.04 /bin/bash -c "while true; do echo hello world;done"

  docker run -d ubuntu:14.04 /bin/bash "while true; do echo hello world; done"

I tried both.

In case 2, the container stopped immediately. So docker ps returns nothing. And docker ps -a returns just itself.

In case 1, docker ps lists the container. It is not stopped.

So what does -c flag do?

Chad
  • 1,750
  • 2
  • 16
  • 32
Gibbs
  • 21,904
  • 13
  • 74
  • 138

3 Answers3

11

From the bash manual page:

bash interprets the following options when it is invoked:

-c string

If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

Without the -c, the "while true..." string is taken to be a filename for bash to open.

Community
  • 1
  • 1
Bryan
  • 11,398
  • 3
  • 53
  • 78
5

The -c flag tells Bash that the next argument is a string of commands to run instead of the filename of a script. In the first case, Bash executes the next argument after -c. In the second case, where the -c flag isn't passed, Bash tries to run a script called while true; do echo hello world; done but can't find that script, so it quits.

From man bash:

-c string

If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

Quip Yowert
  • 444
  • 2
  • 7
0

In this situation(docker run -d ubuntu:14.04 /bin/bash -c "while true; do echo hello world;done"), you have run docker with the docker CMD with value:

/bin/bash -c "while true; do echo hello world;done"

-c is the argument for /bin/bash, not for docker.

you can run /bin/bash -c "while true; do echo hello world;done" outside docker to see what happened.

Lax
  • 71
  • 5
  • Since there is `while`-loop running in the container, the bash process won't exit, and you can see this in `docker ps`. In the other case, `/bin/bash "while true; do echo hello world; done"` exit quickly. – Lax Dec 25 '14 at 14:06