TIL how to check if a port is open using only Bash
May 16, 2025
1 min read
Docker
Bash
Bash can open TCP connections via /dev/tcp without needing curl, wget, or nc installed.
This is handy for Docker healthchecks in minimal images:
services:
collabora:
image: collabora/code
healthcheck:
test: bash -c "exec 6<> /dev/tcp/localhost/9980"
interval: 15s
retries: 5
start_period: 60s
timeout: 10s
exec 6<> /dev/tcp/localhost/9980 tries to open file descriptor 6 as a read-write connection to localhost:9980.
If the port is not listening, it fails and the healthcheck reports unhealthy.