Skip to content

Set up the box firewall

This page describes the developer’s Mac, where check slots and bin/agentsctl up instances start boxes as Docker containers in Colima. gomoe.dev runs computers on Fargate and keeps them apart with security groups, which How people and computers reach gomoe.dev covers. Nothing on this page applies to gomoe.dev.

A box is a container where an agent runs code, with a browser on the open web. The firewall lets a box reach the internet, the Mac’s machine ports and the VM’s DNS, and no other private address. Boxes on the same Docker bridge can still reach each other, since the bridge passes their traffic without the firewall’s rules. Set it up before a check or an instance starts a box. Without it, containers can reach the other services on the Mac, such as Electric and the stream server.

ops/box-firewall.sh holds the rules. It runs inside the Colima VM each time Colima starts.

Every container reaches the Mac only on the machine ports, 7700 and 7701. Port 7701 is where checks start their own API’s machine port. Boxes run on Docker’s default bridge, docker0, and get less. A box reaches no other address in these ranges, and nothing of the VM except DNS on port 53. The Mac’s own address in the VM, 192.168.5.2, is in 192.168.0.0/16, and its machine ports are let through before the ranges are dropped.

0.0.0.0/8 10.0.0.0/8 100.64.0.0/10
169.254.0.0/16 172.16.0.0/12 192.168.0.0/16
198.18.0.0/15 224.0.0.0/4 240.0.0.0/4

Answers to connections made from outside a box still pass, so published ports keep working. The Compose services run on their own network and keep what they reach now, except for the Mac.

  1. Apply it to the running VM. It prints nothing.

    Terminal window
    colima ssh -- sudo sh -s < ops/box-firewall.sh
  2. Make it last. Colima runs its provision steps at every start. Print the block that holds the script.

    Terminal window
    { printf 'provision:\n - mode: system\n script: |\n'; sed 's/^/ /' ops/box-firewall.sh; }
  3. Open ~/.colima/default/colima.yaml and keep a copy of it. In a new config the line reads provision: null, so replace that line with the printed block. If the file already has a provision: list, add the printed - mode: system item to the end of that list and keep the items it holds. If one of them is an older copy of the firewall script, replace that one item.

  4. Keep boxes apart from each other. The script does not do this, since Docker’s default bridge switches packets between two boxes without passing them to iptables. In the same file, set icc: false under the docker: key, keeping any other settings it holds.

    docker:
    icc: false

    After the restart below, Docker loads br_netfilter and drops traffic from one box to another on docker0 (-A DOCKER-FORWARD -i docker0 -o docker0 -j DROP). Containers on a network of their own, as Compose’s are, still reach each other by name, as docs/design/aws.md records.

  5. Restart Colima. This stops every container for a short time, so run it when no check or run is in progress.

    Terminal window
    colima restart

Running the script again is safe. It empties its own chains and refills them. For a moment during the refill, packets from boxes pass unfiltered.

  1. List the filter rules. Expect ACCEPT rules for ports 7700 and 7701 on 192.168.5.2, then a DROP for that address, then -A DOCKER-USER -i docker0 -j AGENTS-BOXES, in that order.

    Terminal window
    colima ssh -- sudo iptables -S DOCKER-USER
  2. List the input rules. Expect -A INPUT -i docker0 -j AGENTS-BOXES-IN.

    Terminal window
    colima ssh -- sudo iptables -S INPUT
  3. Probe from a container on the default bridge. The first three lines print dns ok, internet ok and machine port ok. The last line prints VM closed ok.

    Terminal window
    docker run --rm alpine:3.20 sh -c '
    nslookup example.com >/dev/null 2>&1 && echo "dns ok"
    nc -z -w 5 example.com 443 && echo "internet ok"
    nc -z -w 3 192.168.5.2 7700 && echo "machine port ok"
    nc -z -w 3 172.17.0.1 22 || echo "VM closed ok"
    '

To remove the box filtering without a restart, delete the two jumps. The rules that limit every container to the machine ports stay.

Terminal window
colima ssh -- sudo sh -c 'iptables -D DOCKER-USER -i docker0 -j AGENTS-BOXES; iptables -D INPUT -i docker0 -j AGENTS-BOXES-IN'