Bridge Docker containers to host localhost services via socat. Solves the #1 networking issue in containerized AI agent deployments: containers can't reach services bound to 127.0.0.1.
localhost-bridge — Connect containers to host localhost services
⚠️ Security & Privileges
This skill requires host-level privileges. It must be reviewed and executed manually by an administrator — never autonomously by an agent.
What it does on the host:
- Creates a systemd service (persistent across reboots) that forwards traffic from a Docker bridge IP to localhost
Adds a UFW firewall rule scoped to a specific Docker bridge interface
Requires sudo, Docker daemon access, and socat from your distro's official package repository
Before running any command:
1. Review the generated /etc/systemd/system/socat-<SOURCE_NETWORK>-<TARGET_SERVICE>-<PORT>.service file — confirm ExecStart binds only to the intended Docker bridge IP (172.x.x.1), never 0.0.0.0
Review the UFW rule — confirm it targets the correct br-<ID> interface and port
After setup, verify the port is NOT reachable from the public network: curl --connect-timeout 2 http://<PUBLIC_IP>:<PORT>/ must fail
Test from inside a container before deploying widely
Do not grant an automated agent permissions to run these commands without human approval.
The Problem
A service on the host listens on 127.0.0.1 (AI gateway, MCP server, Ollama, database...). A Docker container needs to reach it. localhost inside the container points to the container itself, not the host. Requests either timeout silently (firewall drops packets) or get connection refused.
The Solution
INLINECODE6 listens on the Docker bridge gateway IP and forwards to host loopback. Combined with a scoped firewall rule, this gives containers access without exposing the service externally.
Setup (run manually as admin)
1. Find the Docker bridge gateway IP
CODEBLOCK0
2. Create a systemd service
Replace <GATEWAY_IP>, <PORT>, <SOURCE_NETWORK>, and <TARGET_SERVICE> with your values.
Naming convention:socat-<source_network>-<target_service>-<port> — source network is the Docker network (consumer), target service is the host service. Self-documenting.
Review the ExecStart line before enabling — confirm it binds to the Docker bridge IP only.
CODEBLOCK1
3. Add firewall rule (MANDATORY)
Without this, socat listens but packets from the container are silently dropped — causing 30-second timeouts with no error.
Review the bridge ID before applying — a wrong ID can expose services.
CODEBLOCK2
4. Verify security
CODEBLOCK3
Multi-Network Workers
A container can be on multiple Docker networks. Each has its own bridge IP. You need a socat instance + firewall rule for each network the container uses. In practice, one network is usually enough.
socat not running | systemctl status socat-<SOURCE_NETWORK>-<TARGET_SERVICE>-<PORT> |
| Works then stops after Docker restart | Bridge IP changed | Check new gateway IP, update socat bind |
| socat won't start after reboot | Docker not ready | Ensure After=docker.service in unit file |
Alternatives
Depending on your security posture, consider:
- Docker host networking (network_mode: host) — simpler but removes all container network isolation