**Docker Magic Firewall (magicfw)** is an application designed to provide a dynamic and automated approach to managing Docker containers' networking firewall rules. By utilizing container labels, it allows fine-grained control over Docker container connectivity, such as enabling or restricting communication between containers on different Docker networks, external traffic access, and published ports. The service operates in the background, listens for Docker events, and updates `iptables`/`ip6tables` rules accordingly, providing seamless and secure container networking.
The main idea is to allow Docker to run without NAT by making each container accessible with its own IP address, while still maintaining security by dynamically configuring iptables rules on the host.
Features include:
- Can disable Docker's source NAT
- Can allow some containers to connect to containers in different Docker networks (very handy for applications like a reverse proxy)
- Dynamic management of firewall rules based on container and network's lifecycle
- Container-level settings via container labels
- IPv4 and IPv6 support
- Automatic subnet detection based on Docker's `daemon.json`
### Container Labels
You can define rules per container using specific Docker labels:
| `magicfw.firewall.allow_icc` | Enables communication between the container and other containers on different Docker networks (not normally possible in Docker environments). | `false` |
| `magicfw.firewall.allow_external` | Enables access to external networks (e.g., the internet) for the container. | `false` |
### Behavior and Functionalities
- **Allow ICC (Inter-Container Communication):** When `magicfw.firewall.allow_icc` is `true`, the container can communicate with other containers across **different Docker networks** (useful for applications like reverse proxies such as Traefik). If `false`, the container is isolated from other Docker networks (default Docker behavior).
- **External Traffic:** When `magicfw.firewall.allow_external` is `true`, the container's network rules allow communication with external IPs.
- **Automatic Rule Cleanup:** When a container is restarted, stopped, or removed, the corresponding firewall rules are automatically cleaned.
- **Support for Published Ports:** Rules are auto-generated for any published ports, restricting incoming traffic to only the ports explicitly exposed via Docker.
## Configuration
### Environment Variables
Below is a table of environment variables that can be customized within the systemd service: