Nan Xiao
Nan Xiao

Reputation: 17467

How to enter bash of an ubuntu docker container?

I want to run an ubuntu container and enter bash:

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

The ubuntu container exits directly. How can I enter the bash?

Upvotes: 4

Views: 4753

Answers (1)

James Mills
James Mills

Reputation: 19030

Use -i and -t options.

Example:

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

See: Docker run Reference

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

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

-t, --tty=false Allocate a pseudo-TTY

Update: The reason this works and keeps the container running (running /bin/bash) is because the -i and -t options (specifically -i) keep STDIN open and so /bin/bash does not immediately terminate thus terminate the container. -- The reason you also need/want -t is because you presumably want to have an interactive terminal-like session so t creates a new pseudo-tty for you. -- Furthermore if you looked at the output of docker ps -a without using the -i/-t options you'd see that your container terminated normally with an exit code of 0.

Upvotes: 11

Related Questions