I have a squid proxy running in a docker container, started like this:

docker run --privileged -d --publish=3128:3128 --name foo squid

I want to get some information from it, from another container called bar.

docker run --rm -it --name bar ubuntu:trusty bash

If I run this in the bar container:

nc 3128

Then I get some output from the server. But if I run this:

echo 'HEAD /' | nc 3128

I get nothing. Why not?

1 Answer 1


Probably because netcat exits shortly after getting an end-of-file on its standard input.  Try

(echo 'HEAD /'; sleep 5) | nc 3128

to keep netcat's local input open long enough for it to read the network data and write it to the standard output.  The 5 is the number of seconds to delay.

Netcat has an option to deal with this. If you specify a negative quit timeout, it will wait for the network connection to close instead of quitting after EOF on stdin:

echo 'HEAD /' | nc -q -1 3128
  • OK, that works. But if I run it on my host (not in a container), I don't need the sleep 5. Is there a difference in the buffer sizes, or something?
    – z0r
    Apr 6, 2016 at 6:03
  • Ah, apparently netcat has a flag to help with this: -q -1 will tell it not to quit after receiving EOF; it waits instead for the connection to close.
    – z0r
    Apr 6, 2016 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.