Docker Community Forums

Share and learn in the Docker community.

Netcat example not working


(Littlewing1208) #1

I’m following some of the Getting Started guides and the following one does not work for me:

https://docs.docker.com/engine/userguide/basics/#bind-a-service-on-a-tcp-port

Everything is fine until you pipe the echo output to netcat, then the container exits but there is nothing in the logs at the end. I tested it by doing a bash session:

docker run -i -t -p 4444 ubuntu /bin/bash

And then doing a

nc -kl 4444

And in the host, sending text to the forwarded port and I’m seeing that the container is outputting the messages, but if I just do as the example has:

JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)

Then the message is never (seemingly) put on stdout and into the docker logs before the container exits.

Any ideas as to why I’m seeing this behavior?

I’m running docker 1.9 in Ubuntu Trusty.

EDIT:

More observations:

docker run -d -p 4444 ubuntu /bin/bash -c "/bin/nc -l 4444; echo hey"

I ran this and the ‘hey’ is always output in the docker logs, but netcat does not output the text that I sent to it on the forwarded port (to 4444) to the docker logs.


(Littlewing1208) #2

Sorry to answer my own question. Seems the issue is that nc (server side) exits before the received text is written (whether it be redirected to a file or just to stdout) meaning it never makes it to the docker logs before the container exits.

Solution is to pass -q 1 to the nc command:

$ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l -q 1 4444)