pipe and grep in Docker container seems to get blocked pipe and grep in Docker container seems to get blocked docker docker

pipe and grep in Docker container seems to get blocked


This one has me puzzled! But I have a solution for you:

Use awk instead of grep

In test.sh use this instead:

netstat -anpt | awk /tcp/

So that the file looks like this:

#!/bin/bashset -elogit() {    log_date=`date +"%F %T"`    echo "[$log_date][INFO] $1"}waitForServerToStart() {    while true; do        logit "Testing .... 1"        netstat -anpt        logit "Testing .... 2"        netstat -anpt | awk /tcp/        logit "Testing .... 3"        sleep 5        logit "Testing .... 4"    done}waitForServerToStart

For a reason that I cannot explain - grep will not return when reading from the pipe when invoked from the script. I created your container locally, ran it and entered it - and the command netstat -anpt | grep tcp runs just fine and exits. If you replace it with netstat -anpt | cat in your test.sh script, then it will also pass just fine.

I looked all over the place for the someone with an identical issue with grep in a container from the distro you are using, the version etc. - but came up empty handed.

I believe that it may have to do with grep waiting for a EOF character that never lands - but I am not sure.