|
| 1 | +--- |
| 2 | +title: Docker and iptables |
| 3 | +description: The basics of how Docker works with iptables |
| 4 | +keywords: network, iptables |
| 5 | +--- |
| 6 | + |
| 7 | +On Linux, Docker manipulates `iptables` rules to provide network isolation. |
| 8 | +This is an implementation detail, and you should not modify the rules Docker |
| 9 | +inserts into your `iptables` policies. |
| 10 | + |
| 11 | +## Add iptables policies before Docker's rules |
| 12 | + |
| 13 | +All of Docker's `iptables` rules are added to the `DOCKER` table. Do not |
| 14 | +manipulate this table manually. If you need to add rules which load before |
| 15 | +Docker's rules, add them to the `DOCKER-USER` table. These rules are loaded |
| 16 | +before any rules Docker creates automatically. |
| 17 | + |
| 18 | +### Restrict connections to the Docker daemon |
| 19 | + |
| 20 | +By default, all external source IPs are allowed to connect to the Docker daemon. |
| 21 | +To allow only a specific IP or network to access the containers, insert a |
| 22 | +negated rule at the top of the DOCKER filter chain. For example, the following |
| 23 | +rule restricts external access to all IP addresses except 192.168.1.1: |
| 24 | + |
| 25 | +```bash |
| 26 | +$ iptables -I DOCKER-USER -i ext_if ! -s 192.168.1.1 -j DROP |
| 27 | +``` |
| 28 | + |
| 29 | +You could instead allow connections from a source subnet. The following rule |
| 30 | +only allows access from the subnet 192.168.1.0/24: |
| 31 | + |
| 32 | +```bash |
| 33 | +$ iptables -I DOCKER-USER -i ext_if ! -s 192.168.1.0/24 -j DROP |
| 34 | +``` |
| 35 | + |
| 36 | +Finally, you can specify a range of IP addresses to accept using `--src-range` |
| 37 | +(Remember to also add `-m iprange` wen using `--src-range` or `--dst-range`): |
| 38 | + |
| 39 | +```bash |
| 40 | +$ iptables -I DOCKER-USER -m iprange -i ext_if ! --src-range 192.168.1.1-192.168.1.3 -j DROP |
| 41 | +``` |
| 42 | + |
| 43 | +You can combine `-s` or `--src-range` with `-d` or `--dst-range` to control both |
| 44 | +the source and destination. For instance, if the Docker daemon listens on both |
| 45 | +192.168.1.99 and 10.1.2.3, you can make rules specific to `10.1.2.3` and leave |
| 46 | +`192.168.1.99` open. |
| 47 | + |
| 48 | +`iptables` is complicated and more complicated rule are out of scope for this |
| 49 | +topic. See the [Netfilter.org HOWTO](https://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html) |
| 50 | +for a lot more information. |
| 51 | + |
| 52 | + |
| 53 | +## Prevent Docker from manipulating iptables |
| 54 | + |
| 55 | +To prevent Docker from manipulating the `iptables` policies at all, set the |
| 56 | +`iptables` key to `false` in `/etc/docker/daemon.json`. This is inappropriate |
| 57 | +for most users, because the `iptables` policies then need to be managed by hand. |
| 58 | + |
| 59 | +## Next steps |
| 60 | + |
| 61 | +- Read [Docker Reference Architecture: Designing Scalable, Portable Docker Container Networks](https://success.docker.com/Architecture/Docker_Reference_Architecture%3A_Designing_Scalable%2C_Portable_Docker_Container_Networks) |
0 commit comments