TCP requests with plain bash in Linux

Recently I had a problem where I need to check if specific port of specific host was available before I can proceed further. Basically it was a docker composer running a container which depends on mysql service and I had to make sure mysql service was available before I can continue with my main docker container.

First I looked into nc implementation of a waiting part in the container entrypoint that looked something like this:

sh -c 'until nc -z mysql_host 3306; do sleep 1; done'

but it turned out I didn’t have nc installed in the container and was lazy enough to rebuild the whole thing just for this tiny tool.

Apparently, this discussion pointed out a solution:

sh -c 'until printf "" 2>>/dev/null >>/dev/tcp/mysql_hostname/3306; do sleep 1; done'

which was something I never saw before, so a bit of googling brought me to this nice page with an example how to make HTTP request and get response while working with /dev/tcp. Useful to know, so I am leaving it here.