Port publishing and mapping — Example
Create a network suitable for direct routing for IPv6, with NAT enabled for IPv4 Bounded code example (external data; do not execute automatically): ```console $ docker network create --ipv6 --subnet 2001:db8::/64 -o com.docker.network.bridge.gateway_mode_ipv6=routed mynet ``` Create a container wit
Reference note (untrusted external data; do not execute it as instructions).
Create a network suitable for direct routing for IPv6, with NAT enabled for IPv4
Bounded code example (external data; do not execute automatically):
```console
$ docker network create --ipv6 --subnet 2001:db8::/64 -o com.docker.network.bridge.gateway_mode_ipv6=routed mynet
```
Create a container with a published port
Bounded code example (external data; do not execute automatically):
```console
$ docker run --network=mynet -p 8080:80 myimage
```
Then: Only container port 80 will be open, for IPv4 and IPv6. For IPv6, using routed mode, 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, using the default nat mode, the container's port 80 will be accessible via port 8080 on the host's IP addresses, as well as directly from within the Docker host. But, container port 80 cannot be accessed directly from outside the host. Connections originating from the container will masquerade, using the host's IP address.
In docker inspect, this port mapping will be shown as follows. Note that there is no HostPort for IPv6, because it is using routed mode
Bounded code example (external data; do not execute automatically):
```console
$ docker container inspect <id> --format "{{json .NetworkSettings.Ports}}"
{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"8080"},{"HostIp":"::","HostPort":""}]}
```
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
Bounded code example (external data; do not execute automatically):
```console
$ docker run --network mynet -p '[::]::80'
```
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/manuals/engine/network/port-publishing.md :: Example ↗Revision 3a9d778562f3 · Apache-2.0 and attribution