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
Here is a very simple Dockerfile with instructions as comments ... give it a whirl and see if you then have a working container you can exec login to
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 as the final parameter of your call to
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
orenter
. So the command ofdocker attach
,docker exec
are target at running container.Which process will be started when you
docker run
is configured in aDockerfile
and built into a docker image. Take imageubuntu
as an example, if you rundocker inspect ubuntu
, you'll find the following configs in the output: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: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 candocker attach
to it ordocker exec -it <container_id> bash
to enter it again.You can also refer to this link for more info.