CodexBloom - Programming Q&A Platform

advanced patterns with network namespaces in Ubuntu 20.04 when using Docker and iptables

👀 Views: 19 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-27
linux networking docker iptables bash

I'm working with a strange scenario while using network namespaces in Ubuntu 20.04 alongside Docker containers. I created a custom network namespace for testing purposes but noticed that iptables rules aren't being applied as expected. I have a Docker container running with the following command: ```bash docker run --rm -it --net=my_custom_net --name my_container alpine sh ``` After creating the custom network with: ```bash ip netns add my_custom_net ip link add veth0 type veth peer name veth1 ip link set veth0 netns my_custom_net ip netns exec my_custom_net ip link set veth0 up ip link set veth1 up ``` I set up a simple iptables rule to allow traffic only from a specific IP: ```bash iptables -A INPUT -s 192.168.1.100 -j ACCEPT iptables -A INPUT -j DROP ``` However, when I try to reach my container from another host, the connection is still being accepted from all IPs, and I'm not seeing the filtering effect. I've made sure to check the default policy and other rules that might affect the namespace, but the behavior remains unchanged. I've also tried flushing the iptables rules and reapplying them, but the scenario continues. Can someone guide to understand why the iptables rules aren't working within my custom namespace? Am I missing a step or misconfiguring something in the setup? This is for a application running on Ubuntu 22.04. Is this even possible?