Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run bash in detached mode in docker?

Tags:

docker

docker run -d ubuntu bash Container will immediately exit. What I need is keeping the container run, and I can use exec to login into this container.

In fact, I need do these things in a bash script:

docker run -it ubuntu bash
docker run -it centos bash

But it does not work.

like image 823
Sato Avatar asked Jan 04 '23 17:01

Sato


1 Answers

Another option is to use -i and -d together (I expected them to be mutually exclusive and was surprised to learn they are not).

See example output below, I started an Ubuntu container with -itd flags, then waited a couple of minutes and ran docker ps to confirm that the container is still running.

> docker run -itd ubuntu
03c55e9ba9de3e0b80ad9f3e0629dc63f4ab65291b79e133af2b392030ffc17d

> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
03c55e9ba9de        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            gallant_hypatia
like image 106
Roman Avatar answered Jan 11 '23 19:01

Roman