惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
U
Unit 42
L
LangChain Blog
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
F
Fortinet All Blogs
小众软件
小众软件
I
InfoQ
P
Proofpoint News Feed
D
DataBreaches.Net
Martin Fowler
Martin Fowler
H
Help Net Security
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog

Leo's Field

Sending Emails, with netcat - Leo's Field Use ZED with msmtp on Debian - Leo's Field Have fun with ZFS: Maintenance and Error Recovery - Leo's Field Have fun with ZFS: Tuning - Leo's Field Have fun with ZFS: Setting up storage pool - Leo's Field 2021 Recap: Seek - Leo's Field Deploying this site on CloudFlare Pages - Leo's Field Redesigning this blog - Leo's Field Homelab Project: 6 months in - Leo's Field Fix incompatible bytes library for actix-web and tokio - Leo's Field A Breif Look at Linux's Audio System - Leo's Field Record to MiniDisc with correct Track Marker on Linux - Leo's Field Build Fcitx5 IM module for proprietary software with its Qt library - Leo's Field Way to AOSC OS Maintainer: Advanced Techniques - Leo's Field Way to AOSC OS Maintainer: Basics - Leo's Field Have fun with ZFS: Introduction - Leo's Field Fix clipboard permission on Android 10 - Leo's Field MDR-7506 Detachable Cable Mod - Leo's Field Update firmware of a Crucial SSD with systemd-boot - Leo's Field Install Arch Linux, using a Sony Walkman - Leo's Field Coreboot, me_cleaner, Tianocore, ThinkPad X220 - Leo's Field Dark mode! - Leo's Field Links - Leo's Field Some notes about the creation of this blog - Leo's Field Hello, world! - Leo's Field About - Leo's Field
Don't nuke my docker network, nftables.service! - Leo...
Leo Shen · 2025-03-03 · via Leo's Field

More Posts 暂无翻译版本

A tale of several confusing service interruptions

If you've ever used docker or other container software you might notice that they need to fiddle with the system firewall to wire things up correctly. And since you are on a server, you'd also want to have some kind of firewall to block unintended traffic. So you set both up, and life is good.

That is, until you need to reload your firewall. This is typically done via running systemctl restart nftables.service, but after that you'll find your docker network no longer working. What happened?

Well, if we dig into nftables.service, we will find this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[Unit]
Description=nftables
...ignored...

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/nft -f /etc/nftables.conf
ExecReload=/usr/sbin/nft -f /etc/nftables.conf
ExecStop=/usr/sbin/nft flush ruleset

[Install]
...not relavent...

And there's our problem! ExecStop= configures what to do when the service stops, and it's configured to nft flush ruleset, thus erasing all the rules (including our rules and rules injected by services like docker and systemd-nspawn) when we restart the service, and thus breaking docker and systemd-nspawn's network.

How do we solve this then? We just ask systemd to not run ExecStop! systemd has an override system, which allows us to modify the config of a service without modifying the original service file (this is bad because it will be overwritten by package updates). For our problem, we just write this:

1
2
3
# Put me in `/etc/systemd/system/nftables.service.d/override.conf`
[Service]
ExecStop=

This set ExecStop to nothing, overriding the ExecStop defined in the default service file.

Then, we can just flush the firewall table only in /etc/nftables.conf, so we won't touch docker's or systemd-nspawn's firewall rules when reloading our ruleset:

1
2
3
4
5
6
7
8
9
# Change this line
flush ruleset
# To
table inet firewall
flush table inet firewall

table inet firewall {
    # Proceed to define your firewall rules
}