37

I am writing a dockerfile and I need IPtables to be installed in docker container. I need to add a rule to the IP table as I am trying to run on "host" network mode and it seems I need install IPtables for this purpose. when I try to include the rule as follows I get the following error.

iptables -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT


iptables v1.6.0: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.

Is it possible to run iptables with root privileges.

NirIzr
  • 3,131
  • 2
  • 30
  • 49
Tharanga
  • 2,007
  • 4
  • 32
  • 50

2 Answers2

69

--privileged flag is not required anymore. Starting with Docker 1.2 you can now run your image with parameters --cap-add=NET_ADMIN and --cap-add=NET_RAW which will allow internal iptables.

Dmitriusan
  • 11,525
  • 3
  • 38
  • 38
-1

Google to the rescue.

IPTables in docker

Docker runtime privilege

TLDR version:

 docker run --privileged
user2105103
  • 11,599
  • 2
  • 18
  • 11
  • 4
    While correct, IMHO feel like this is a Bad Idea for anything approaching a production environment (or development that is internet accessible) for the same reason why you don't run services as root. OP, please be very careful. There are better ways to skin this cat, IMHO. – Nick Burke Jan 17 '17 at 21:46
  • 2
    Agreed, but I just answered the question because I hate it when people cross examine -vs- answering the question. – user2105103 Jan 17 '17 at 21:47
  • 2
    Agree with that. I don't know the circumstances that OP is up to, but figured I'd toss the warning in just in case. – Nick Burke Jan 17 '17 at 22:09
  • I agree with both of you that it is a bad idea to run it with --privileged but this is a special circumstance where I need to debug an issue on a developer setup. Thanks @NickBurke I tried that and it works. But I forgot to mention that I was running everything(There are a few services involved which all of them are running as docker containers) on a docker-compose and that's where I hit the issue.I tried changing the user to root but did not help. There are a lot of repetitive tasks so I really don't want to start each and every service individually. – Tharanga Jan 18 '17 at 06:47