docker run ubuntu /bin/bash vs docker run ubuntu

Docker images can can specify that a certain command is to be run by default, using the CMD directive in the Dockerfile. And:

If the user specifies arguments to docker run then they will override the default specified in CMD.

As it happens, the default command specified for the Ubuntu Dockerfile is, in fact, bash:

CMD ["/bin/bash"]

So, for the specific case of the Ubuntu image, docker run ... ubuntu /bin/bash is no different from docker run ... ubuntu.

Of course, this needn't be true always. A Dockerfile for a database engine may run the database command by default. In that case, if you needed an interactive shell, you'll need to do docker run ... /bin/bash.

In general, you can't assume that docker run will give you an interactive shell. It's safer to specify /bin/bash if you need a shell.


When you do not provide the command, which in your case is /bin/bash, while using -ti (i interactive, t terminal) you will be attached to the default program which has been defined to be executed when using run command in DockerFile.

For example, if an image runs a web server on the foreground, what you will see after using run without the /bin/bash is the logs of that web server (default program which has been run).

While specifying the command, you are telling that I don't care what is going on or is running on the image give me a interactive terminal by running this "command".

In Ubuntu the default command is bash and if you do not provide -ti the container will be stopped right after getting run. because it ran the bash in non-interactive mode, and after it's finish the container has nothing to do anymore.

Tags:

Docker