How do I run a Docker-ubuntu container in bash?

The answer to this question doesn't work.

The docker container always exits before I may attach

or may not accept a flag -t

. I could list all the commands I tried, but this is a combination start

exec

attach

with different flags -it

and /bin/bash

.

How do I start an existing container in bash? Why is it so hard? Is this the "wrong" use of Docker?

edits: I created a container with docker run ubuntu

. Container information:60b93bda690f ubuntu "/bin/bash" About an hour ago Exited (0) 50 minutes ago ecstatic_euclid

+10


source to share


2 answers


First of all, a container is not a virtual machine, a container is an isolation environment for starting a process, the life cycle of a container is associated with a process running internally, when the process exits, the container also exits and the isolation environment is not running. The meaning of "attach to container" or "enter container" actually means that you are entering the isolation environment of a running process , so if your process exited, your container was also deleted, so there is no container for you before attach

or enter

. Thus, the command docker attach

is docker exec

defined in the running container .

What process will run if docker run

configured in Dockerfile

and embedded in docker image. Taking the image ubuntu

as an example, if you run docker inspect ubuntu

, you will find the following configurations in the output:

"Cmd": ["/bin/bash"]

      

which means that the process started when you run docker run ubuntu

is /bin/bash

, but you are not interactively and allocate a tty to it, so the process exited immediately and the container exited. This is why there is no way for you to enter the container again.



To start a container and enter bash

, just try:

docker run -it ubuntu

      

Then you will be taken to the container shell. If you open another terminal and docker ps

, you will see that the container is running and you can go docker attach

to it or docker exec -it <container_id> bash

to enter it again.

You can also refer to this link for more information.

+17


source


Here is a very simple Dockerfile with instructions in the comments ... turn around and see if you have a working container that you can login to

FROM ubuntu:19.04

ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get install -y  

CMD ["/bin/bash"]


# ... save this file as Dockerfile then in same dir issue following
#
# docker build --tag stens_ubuntu .   # creates image stens_ubuntu
#
# docker run -d  stens_ubuntu  sleep infinity # launches container 
#
# docker ps     #   show running containers
#
# 
# ... find CONTAINER ID from above and put into something like this
#
# docker exec -ti $( docker ps | grep stens_ubuntu | cut -d' ' -f1 ) bash   #  login to running container
# docker exec -ti 3cea1993ed28 bash   #  login to running container using sample containerId  
#

      



The container will shutdown normally when it has no work ... if you are not running it, for that reason it will exit right after startup ... usually the last command of your Dockerfile is some kind of server execution that stays alive due to inner event loop and still maintains its enclosing container ... apart from the fact that you can mention the server executable as the last parameter of your call

docker run -d  my-image-name  my-server-executable

      

+3


source







All Articles