Why does docker attach hang?
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:
- docker attach [container] hangs, requires input #8521
- docker attach hangs setting terminal state when attaching to container
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.
Why docker attach not working and blocking my konsole on ubuntu
Try instead a docker exec
, for debugging purpose:
docker exec -it test bash
(assuming your test image has a bash
installed, or at least an sh
. If based on Alpine, an ash
)
docker attach
uses the container’s stdio/stderr, so you need to makle sure your script is actually flushing/printing something.
Any commands hang inside docker container
The solution is to use the flag -i/--interactive
with docker run
. Here is a relevant section of the documentation:
--interactive , -i
Keep STDIN open even if not attached
docker run command hangs until the container stops
To prevent this, you should start a container in detached mode by specifying -d=true
or just -d
option. If this is not specified, docker defaults to foreground mode which attaches the console to the process inside your container's standard input, output and standard error, which makes it appear as if your terminal is "hanging".
So try:
docker run -d pptpserver
You can read more about this behaviour in the Docker Run Reference documentation.
Related Topics
Sed Replacement Not Working When Using Variables
Platform Independent Resource Management
Write(2)/Read(2) Atomicity Between Processes in Linux
How to Remove Folders with a Certain Name
Start Jboss 7 as a Service on Linux
When Setting Ifs to Split on Newlines, Why Is It Necessary to Include a Backspace
How to Post Raw Body Data with Curl
Hard Time in Understanding Module_Device_Table(Usb, Id_Table) Usage
Examining C/C++ Heap Memory Statistics in Gdb
Linux Configure/Make, --Prefix
How to Access Team Foundation Server (Tfs) from Linux
How to Use Iptables in Linux to Forward Http and Https Traffic to a Transparent Proxy
Save in a Variable The Number of Seconds a Process Took to Run
Bash Script to Find The Frequency of Every Letter in a File
Gvim Redraw Issues with Virtual Box and Windows 7 Host
Manage Source Under Git and Svn Simultanously - Does It Make Sense