# UFW + Docker: Letting Docker Containers Reach Host Services

When Docker containers need to reach services running on the host (e.g., llama-swap on :9292), UFW can block them even though `host.docker.internal` resolves correctly.

## The Problem

Docker containers connect from bridge network IPs (e.g., 172.17.0.0/16, 172.21.0.0/16), not the host's LAN IP. If UFW only allows the LAN IP, those container requests are silently dropped.

## Symptoms

- `wget http://host.docker.internal:9292/v1/models` times out from inside the container
- `ping host.docker.internal` works fine
- Host curl to localhost:9292 works fine

## The Fix

Allow the Docker bridge subnets through UFW for the needed port:

```bash
# Find your Docker bridge networks
ip addr show docker0 | grep inet          # typically 172.17.0.0/16
docker network inspect bridge --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}'

# Find custom Docker networks
docker network inspect ai-web --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}'

# Allow them in UFW
sudo ufw allow from 172.17.0.0/16 to any port 9292 proto tcp
sudo ufw allow from 172.21.0.0/16 to any port 9292 proto tcp
```

## Verification

```bash
# From inside the container
docker exec <container> wget -q -O - http://host.docker.internal:9292/v1/models

# Full chat completion test
docker exec <container> wget -q -O - --post-data='{"model":"model-name","messages":[{"role":"user","content":"hi"}],"max_tokens":5}' http://host.docker.internal:9292/v1/chat/completions
```

## Notes

- `host.docker.internal` requires `extra_hosts: ["host.docker.internal:host-gateway"]` in docker-compose.yml
- On Linux, this maps to the Docker bridge gateway (e.g., 172.17.0.1)
- UFW's default `deny (incoming)` policy blocks traffic from non-LAN subnets
- This applies to any host service, not just llama-swap
