Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 201
Nan Xiao Avatar asked Sep 10 '25 10:09

Nan Xiao


1 Answers

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.

like image 112
James Mills Avatar answered Sep 13 '25 00:09

James Mills