|
| 1 | +--- |
| 2 | +title: Container networking |
| 3 | +description: How networking works from the container's point of view |
| 4 | +keywords: networking, container, standalone |
| 5 | +redirect_from: |
| 6 | +- /engine/userguide/networking/configure-dns/ |
| 7 | +- /engine/userguide/networking/default_network/binding/ |
| 8 | +--- |
| 9 | + |
| 10 | +The type of network a container uses, whether it is a [brudge](bridges.md), an |
| 11 | +[overlay](overlay.md), a [macvlan network](macvlan.md), or a custom network |
| 12 | +plugin, is transparent from within the container. From the container's point of |
| 13 | +view, it has a network interface with an IP address, a gateway, a routing table, |
| 14 | +DNS services, and other networking details (assuming the container is not using |
| 15 | +the `none` network driver). This topic is about networking concerns from the |
| 16 | +point of view of the container. |
| 17 | + |
| 18 | +## Published ports |
| 19 | + |
| 20 | +By default, when you create a container, it does not publish any of its ports |
| 21 | +to the outside world. To make a port available to services outside of Docker, or |
| 22 | +to Docker containers which are not connected to the container's network, use the |
| 23 | +`--publish` or `-p` flag. This creates a firewall rule which maps a container |
| 24 | +port to a port on the Docker host. Here are some examples. |
| 25 | + |
| 26 | +| Flag value | Description | |
| 27 | +|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------| |
| 28 | +| `-p 8080:80` | Map TCP port 80 in the container to port 8080 on the Docker host. | |
| 29 | +| `-p 8080:80/udp` | Map UDP port 80 in the container to port 8080 on the Docker host. | |
| 30 | +| `-p 8080:80/tcp -p 8080:80/udp` | Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host. | |
| 31 | + |
| 32 | +## IP address and hostname |
| 33 | + |
| 34 | +By default, the container is assigned an IP address for every Docker network it |
| 35 | +connects to. The IP address is assigned from the pool assigned to |
| 36 | +the network, so the Docker daemon effectively acts as a DHCP server for each |
| 37 | +container. Each network also has a default subnet mask and gateway. |
| 38 | + |
| 39 | +When the container starts, it can only be connected to a single network, using |
| 40 | +`--network`. However, you can connect a running container to multiple |
| 41 | +networks using `docker network connect`. When you start a container using the |
| 42 | +`--network` flag, you can specify the IP address assigned to the container on |
| 43 | +that network using the `--ip` or `--ip6` flags. |
| 44 | + |
| 45 | +When you connect an existing container to a different network using |
| 46 | +`docker network connect`, you can use the `--ip` or `--ip6` flags on that |
| 47 | +command to specify the container's IP address on the additional network. |
| 48 | + |
| 49 | +In the same way, a container's hostname defaults to be the container's name in |
| 50 | +Docker. You can override the hostname using `--hostname`. When connecting to an |
| 51 | +existing network using `docker network connect`, you can use the `--alias` |
| 52 | +flag to specify an additional network alias for the container on that network. |
| 53 | + |
| 54 | +## DNS services |
| 55 | + |
| 56 | +By default, a container inherits the DNS settings of the Docker daemon, |
| 57 | +including the `/etc/hosts` and `/etc/resolv.conf`.You can override these |
| 58 | +settings on a per-container basis. |
| 59 | + |
| 60 | +| Flag | Description | |
| 61 | +|----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 62 | +| `--dns` | The IP address of a DNS server. To specify multiple DNS servers, use multiple `--dns` flags. If the container cannot reach any of the IP addresses you specify, Google's public DNS server `8.8.8.8` is added, so that your container can resolve internet domains. | |
| 63 | +| `--dns-search` | A DNS search domain to search non-fully-qualified hostnames. To specify multiple DNS search prefixes, use multiple `--dns-search` flags. | |
| 64 | +| `--dns-opt` | A key-value pair representing a DNS option and its value. See your operating system's documentation for `resolv.conf` for valid options. | |
| 65 | +| `--hostname` | The hostname a container uses for itself. Defaults to the container's name if not specified. | |
0 commit comments