How to inject the bash of an Ubuntu docker container?

I want to start a container ubuntu

and enter bash

:

[root@localhost backup]# docker run ubuntu bash
[root@localhost backup]#

      

The container ubuntu

exits directly. How can I enter bash

?

+3


source to share


1 answer


Use the -i

and options -t

.

Example:

$ docker run -i -t ubuntu /bin/bash
root@9055316d5ae4:/# echo "Hello Ubuntu"
Hello Ubuntu
root@9055316d5ae4:/# exit

      



See: Docker run

Link

$ docker run --help | egrep "(-i, | -t,)"

-i, --interactive = false Keep STDIN open even if not connected

-t, --tty = false Select pseudo-topics

Update: The reason this works and keeps the container running (does /bin/bash

) is because the options -i

and -t

(in particular -i

) keep STDIN

open and therefore /bin/bash

does not terminate the container immediately. - The reason you also want / need -t

is because you presumably want to have an interactive session like a terminal, so t

creates a new pseudo-tty for you. - Also, if you view the output docker ps -a

without using the -i

/ options -t

, you will see that your container exited normally with an exit code 0

.

+10


source