43

The answers from this question do not work.

The docker container always exits before I can attach or won't accept the -t flag. I could list all of the commands I've tried, but it's a combination of start exec attach with various -it flags and /bin/bash.

How do I start an existing container into bash? Why is this so difficult? Is this an "improper" use of Docker?

EDITS: I created the container with docker run ubuntu. The information about the container: 60b93bda690f ubuntu "/bin/bash" About an hour ago Exited (0) 50 minutes ago ecstatic_euclid

Community
  • 1
  • 1
Andrew
  • 839
  • 2
  • 7
  • 16
  • What was the container's original command? – jwodder Apr 14 '17 at 21:29
  • 1
    I typically use: `docker exec -i -t container-name /bin/bash`. Could it be that your container does not have `/bin/bash` ? – Arash Apr 14 '17 at 21:35
  • 2
    What's the image / Dockerfile? Several possible reasons, impossible to know without one / both of those pieces of info. – johnharris85 Apr 14 '17 at 21:47
  • @Arash I can't `exec` without the container started. @jwodder @johnharris85 I've added that information to my question. Thanks! – Andrew Apr 14 '17 at 22:20
  • So, the edit shows me that the container runs, does nothing and exits successfully. You say that the container won't accept the `-t` flag, and you've tried various combinations of `-it` etc... I would have thought (if you're using the `ubuntu` image) that `docker run -it ubuntu` should work just fine (which it does on my system). – johnharris85 Apr 15 '17 at 19:16
  • I'm missing the answer (for this old question) that you can, in most cases, start your container with a tail command to keep it alive, as explained here: https://stackoverflow.com/questions/45638784/how-to-retain-docker-alpine-container-after-exit-is-used. `docker run -d --name containername some/image tail -f /dev/null`. After that, you can attach to it using `docker exec -it containername /bin/sh` or if bash is installed in the container, /bin/bash – Tim Chaubet May 26 '23 at 07:56

2 Answers2

76

First of all, a container is not a virtual machine. A container is an isolation environment for running a process. The life-cycle of the container is bound to the process running inside it. When the process exits, the container also exits, and the isolation environment is gone. The meaning of "attach to container" or "enter an container" actually means you go inside the isolation environment of the running process, so if your process has been exited, your container has also been exited, thus there's no container for you to attach or enter. So the command of docker attach, docker exec are target at running container.

Which process will be started when you docker run is configured in a Dockerfile and built into a docker image. Take image ubuntu as an example, if you run docker inspect ubuntu, you'll find the following configs in the output:

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

which means the process got started when you run docker run ubuntu is /bin/bash, but you're not in an interactive mode and does not allocate a tty to it, so the process exited immediately and the container exited. That's why you have no way to enter the container again.

To start a container and enter bash, just try:

docker run -it ubuntu

Then you'll be brought into the container shell. If you open another terminal and docker ps, you'll find the container is running and you can docker attach to it or docker exec -it <container_id> bash to enter it again.

You can also refer to this link for more info.

Anil
  • 2,539
  • 6
  • 33
  • 42
shizhz
  • 11,715
  • 3
  • 39
  • 49
7

Here is a very simple Dockerfile with instructions as comments ... launch it to spin up a running container you can exec login to

FROM ubuntu:20.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  
#

A container will exit normally when it has no work to do ... if you give it no work it will exit immediately upon launch for this reason ... typically the last command of your Dockerfile is the execution of some flavor of a server which stays alive due to an internal event loop and in so doing keeps alive its enclosing container ... short of that you can mention a server executable which has been installed into the container as the final parameter of your call to

docker run -d  my-image-name  my-server-executable
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104