Why does “docker attach” hang?

依然范特西╮ 提交于 2019-11-30 02:38:26

It does not really hang. As you can see in the comment below (You are running "/bin/bash" as command) it seems to be expected behaviour when attaching.

As far as I understand you attach to the running shell and just the stdin/stdout/stderr - depending on the options you pass along with the run command - will just show you whatever goes in/out from that moment. (Someone with a bit more in-depth knowledge hopefuly can explain this on a higher level).

As I wrote in my comment on your question, there are several people who have opened an issue on the docker github repo describing similar behaviour:

Since you mention shell, I assume you have a shell already running. attach doesn't start a new process, so what is the expected behavior of connecting to the in/out/err streams of a running process? I didn't think about this. Of course this is the expected behavior of attaching to a running shell, but is it desirable?

Would it be at all possible to flush stdout/stderr on docker attach thereby forcing the shell prompt to be printed or is it a bit more complex than that? That's what I personally would "expect" when attaching to an already running shell.

Feel free to close this issue if necessary, I just felt the need to document this and get some feedback.

  • Taken from a comment on this github issue. You can find more insight in the comments of this issue.

If instead of enter you would start typing a command, you would not see the extra empty prompt line. If you were to run

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

where <container-ID-or-name> is the ID or name of the container after you run docker run -it -d ubuntu (so 3aef6e642327 or condescending_sammet in your question) it would run a new command, thus not having this "stdout problem" of attaching to an existing one.

Example

If you would have a Dockerfile in a directory containing:

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

And have a simple bash script script.sh in the same directory containing:

#!/bin/bash

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

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

Then build (in this example in the same directory as the Dockerfile and script.sh) and run it with

$ 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 will end up attached to a container printing out the time every second. (CTRL-C to get out)

Example 2

Or if you would have a Dockerfile containing for example the following:

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

Then run 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

You would end up in a running irssi window without this particular behaviour. Of course you can substitute irrsi for another program.

I ran into this issue as well when attempting to attach to a container that was developed by someone else and already running a daemon. (In this case, it was LinuxServer's transmission docker image).

Problem:

What happened was the terminal appeared to 'hang', where typing anything didn't help and wouldn't show up. Only Ctrl-C would kick me back out.

docker run, docker start, docker attach all was not successful, turns out the command I needed (after the container has been started with run or start) was to execute bash, as chances are the container you pulled from doesn't have bash already running.

Solution:

docker exec -it <container-id> bash

(you can find the container-id from running docker ps -a).

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

I know the accepted answer has captured this as well, but decided to post another one that is a little more terse and obvious, as the solution didn't pop out for me when I was reading it.

This happened to me once for the following reason:

It could be that the bash command inside the container is executing a "cat" command.

So when you attach to the container (the bash command) you are actualy inside the cat command which is expecting input. (text and/or ctrl-d to write the file)

Billy Yang

When I run docker attach container-name, then nothing output, even Ctrl-c is invalid. So, first try

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

and then ctrl-c can stop it. Why it didn't output anything? just because the container doesn't output. Actually I need to enter my container and run some shell command. So the correct command is

docker exec -ti container-name bash

If you cannot access command line, just make sure you run your container with -i flag at start.

I just had a similar problem today and was able to fix it:

Here is what was happening for me:

docker-compose logs -f nginx
Attaching to laradock_nginx_1

Then it would hang there until I quit via CTRL-C: ^CERROR: Aborting.

docker ps -a showed that what SHOULD have been called laradock_nginx did not exist with that Image Name, so I figured I'd just remove and re "up" that container:

docker stop cce0c32f7556
docker rm cce0c32f7556
docker-compose up -d laradock_nginx

Unfortunately: ERROR: No such service: laradock_nginx

So I did a sudo reboot and then docker ps -a, but laradock_nginx still wasn't there.

Luckily, docker-compose up -d nginx then worked and docker-compose logs -f nginx now works.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!