Coder Perfect

Why does “docker attach” take so long to complete?


I can successfully launch an Ubuntu container:

# docker run -it -d ubuntu
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3aef6e642327        ubuntu              "/bin/bash"         3 seconds ago       Up 2 seconds                            condescending_sammet

Docker attach, on the other hand, hangs:

# docker attach 3aef6e642327

Until I press a key, for example, Enter:

# docker attach 3aef6e642327
root@3aef6e642327:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Why is it that Docker attaches a hang?


I believe I have the answers after reading the comments:


“docker attach” re-uses the same tty rather than creating a new one.

(1) Using docker run without using daemon mode:

# docker run -it ubuntu

If everything seems good, execute the ls command:

root@eb3c9d86d7a2:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

(2) Start docker daemon mode:

# docker run -it -d ubuntu

Actually, the running container should have outputted the following to stdout:


Docker connect appears to hang, however it is actually waiting for your input:

# docker attach 91262536f7c9
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Asked by Nan Xiao

Solution #1

It doesn’t actually dangle. It appears to be typical behavior when attaching, as you can see in the comment below (You are executing “/bin/bash” as command).

As far as I understand it, you attach to the running shell and stdin/stdout/stderr will just display you whatever comes in/out from that point forward, depending on the options you supply along with the run command. (Hopefully, someone with a little more in-depth understanding will be able to explain this at a higher level.)

Several individuals have created an issue on the docker github repo expressing similar behavior, as I said in my response to your question:

You wouldn’t see the extra empty prompt line if you started entering a command instead than pressing enter. If you had to run, what would you do?

$ docker exec -it ubuntu <container-ID-or-name> bash 

When container-ID-or-name> is the container’s ID or name after you execute docker run -it -d ubuntu (like 3aef6e642327 or condescending sammet in your query), it will perform a new command instead of connecting to an existing one, avoiding the “stdout problem.”

If you have a Dockerfile in a directory with the following contents:

FROM ubuntu:latest
ADD ./ / 
RUN chmod +x /
CMD ["/"]

Also, in the same directory, have a basic bash script that contains:


#trap ctrl-c and exit, couldn't get out
#of the docker container once attached
trap ctrl_c INT
function ctrl_c() {

while true; do
    time=$(date +%N)
    echo $time;
    sleep  1;

Then build and run it (in this case, in the same directory as the Dockerfile and

$ docker build -t nan-xiao/time-test .
..stuff happening...
$ docker run -itd --name time-test nan-xiao/time-test

Finally attach

$ docker attach time-test

You’ll be tethered to a container that prints the time every second. (To exit, press CTRL-C.)

Alternatively, if you have a Dockerfile that includes, for example, the following:

FROM ubuntu:latest
RUN apt-get -y install irssi
ENTRYPOINT ["irssi"]

Then run the following commands in the same directory:

$ docker build -t nan-xiao/irssi-test .

Then run it:

$ docker run -itd --name irssi-test nan-xiao/irssi-test

And finally

$ docker attach irssi-test

Without this exact behavior, you’d end up with a running irssi window. Of course, you can use another program instead of irrsi.

Answered by Francesco de Guytenaere

Solution #2

I had the same problem when trying to connect to a container that had been constructed by someone else and was already running a daemon. (In this case, the transmission docker image from LinuxServer.)

What happened was that the terminal looked to ‘hang,’ meaning that typing anything didn’t help and the screen didn’t appear. I could only get out by using Ctrl-C.

Because docker run, docker start, and docker attach all failed, it turned out that the command I needed (after the container had been launched with run or start) was to execute bash, because the container you pulled from probably didn’t already have bash running.

exec docker -it container-id> bash

(Using docker ps -a, you may get the container-id.)

This will bring you into the instance as root with a working bash (assuming there was no other explicit set up done by the image you pulled).

I know the accepted answer does as well, but I chose to post another that is a little more concise and apparent because the solution didn’t jump out at me when I read it.

Answered by matrixanomaly

Solution #3

When I run docker attach container-name, nothing happens, not even Ctrl-c. So, first and foremost, try.

docker attach container-name --sig-proxy=false

Then press ctrl-c to stop it. Why didn’t it produce anything? simply because the container isn’t able to export. In fact, I need to log into my container and run a shell command. So this is the proper command:

docker exec -ti container-name bash

Answered by Billy Yang

Solution #4

This happened to me once because of the following:

It’s possible that the container’s bash command is running a “cat” command.

As a result, when you connect to the container (using the bash command), you are actually within the cat command, which is waiting for input. (To write the file, use text and/or ctrl-d)

Answered by HerbalMart

Solution #5

If you don’t have access to the command line, start your container with the -i flag.

Answered by Ali Motameni

Post is based on