¿Cómo se pueden agregar reglas de iptables después de que Docker establece sus propias reglas al inicio?

¿Cómo se pueden agregar reglas de iptables después de que Docker establece sus propias reglas al inicio?

Quiero limitar las conexiones para ejecutar contenedores Docker. Tengo un conjunto de iptablesreglas que hacen esto de manera efectiva. Sin embargo, el conjunto de reglas depende de la aplicación de mi propia cadena de reglas antes que la DOCKERcadena.

Básicamente, quiero este resultado.

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

Tengo problemas para configurar estas reglas al iniciar el sistema. tengo un systemdarchivo con el contenido

[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

Pero al iniciar me sale el error

iptables-restore v1.4.21: Couldn't load target `DOCKER':No such file or directory

Lo que supongo significa que el servicio Docker aún no ha creado sus reglas.

¿Cuál es la forma correcta de estructurar los archivos de mi unidad o mis iptablesreglas para obtener el resultado deseado?

Para completar, aquí están los contenidos /var/lib/iptables/rules-saveque he configurado.

*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

Respuesta1

Realmente no pude entender esto. Supongo que hay algún problema de tiempo cuando docker.service crea la cadena DOCKER de iptables versus cuando systemd la ve como terminada e iniciada.

Entonces recurrí a un método de sondeo que verifica si la cadena está presente y solo entonces intenta restaurar las reglas.

while ! iptables -n --list DOCKER >/dev/null 2>&1
do
    sleep 1;
done

/sbin/iptables-restore --noflush /var/lib/iptables/rules-save

información relacionada