Spent a while on this today and it is worth writing down, because the symptom points nowhere near the cause.

A container needed to reach a mail server running in another container on the same host. The mail server publishes 25, 465 and 587 on the host. From the host itself, telnet localhost 587 gives a banner immediately. From inside the other container, the same address times out. HTTPS on 443 to the identical host worked fine, which made it look like a mail-specific problem.

It is not. Check the DOCKER iptables chain:

iptables-save | grep -E "dport (25|465|587)"

There were ACCEPT rules for those ports, but scoped to one specific destination address on docker_gwbridge. Anything on a different bridge network was dropped by the chain default. Worse, that destination address belonged to a container that no longer existed: the service had been migrated from a Swarm service to a plain container months earlier, and the rule was left behind pointing at a dead IP.

Three things I would do differently next time:

  1. Test from inside the container’s network namespace, not from the host. docker run --rm --network container:<name> curlimages/curl ... shares the exact namespace. Testing from the host proves nothing about what the container can reach.
  2. sh does not support /dev/tcp. An early test using </dev/tcp/host/587 inside a minimal image reported “unreachable” when the real answer was “your shell is dash”. That is a false negative that sends you down the wrong path.
  3. The fix was the container’s own default gateway. Connecting to the gateway address reaches the host’s published port through INPUT rather than the DOCKER chain. In compose, extra_hosts: ["service.example:host-gateway"] keeps the hostname intact so TLS still validates.

The general lesson: when a container-to-container path fails but the host-to-service path works, suspect the FORWARD/DOCKER chain before you suspect the application. And go looking for stale rules after any migration between Swarm services and plain containers.