Skip to content

Commit

Permalink
Bridge network gateway_mode_ipv[46]=<nat|routed>
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Murray <[email protected]>
  • Loading branch information
robmry committed Jun 12, 2024
1 parent 7eb32d4 commit 10f3ff5
Showing 1 changed file with 80 additions and 5 deletions.
85 changes: 80 additions & 5 deletions content/network/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ $ docker run --rm -it --network container:redis example/redis-cli -h 127.0.0.1
## Published ports

By default, when you create or run a container using `docker create` or `docker run`,
the container doesn't expose any of its ports to the outside world.
containers on bridge networks don't expose any ports to the outside world.
Use the `--publish` or `-p` flag to make a port available to services
outside of Docker.
outside the bridge network.
This creates a firewall rule in the host,
mapping a container port to a port on the Docker host to the outside world.
Here are some examples:
Expand All @@ -111,11 +111,12 @@ Here are some examples:
> a container's ports it becomes available not only to the Docker host, but to
> the outside world as well.
>
> If you include the localhost IP address (`127.0.0.1`) with the publish flag,
> only the Docker host can access the published container port.
> If you include the localhost IP address (`127.0.0.1`, or `::1`) with the
> publish flag, only the Docker host and its containers can access the
> published container port.
>
> ```console
> $ docker run -p 127.0.0.1:8080:80 nginx
> $ docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginx
> ```
>
> > **Warning**
Expand All @@ -132,6 +133,80 @@ it isn't necessary to publish the container's ports.
You can enable inter-container communication by connecting the containers to the
same network, usually a [bridge network](./drivers/bridge.md).
On an IPv6-enabled Docker host, if no host IP is given in a port mapping

Check warning on line 136 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.SentenceLength] Write short, concise sentences. (<=40 words) Raw Output: {"message": "[Docker.SentenceLength] Write short, concise sentences. (\u003c=40 words)", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 136, "column": 1}}}, "severity": "WARNING"}
and the bridge network is IPv4-only, ports on the Docker host's IPv6
addresses will be mapped to the container's IPv4 address - unless docker-proxy

Check failure on line 138 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Docker' instead of 'docker'. Raw Output: {"message": "[Vale.Terms] Use 'Docker' instead of 'docker'.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 138, "column": 67}}}, "severity": "ERROR"}
is disabled.
### Direct routing for bridge networks
By default, IPv4 and IPv6 networks both use NAT and masquerading to map

Check warning on line 143 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 143, "column": 45}}}, "severity": "WARNING"}
ports between host IP addresses and the container. For example,
`docker run -p 8080:80 [...]` creates a mapping between port 8080 on any
address on the Docker host, and the container's port 80. Outgoing
connections from the container will masquerade, using the Docker host's
IP address.
However, particularly with IPv6, you may prefer to avoid using NAT. To

Check warning on line 150 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 150, "column": 64}}}, "severity": "WARNING"}
access containers on a bridge network from outside the Docker host, you
must set up routing to the bridge network via an address on the Docker
host. This can be achieved using static routes, BGP, or any other means

Check warning on line 153 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'BGP' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'BGP' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 153, "column": 49}}}, "severity": "WARNING"}
appropriate for your network.
To avoid NAT and masquerading, one option is to disable `ip6tables`

Check warning on line 156 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 156, "column": 10}}}, "severity": "WARNING"}
(or `iptables` for IPv4) in [daemon configuration](https://docs.docker.com/reference/cli/dockerd/).
All ports of all containers will then be accessible from the network,
and none will be mapped from Docker host IP addresses.
Alternatively, the bridge network driver has options
`com.docker.network.bridge.gateway_mode_ipv6=<nat|routed>` and
`com.docker.network.bridge.gateway_mode_ipv4=<nat|routed>`. The default
is `nat`, described above; NAT and masquerading rules are set up for

Check warning on line 164 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.RecommendedWords] Consider using 'previous' instead of 'above' Raw Output: {"message": "[Docker.RecommendedWords] Consider using 'previous' instead of 'above'", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 164, "column": 21}}}, "severity": "INFO"}

Check warning on line 164 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 164, "column": 28}}}, "severity": "WARNING"}
each mapped container port. With mode `routed`, no NAT or masquerading

Check warning on line 165 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 165, "column": 52}}}, "severity": "WARNING"}
rules are set up, and only mapped container ports will be directly
accessible.
In `routed` mode, a host port in a `-p` or `--publish` port mapping is
not used, and the host address is only used to decide whether to apply
the mapping to IPv4 or IPv6. So, when mapping only applies to `routed`
mode, because an IPv4 or IPv6 host address is included, only addresses
`0.0.0.0` or `::1` are allowed, and a host port must not be given.
Mapped container ports, in `nat` or `routed` mode, are accessible from
any remote address, if routing it set up in the network, unless the
Docker host's firewall has additional restrictions.
#### Example
Create a network suitable for direct routing for IPv6, with NAT enabled

Check warning on line 181 in content/network/_index.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'NAT' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'NAT' has no definition.", "location": {"path": "content/network/_index.md", "range": {"start": {"line": 181, "column": 61}}}, "severity": "WARNING"}
for IPv4:
```console
$ docker network create --ipv6 --subnet 2001:db8::/64 -o com.docker.network.bridge.gateway_mode_ipv6=routed mynet
```
Create a container with a port mapping:
```console
$ docker run --network=mynet -p 8080:80 myimage
```

Then:
- Only container port 80 will be open, for IPv4 and IPv6. It is accessible
from anywhere, if there is routing to the container's address, and access
is not blocked by the host's firewall.
- For IPv6, port 80 will be open on the container's IP address. Port 8080
will not be opened on the host's IP addresses, and outgoing packets will
use the container's IP address.
- For IPv4, the container's port 80 will also be accessible via port 8080 on
the host's IP addresses. Connections originating from the container will
masquerade, using the host's IP address.

Alternatively, to make the mapping IPv6-only, disabling IPv4 access to the
container's port 80, use the unspecified IPv6 address `[::]` and do not
include a host port number:
```console
$ docker run --network mynet -p '[::]::80'
```

## IP address and hostname

By default, the container gets an IP address for every Docker network it attaches to.
Expand Down

0 comments on commit 10f3ff5

Please sign in to comment.