7

Docker version 17.06.0-ce

I'm studying Docker by watching a video course.

The lecturer shows:

sudo docker run -ti ubuntu /bin/bash

Run docker with image ubuntu. And what troubles me is /bin/bash\. man docker run shows that /bin/bash is a command. That is docker run IMAGE [COMMAND]. Well, it is ok. But what is the difference between

sudo docker run -ti ubuntu 

and

sudo docker run -ti ubuntu /bin/bash

For me there is none. And the lecturer doesn't focus attention on the command. He said that we are just run docker. It was his first command in the course. And then he shows that we have been isolated from the host machine, we can freely ruin what we want without any damage (like rm -rf /bin).

I checked:

$ sudo docker run -ti ubuntu
root@aaf7cd26fe18:/# echo $SHELL
/bin/bash


$ sudo docker run -ti ubuntu /bin/bash
root@6b2570958216:/# echo $SHELL
/bin/bash

Well, for myself I decided to throw this /bin/bash part away as garbage.

But anyway I decided to ask you: maybe there is some difference between the two ways of running Docker? If there is, what it is like?

4

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.

1

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.