
実行中の Docker コンテナへの接続を制限したいと考えています。これを効果的に行う一連のiptables
ルールがあります。ただし、ルール セットは、チェーンの前に独自のルール チェーンを適用することに依存していますDOCKER
。
基本的に、私はこの結果を望んでいます
Chain FORWARD (policy DROP)
target prot opt source destination
PRE_DOCKER all -- 0.0.0.0/0 0.0.0.0/0 /* Insert before Docker's filtering to apply our own */
DOCKER all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain PRE_DOCKER (policy DROP)
target prot opt source destination
//My own rules go here targeting the DOCKER chain
システム起動時にこれらのルールを設定するのに問題があります。systemd
内容が書かれたファイルがあります
[Unit]
Description=Restore iptables firewall rules
Before=iptables-store.service
Requires=docker.service
After=docker.service
Conflicts=shutdown.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore --noflush /var/lib/iptables/rules-save
[Install]
WantedBy=basic.target
しかし起動時にエラーが発生します
iptables-restore v1.4.21: Couldn't load target `DOCKER':No such file or directory
これは、Docker サービスがまだルールを作成していないことを意味していると思います。
iptables
目的の出力が得られるようにユニット ファイルまたはルールを構成する正しい方法は何ですか。
完全を期すために、/var/lib/iptables/rules-save
私が設定した内容をここに示します。
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:PRE_DOCKER - [0:0]
-I FORWARD -o docker0 -j PRE_DOCKER -m comment --comment "Insert before Docker's filtering to apply our own"
-A PRE_DOCKER ! -i eth0 -o docker0 -j DOCKER -m comment --comment "Anything coming from something other than the public interface send to DOCKER chain"
-A PRE_DOCKER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow connections from established connections"
-A PRE_DOCKER -j DROP -m comment --comment "Drop anything else"
-A INPUT ! -i eth0 -j ACCEPT -m comment --comment "Accept anything coming from something other than the public interface"
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow connections from established connections"
COMMIT
答え1
これを本当に理解できませんでした。docker.service が iptables DOCKER チェーンを作成するタイミングと、systemd がそれを完了して開始したと見なすタイミングとの間に、何らかのタイミングの問題があるのではないかと思います。
そこで私は、チェーンが存在するかどうかを確認し、存在する場合にのみルールを復元しようとするポーリング方式に頼りました。
while ! iptables -n --list DOCKER >/dev/null 2>&1
do
sleep 1;
done
/sbin/iptables-restore --noflush /var/lib/iptables/rules-save