socat: Usage, Options & Common CI Errors
socat relays bytes between any two endpoints - sockets, files, pipes, TTYs.
socat is netcat on steroids: it connects two addresses of almost any type, which makes it the tool for port-forwards and bridging a UNIX socket to TCP in CI. Its address syntax is the learning curve.
What it does
socat opens two "address" channels (TCP, UDP, UNIX sockets, files, STDIO, EXEC, …) and shuttles data between them bidirectionally. It is the general-purpose relay used to forward ports, expose UNIX sockets over TCP, and proxy connections.
Common usage
socat - TCP:localhost:6379 # interactive client
socat TCP-LISTEN:8080,fork TCP:backend:80 # simple TCP forwarder
socat TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
socat UNIX-LISTEN:/tmp/s.sock,fork TCP:host:5432
echo "PING" | socat - TCP:host:6379Options
| Address / item | What it does |
|---|---|
| TCP:host:port | Connect out to a TCP endpoint |
| TCP-LISTEN:port | Listen for inbound TCP |
| UNIX-CONNECT / UNIX-LISTEN | Connect to / listen on a UNIX socket |
| - | STDIO (your terminal/pipe) |
| fork | Handle each new connection in a child |
| reuseaddr | Allow immediate re-bind of the port |
Common errors in CI
"socat ... E ... bind: Address already in use" - a previous socat (or the real service) still holds the port; add reuseaddr or stop it. Without fork, a LISTEN address handles one connection then exits, so the second client gets refused - add ,fork for a persistent forwarder. "socat: command line ... unknown option" usually means lowercase/uppercase or comma-option mistakes (options attach to the address with commas, e.g. TCP-LISTEN:80,fork,reuseaddr). A UNIX-CONNECT to a missing socket gives "No such file or directory".