Skip to content

Commit 8076104

Browse files
Misty Stanley-JonesMisty Stanley-Jones
authored andcommitted
Add more details about Docker and iptables
1 parent a8e84a8 commit 8076104

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

_data/toc.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ guides:
207207
path: /get-started/part5/
208208
- title: "Part 6: Deploy your app"
209209
path: /get-started/part6/
210-
- sectiontitle: Learn by example
211-
section:
212-
- path: /engine/tutorials/networkingcontainers/
213-
title: Network containers
214210
- path: /engine/docker-overview/
215211
title: Docker overview
216212
- sectiontitle: Develop with Docker

network/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ This topic does **not** go into OS-specific details about how Docker networks
2929
work, so you will not find information about how Docker manipulates `iptables`
3030
rules on Linux or how it manipulates routing rules on Windows servers, and you
3131
will not find detailed information about how Docker forms and encapsulates
32-
packets or handles encryption. See
32+
packets or handles encryption. See [Docker and iptables](/network/iptables.md)
33+
and
3334
[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)
3435
for a much greater depth of technical detail.
3536

network/iptables.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)