mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Docker on a VPS with a host firewall: DOCKER-USER script and documentation (iptables nft/legacy, ordering, systemd hook, ufw notes)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -64,6 +64,67 @@ $PQ down # stop (WITHOUT -v: "down -v" deletes t
|
||||
- **Rate limiting**: it relies on the IP address nginx sees. On a standard Docker host this is the client's; behind another
|
||||
proxy it would be the proxy's.
|
||||
|
||||
## Host firewall (iptables, ufw)
|
||||
|
||||
Docker writes its own firewall rules (`DOCKER`, `DOCKER-USER` chains, address translation). A strict host firewall can
|
||||
break them. **Typical symptom**, as early as the build: `apk add` or `apt-get` print `DNS: transient error` or
|
||||
`Temporary failure resolving`, while the host itself reaches the Internet. The failures met on a real VPS (Debian,
|
||||
nginx and MariaDB already present, custom iptables firewall, `DROP` policies):
|
||||
|
||||
| Cause | How to recognise it | Remedy |
|
||||
|---|---|---|
|
||||
| The firewall script flushes all chains (`iptables -F`, `-X`, `-t nat -F`) **after** Docker started | `iptables -S DOCKER-USER`: "No chain/target/match by that name" | restart Docker **after** the firewall (see below) |
|
||||
| Two engines mixed: `iptables` on nftables, rules from Docker or another tool in legacy | `iptables -V` shows `(nf_tables)` and the "iptables-legacy tables present" message; compare `iptables-nft -S` and `iptables-legacy -S` | **a single engine**: the one holding the host firewall; do not switch lightly (you would lose the rules). Docker uses the engine of the `iptables` command |
|
||||
| `FORWARD` on `DROP` with nothing allowing containers | containers reach nothing (DNS included) | the `DOCKER-USER` rules below |
|
||||
| Rules written with `iptables -I` in the wrong order | a `DROP` for the public interface runs before the `ACCEPT ... ESTABLISHED`: replies from the Internet are dropped | write with `-A` on an emptied chain (the provided script does) |
|
||||
|
||||
### Setup (iptables)
|
||||
|
||||
The `docker/firewall/docker-user-rules.sh` script (tested on this VPS) sets up the `DOCKER-USER` chain, which Docker
|
||||
consults before its own rules: everything is accepted for containers and replies, **only the `PUBLIC_PORTS` (80 and
|
||||
443) are reachable from the Internet**, anything else arriving on the public interface is refused (without it, every
|
||||
port published by a container would be open to the world: Docker bypasses `INPUT` rules).
|
||||
|
||||
```bash
|
||||
install -m 755 docker/firewall/docker-user-rules.sh /usr/local/sbin/docker-user-rules.sh
|
||||
mkdir -p /etc/systemd/system/docker.service.d
|
||||
printf '[Service]\nExecStartPost=-/usr/local/sbin/docker-user-rules.sh\n' > /etc/systemd/system/docker.service.d/firewall.conf
|
||||
systemctl daemon-reload && systemctl restart docker
|
||||
iptables -S DOCKER-USER # the rules are there
|
||||
docker run --rm alpine apk add --no-cache bash # the container reaches the Internet
|
||||
```
|
||||
|
||||
- The **`-`** before the `ExecStartPost` path is essential: without it, a script error (missing or non-executable file:
|
||||
`status=203/EXEC`) stops Docker, hence every container on the machine.
|
||||
- Variables: `PUBLIC_PORTS="80 443 8883"` for other ports published by containers that must stay public;
|
||||
`TRUSTED_IFS="wg0"` to never filter a VPN. Put them in the unit (`Environment=` in the same `.conf`) rather than in the
|
||||
script.
|
||||
- **Order at boot**: the firewall must run **before** Docker (`Before=docker.service` in its service), or end with
|
||||
`systemctl is-active --quiet docker && systemctl restart docker`. For containers to survive that Docker restart, set
|
||||
`"live-restore": true` in `/etc/docker/daemon.json`.
|
||||
- Do **not** put these rules in the general firewall script: it empties the chains, so `DOCKER-USER` does not exist yet
|
||||
when they would run.
|
||||
- To open a test on other ports (`--http-port`, `--https-port`), add them to `PUBLIC_PORTS` for the duration of the
|
||||
test, or go through your VPN.
|
||||
- Also check that no general rule cancels the firewall: for example `iptables -I INPUT -m conntrack --ctstate
|
||||
NEW,ESTABLISHED -j ACCEPT` accepts **every** new inbound connection.
|
||||
|
||||
### ufw
|
||||
|
||||
*Not tried here; standard Docker guidance.* `ufw` does not filter ports published by Docker (`ufw allow/deny` rules do
|
||||
not apply to them): a published port is open even if `ufw` denies it. To control it, use the same `DOCKER-USER` chain (the
|
||||
script above works with `ufw`, which also uses `iptables`) or the `ufw-docker` project. If containers have no outbound
|
||||
access, set `DEFAULT_FORWARD_POLICY="ACCEPT"` in `/etc/default/ufw`, then `ufw reload`, and allow the traffic of `docker0`
|
||||
and the `br-…` networks.
|
||||
|
||||
### Other points specific to a VPS that already hosts something else
|
||||
|
||||
- **Ports 80 and 443**: a host nginx and the Docker stack cannot share them (`address already in use`). For a test, stop
|
||||
nginx; for lasting coexistence, use `--http-port`/`--https-port` behind another proxy, or the classic installation
|
||||
instead of Docker.
|
||||
- **Host MariaDB**: no conflict, the Docker stack's database is not published.
|
||||
- **Let's Encrypt**: port 80 must be reachable from the Internet (hence in `PUBLIC_PORTS`).
|
||||
|
||||
## Verified
|
||||
|
||||
`make test-docker` builds the images, starts both stacks on free ports, queries nginx over HTTPS (isolation headers,
|
||||
|
||||
Reference in New Issue
Block a user