NetBird with UFW-Docker
Use UFW-Docker to control public access to containers and NetBird policies to control access between peers. This lets you publish the NetBird services that need internet access while keeping sensitive services available only to selected peers.
This guide configures the public service allowances and adds a hook to the UFW-Docker integration so it recognizes NetBird-authorized traffic to private Docker services.
Before you start
This guide assumes:
- A Linux host using UFW and systemd.
- Docker bridge networking with the iptables-nft backend and a
DOCKER-USERchain. - NetBird installed on the host, using the
wt0interface.
The hook covers the IPv4 NetBird path. Recheck the mark and firewall backend before applying it elsewhere or after an upgrade.
How the firewall layers fit together
| Layer | What it controls |
|---|---|
| UFW host rules | Connections delivered to the host itself |
| UFW-Docker | Forwarded connections to Docker-published ports |
| NetBird policies | Which peers may reach a peer or resource |
Docker usually rewrites an incoming published-port connection to the container's address. That process, called destination NAT or DNAT, moves the packet through FORWARD rather than INPUT. A host rule such as ufw allow 5432/tcp therefore does not describe the whole Docker path.
UFW-Docker places its checks in DOCKER-USER, ahead of Docker's forwarding rules. Docker documents this ordering.
Run the host commands below as root. Keep an independent management connection or console available when changing the firewall. A tmux session preserves a shell, but it does not provide another network path if NetBird stops working.
Allow the public NetBird services
Container names vary with the installation method and Compose configuration. I customized this stack by prefixing every container name with netbird-, so the examples use names such as netbird-server, netbird-proxy, and netbird-traefik. Your installation may use different names.
Check the actual container and network names before copying the rules:
docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker network ls
Replace the example container names and netbird_netbird network with the names from your installation.
| Port | Service | Exposure |
|---|---|---|
| 80/tcp and 443/tcp | Traefik | Public dashboard, API, relay and HTTPS ingress |
| 3478/udp | NetBird server | Public STUN |
This setup assumes UFW is already enabled. Keep the host allowances used by your deployment:
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 3478/udp
If you use QUIC on UDP 443, retain that allowance too. A firewall rule alone does not create a listener or publish a Docker port. Host-input and Docker-forwarding rules cover different paths; inspect both, including IPv6, before removing an existing rule.
Install UFW-Docker
If UFW-Docker is not installed, follow the project's installation instructions to install the helper and its UFW integration.
During initial setup, install its systemd service and load the UFW rules:
ufw-docker install-service
ufw reload
Skip this setup if the helper and service are already installed. Adding the NetBird hook later does not require another global reload.
Allow the published containers
ufw-docker allow netbird-traefik 80/tcp netbird_netbird
ufw-docker allow netbird-traefik 443/tcp netbird_netbird
ufw-docker allow netbird-server 3478/udp netbird_netbird
Check the effective rules and container addresses:
ufw-docker status
iptables -S DOCKER-USER
iptables -t nat -S DOCKER
docker inspect --format '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{"\n"}}{{end}}' netbird-server
UFW-Docker's allowances contain container IP addresses. After a container is recreated, refresh its allowance with the same ufw-docker allow command and check that it matches Docker's current DNAT destination. A container name in a rule comment does not make the address update automatically.
Optional: NetBird Proxy direct transport
Skip this section if you do not use the proxy's fixed-port direct transport. If you enable it, allow the proxy's published UDP port:
ufw-docker allow netbird-proxy 51820/udp netbird_netbird
This example uses NB_PROXY_WG_PORT=51820. Publish the same UDP port in Docker and configure NB_PROXY_EXTERNAL_IP_MAP with the host's public address. Use the port configured for your installation if it differs. See NetBird's proxy P2P configuration for the complete setup.
Keep sensitive services private
Choose which peers may access each private service, then create a NetBird policy for that access. For example, to restrict PostgreSQL on TCP 5432:
- Put the permitted client peers in a source group.
- Put the peer hosting the container in a destination group.
- Create an allow policy between those groups for TCP 5432.
- Check other policies and routes for broader access to the same destination.
Use the protocol and port for your own service. PostgreSQL is only the example used in the commands below. The container must already have a Docker-published port reachable through the host's NetBird address; the firewall hook does not publish ports.
Keep private ports out of the public UFW-Docker allowances. For example, ufw-docker allow netbird-db 5432/tcp generates a from any allowance with this helper and can expose the published database port to the internet.
Pass NetBird-authorized traffic through UFW-Docker
NetBird marks authorized traffic arriving on wt0 for a local service before Docker translates the destination to the container. UFW-Docker also checks that forwarded traffic, so the mark-based hook lets it recognize NetBird's existing authorization.
Without that integration, a packet accepted by NetBird can still reach a UFW-Docker drop rule. A corresponding kernel log entry looks like this:
[UFW DOCKER BLOCK] IN=wt0 OUT=br-... SRC=100.64.0.10 DST=172.20.0.10 ... DPT=5432 ... MARK=0x1bd20
The addresses are examples. The incoming interface, destination port, and mark identify the path. This is a direct connection to the host's NetBird address; Docker DNAT puts it on the forwarding path without requiring the client to use an exit node.
Avoid a blanket ufw route allow in on wt0 exception when you need to preserve NetBird's service-level restrictions. It admits matching forwarded traffic without checking the authorization mark, including traffic permitted by broader routing rules. The hook below matches the mark for authorized local-service traffic instead.
Find NetBird's authorization mark
Identify the mark from the running firewall, then confirm its meaning in the corresponding NetBird source. This connects the value used by the hook to the policy rules that set and accept it.
Start with:
netbird version
iptables --version
nft list chain ip netbird netbird-mangle-prerouting
nft list chain ip netbird netbird-acl-input-rules
nft list chain ip netbird netbird-acl-forward-filter
On an nftables-backed installation, look for a rule like this for an allowed peer and service:
iifname "wt0" ip saddr 100.64.0.10 tcp dport 5432 fib daddr type local meta mark set 0x0001bd20
Read it as: when this peer sends TCP 5432 traffic through wt0 to a local address, set the packet mark before Docker changes the destination.
Check that the peer ACL also grants the expected source and port. In netbird-acl-input-rules, the corresponding rule should grant the same source and port:
ip saddr 100.64.0.10 tcp dport 5432 accept
A mark value by itself is not evidence of permission; check the policy rule and its action too.
The forward filter should also accept the mark before evaluating other forwarding rules:
meta mark 0x0001bd20 accept
iifname "wt0" jump netbird-rt-fwd
iifname "wt0" drop
The matching accept confirms what the mark is used for. The leading zeroes are only formatting: 0x0001bd20 and 0x1bd20 are the same number.
NetBird's client/net/fwmark.go names this value PreroutingFwmarkRedirected. Its default calculation is:
default base 0x1bd00 | redirected offset 0x20 = 0x1bd20
The queuePreroutingRule and addFwmarkToForward functions show where NetBird sets and accepts it for traffic redirected from the host to a container.
NB_FWMARK_BASE can change NetBird's mark range. A different release, backend, or configuration may also change the rules. Confirm the paired mark-and-accept behavior before copying the value below. If the ip netbird table is absent, identify the actual backend/table first.
This is a packet mark (meta mark, matched by iptables -m mark). It is different from a connection mark (ct mark) and from NetBird's masquerade marks. The hook reads it; it never assigns authorization marks itself.
Add the UFW-Docker integration hook
Add the NetBird exception through UFW's executable /etc/ufw/after.init hook, then connect it to ufw-docker.service with a systemd drop-in. UFW calls the hook during its lifecycle, and the drop-in calls it after UFW-Docker refreshes the forwarding rules.
First inspect the existing files. If after.init already contains custom logic, merge this change instead of replacing it. The installation below assumes the stock placeholder hook and no existing netbird.conf drop-in.
cat /etc/ufw/after.init
systemctl cat ufw-docker.service
ls -l /etc/systemd/system/ufw-docker.service.d/
The drop-in directory may not exist yet. Keep the following commands in the same root shell and record the backup directory they print:
backup_dir="/root/ufw-netbird-backup-$(date -u +%Y%m%dT%H%M%SZ)"
install -d -m 0700 "$backup_dir"
cp -a /etc/ufw/after.init "$backup_dir/after.init"
iptables -S DOCKER-USER > "$backup_dir/docker-user.rules"
printf 'Backup directory: %s\n' "$backup_dir"
Create the replacement hook in that protected directory:
cat > "$backup_dir/after.init.new" <<'EOF'
#!/bin/sh
#
# after.init: if executable, called by ufw-init. See 'man ufw-framework' for
# details. Note that output from these scripts is not seen via the
# the ufw command, but instead via ufw-init.
#
# Copyright 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -eu
NETBIRD_INTERFACE=wt0
# Recheck the mark against NetBird's loaded rules after upgrades.
NETBIRD_AUTHORIZED_LOCAL_SERVICE_MARK=0x1bd20
# UFW start/reload and the Docker lifecycle can invoke this concurrently.
exec 9>/run/lock/ufw-netbird-policy.lock
/usr/bin/flock -x 9
netbird_rule() {
/usr/sbin/iptables -w 5 "$@" \
-i "$NETBIRD_INTERFACE" \
-m mark --mark "$NETBIRD_AUTHORIZED_LOCAL_SERVICE_MARK/0xffffffff" \
-m conntrack --ctstate NEW,ESTABLISHED,RELATED \
-m comment --comment "NetBird-authorized local service" \
-j RETURN
}
case "${1:-}" in
start)
# Do not add firewall state when UFW is disabled.
if ! /usr/bin/grep -Eq '^[[:space:]]*ENABLED=yes[[:space:]]*$' /etc/ufw/ufw.conf; then
exit 0
fi
# The Docker service post-start hook retries if this chain is not ready.
if ! /usr/sbin/iptables -w 5 -n -L DOCKER-USER >/dev/null 2>&1; then
exit 0
fi
if ! netbird_rule -C DOCKER-USER >/dev/null 2>&1; then
netbird_rule -I DOCKER-USER 1
fi
;;
stop|flush-all)
# Remove only our exact rule, including any historical duplicates.
while netbird_rule -C DOCKER-USER >/dev/null 2>&1; do
netbird_rule -D DOCKER-USER
done
;;
status)
if netbird_rule -C DOCKER-USER >/dev/null 2>&1; then
echo "NetBird authorized-service forwarding: installed"
else
echo "NetBird authorized-service forwarding: absent"
fi
;;
*)
echo "Usage: $0 {start|stop|flush-all|status}" >&2
exit 2
;;
esac
EOF
sh -n "$backup_dir/after.init.new"
install -o root -g root -m 0750 "$backup_dir/after.init.new" /etc/ufw/after.init
The named constant makes the rule readable. Only traffic entering wt0 with NetBird's exact authorization mark and a valid tracked state matches it. RETURN skips the remaining UFW-Docker checks and resumes Docker's normal processing. Other public and peer traffic still follows the existing rules.
The hook checks before inserting, serializes concurrent calls, and removes only its exact rule on stop. If Docker's chain is not available during UFW startup, the service hook retries after Docker starts.
Add /etc/systemd/system/ufw-docker.service.d/netbird.conf:
install -d -m 0755 /etc/systemd/system/ufw-docker.service.d
cat > /etc/systemd/system/ufw-docker.service.d/netbird.conf <<'EOF'
[Service]
# Reapply after Docker starts and the existing ufw-docker reload finishes.
ExecStartPost=/etc/ufw/after.init start
EOF
systemctl daemon-reload
/etc/ufw/after.init start
/etc/ufw/after.init status
This adds an ExecStartPost; it preserves the service's existing ExecStart. Initial activation does not restart Docker, NetBird, or UFW.
Verify the allowed and blocked paths
Check the rule and its packet counter:
iptables -nvL DOCKER-USER --line-numbers
/etc/ufw/after.init start
iptables -S DOCKER-USER
There should be exactly one rule commented NetBird-authorized local service, before the UFW-Docker checks. Calling start again should not duplicate it. Its counter should increase when the approved peer connects.
Make fresh TCP connections from each relevant source. For example, with netcat available:
nc -vz -w 3 server.nb.example.com 5432
Use the public IP instead of the NetBird hostname for the external test. A successful connection checks TCP reachability. Test application authentication and permissions separately.
| Source and destination | Expected result |
|---|---|
| Peer allowed TCP 5432 → host NetBird address | Connects |
| Actual application client/container → host NetBird address | Connects |
| Peer without that grant → host NetBird address | Blocked |
| External client → host public address, TCP 5432 | Blocked |
Confirm all four results before relying on the configuration. Test from the actual application environment as well as the peer host, since containers can use different network paths.
Repeat these checks after a planned reboot, Docker restart, or UFW reload to verify that the hook is reapplied. Test IPv6 independently if your service or overlay uses it.
Troubleshooting and maintenance
Public services or SSO fail
Check the configured OIDC issuer, DNS, host listeners, and Docker forwarding rules together. Use the discovery URL configured for your identity provider, including any provider-specific path. Replace the example URL below:
OIDC_DISCOVERY_URL='https://idp.example.com/.well-known/openid-configuration'
curl --fail --show-error --silent --max-time 10 "$OIDC_DISCOVERY_URL"
docker logs --tail 100 netbird-proxy
docker logs --tail 100 netbird-traefik
Run discovery checks from the affected source where possible. An HTTP success from the host does not prove that a proxy container can reach the same endpoint. Host-input allowances can matter for host listeners and IPv6 paths, while Docker's IPv4 published ports normally use forwarding. Keep the established public rules and investigate the failing path before opening additional ports.
A Docker update or container recreation changes access
Use an independent management connection or console. Compare current container addresses and Docker DNAT targets with the UFW-Docker allowances, then refresh the affected public service with its ufw-docker allow command once it is running.
The installed UFW-Docker service refreshes allowances with Docker's service lifecycle. It is not a continuous watcher for every individual container recreation. The NetBird hook itself contains no container IP addresses.
After a NetBird update, repeat the mark inspection and all four access tests. Changing the base with NB_FWMARK_BASE also requires updating the hook's named mark value. This mark is an internal implementation detail, not a permanent public API.
Roll back the hook
Use the backup directory saved during installation. Stop the hook before restoring the original file so the live exception is removed:
/etc/ufw/after.init stop
cp -a "$backup_dir/after.init" /etc/ufw/after.init
rm /etc/systemd/system/ufw-docker.service.d/netbird.conf
systemctl daemon-reload
iptables -S DOCKER-USER
Remove only the drop-in created by this guide. Do not flush the firewall or restore the whole saved chain over unrelated changes. Verify that the tagged rule is absent and rerun fresh-connection tests. Removing the hook also removes the mark-based exception, so authorized NetBird connections to private containers may be blocked by UFW-Docker again.
References
- UFW-Docker project and installation
- UFW-Docker maintainer guidance for VPN/container access
- UFW customization hooks
- Docker firewall chains and DNAT
- NetBird ports and host firewalls
- NetBird's explanation of Docker peer-ACL marking
- NetBird mark definitions
- NetBird prerouting and forwarding implementation