@@ -18,55 +18,85 @@ aliases:
18
18
---
19
19
20
20
Container networking refers to the ability for containers to connect to and
21
- communicate with each other, or to non-Docker workloads .
21
+ communicate with each other, and with non-Docker network services .
22
22
23
23
Containers have networking enabled by default, and they can make outgoing
24
24
connections. A container has no information about what kind of network it's
25
- attached to, or whether their peers are also Docker workloads or not . A
25
+ attached to, or whether its network peers are also Docker containers . A
26
26
container only sees a network interface with an IP address, a gateway, a
27
- routing table, DNS services, and other networking details. That is, unless the
28
- container uses the ` none ` network driver.
27
+ routing table, DNS services, and other networking details.
29
28
30
29
This page describes networking from the point of view of the container,
31
30
and the concepts around container networking.
32
- This page doesn't describe OS-specific details about how Docker networks work.
33
- For information about how Docker manipulates ` iptables ` rules on Linux,
34
- see [ Packet filtering and firewalls] ( packet-filtering-firewalls.md ) .
31
+
32
+ When Docker Engine on Linux starts for the first time, it has a single
33
+ built-in network called the "default bridge" network. When you run a
34
+ container with no ` --network ` option, it is connected to the default bridge.
35
+
36
+ Containers attached to the default bridge have access to network services
37
+ outside the Docker host. They use "masquerading" which means, if the
38
+ Docker host has Internet access, no additional configuration is needed
39
+ for the container to have Internet access.
40
+
41
+ For example, to run a container on the default bridge network, and have
42
+ it ping an Internet host:
43
+
44
+ ``` console
45
+ $ docker run --rm -ti busybox ping -c1 docker.com
46
+ PING docker.com (23.185.0.4): 56 data bytes
47
+ 64 bytes from 23.185.0.4: seq=0 ttl=62 time=6.564 ms
48
+
49
+ --- docker.com ping statistics ---
50
+ 1 packets transmitted, 1 packets received, 0% packet loss
51
+ round-trip min/avg/max = 6.564/6.564/6.564 ms
52
+ ```
35
53
36
54
## User-defined networks
37
55
38
- You can create custom, user-defined networks, and connect multiple containers
39
- to the same network. Once connected to a user-defined network, containers can
40
- communicate with each other using container IP addresses or container names.
56
+ With the default configuration, containers attached to the default
57
+ bridge network have unrestricted network access to each other using
58
+ container IP addresses. They cannot refer to each other by name.
59
+
60
+ It can be useful to separate groups of containers that should have full
61
+ access to each other, but restricted access to containers in other groups.
62
+
63
+ You can create custom, user-defined networks, and connect groups of containers
64
+ to the same network. Once connected to a user-defined network, containers
65
+ can communicate with each other using container IP addresses or container names.
41
66
42
67
The following example creates a network using the ` bridge ` network driver and
43
- running a container in the created network:
68
+ runs a container in that network:
44
69
45
70
``` console
46
71
$ docker network create -d bridge my-net
47
- $ docker run --network=my-net -itd --name=container3 busybox
72
+ $ docker run --network=my-net -it --name=container3 busybox
48
73
```
49
74
50
75
### Drivers
51
76
52
- The following network drivers are available by default, and provide core
53
- networking functionality :
77
+ Docker Engine has a number of network drivers, as well as the default "bridge".
78
+ On Linux, the following built-in network drivers are available :
54
79
55
- | Driver | Description |
56
- | :-------- | : ----------------------------------------------------------------------- |
57
- | ` bridge ` | The default network driver. |
58
- | ` host ` | Remove network isolation between the container and the Docker host. |
59
- | ` none ` | Completely isolate a container from the host and other containers. |
60
- | ` overlay ` | Overlay networks connect multiple Docker daemons together. |
61
- | ` ipvlan ` | IPvlan networks provide full control over both IPv4 and IPv6 addressing. |
62
- | ` macvlan ` | Assign a MAC address to a container. |
80
+ | Driver | Description |
81
+ | :--------------------------------| : -------------------------------------------------------------------- |
82
+ | [ bridge] ( ./drivers/bridge.md ) | The default network driver. |
83
+ | [ host] ( ./drivers/host.md ) | Remove network isolation between the container and the Docker host. |
84
+ | [ none] ( ./drivers/none.md ) | Completely isolate a container from the host and other containers. |
85
+ | [ overlay] ( ./drivers/overlay.md ) | Swarm Overlay networks connect multiple Docker daemons together. |
86
+ | [ ipvlan] ( ./drivers/ipvlan.md ) | Connect containers to external VLANs. |
87
+ | [ macvlan] ( ./drivers/macvlan.md ) | Containers appear as devices on the host's network. |
63
88
64
- For more information about the different drivers, see [ Network drivers
65
- overview] ( ./drivers/_index.md ) .
89
+ More information can be found in the network driver specific pages, including
90
+ their configuration options and details about their functionality.
91
+
92
+ Native Windows containers have a different set of drivers, see
93
+ [ Windows container network drivers] ( https://learn.microsoft.com/en-us/virtualization/windowscontainers/container-networking/network-drivers-topologies ) .
66
94
67
95
### Connecting to multiple networks
68
96
69
- A container can be connected to multiple networks.
97
+ Connecting a container to a network can be compared to connecting an ethernet
98
+ cable to a physical NIC. Just as a host can be connected to multiple ethernet
99
+ networks, a container can be connected to multiple Docker networks.
70
100
71
101
For example, a frontend container may be connected to a bridge network
72
102
with external access, and a
@@ -78,6 +108,8 @@ A container may also be connected to different types of network. For example,
78
108
an ` ipvlan ` network to provide internet access, and a ` bridge ` network for
79
109
access to local services.
80
110
111
+ Containers can also share networking stacks, see [ Container networks] ( #container-networks ) .
112
+
81
113
When sending packets, if the destination is an address in a directly connected
82
114
network, packets are sent to that network. Otherwise, packets are sent to
83
115
a default gateway for routing to their destination. In the example above,
@@ -99,40 +131,16 @@ $ docker run --network name=gwnet,gw-priority=1 --network anet1 --name myctr myi
99
131
$ docker network connect anet2 myctr
100
132
```
101
133
102
- ## Container networks
103
-
104
- In addition to user-defined networks, you can attach a container to another
105
- container's networking stack directly, using the `--network
106
- container:<name|id>` flag format.
107
-
108
- The following flags aren't supported for containers using the ` container: `
109
- networking mode:
110
-
111
- - ` --add-host `
112
- - ` --hostname `
113
- - ` --dns `
114
- - ` --dns-search `
115
- - ` --dns-option `
116
- - ` --mac-address `
117
- - ` --publish `
118
- - ` --publish-all `
119
- - ` --expose `
120
-
121
- The following example runs a Redis container, with Redis binding to
122
- ` localhost ` , then running the ` redis-cli ` command and connecting to the Redis
123
- server over the ` localhost ` interface.
124
-
125
- ``` console
126
- $ docker run -d --name redis example/redis --bind 127.0.0.1
127
- $ docker run --rm -it --network container:redis example/redis-cli -h 127.0.0.1
128
- ```
129
-
130
134
## Published ports
131
135
132
- By default, when you create or run a container using ` docker create ` or ` docker run ` ,
133
- containers on bridge networks don't expose any ports to the outside world.
134
- Use the ` --publish ` or ` -p ` flag to make a port available to services
135
- outside the bridge network.
136
+ When you create or run a container using ` docker create ` or ` docker run ` , all
137
+ ports of containers on bridge networks are accessible from the Docker host and
138
+ other containers connected to the same network. Ports are not accessible from
139
+ outside the host or, with the default configuration, from containers in other
140
+ networks.
141
+
142
+ Use the ` --publish ` or ` -p ` flag to make a port available outside the host,
143
+ and to containers in other bridge networks.
136
144
This creates a firewall rule in the host,
137
145
mapping a container port to a port on the Docker host to the outside world.
138
146
Here are some examples:
@@ -165,11 +173,6 @@ Here are some examples:
165
173
> > For more information, see
166
174
> > [moby/moby#45610](https://github.com/moby/moby/issues/45610)
167
175
168
- If you want to make a container accessible to other containers,
169
- it isn't necessary to publish the container's ports.
170
- You can enable inter-container communication by connecting the containers to the
171
- same network, usually a [bridge network](./drivers/bridge.md).
172
-
173
176
Ports on the host's IPv6 addresses will map to the container's IPv4 address
174
177
if no host IP is given in a port mapping, the bridge network is IPv4-only,
175
178
and `--userland-proxy=true` (default).
@@ -237,7 +240,30 @@ containers. To pass additional hosts into a container, refer to [add entries to
237
240
container hosts file] ( /reference/cli/docker/container/run.md#add-host ) in the
238
241
` docker run ` reference documentation.
239
242
240
- ## Proxy server
243
+ ## Container networks
241
244
242
- If your container needs to use a proxy server, see
243
- [ Use a proxy server] ( /manuals/engine/daemon/proxy.md ) .
245
+ In addition to user-defined networks, you can attach a container to another
246
+ container's networking stack directly, using the `--network
247
+ container:<name|id>` flag format.
248
+
249
+ The following flags aren't supported for containers using the ` container: `
250
+ networking mode:
251
+
252
+ - ` --add-host `
253
+ - ` --hostname `
254
+ - ` --dns `
255
+ - ` --dns-search `
256
+ - ` --dns-option `
257
+ - ` --mac-address `
258
+ - ` --publish `
259
+ - ` --publish-all `
260
+ - ` --expose `
261
+
262
+ The following example runs a Redis container, with Redis binding to
263
+ ` localhost ` , then running the ` redis-cli ` command and connecting to the Redis
264
+ server over the ` localhost ` interface.
265
+
266
+ ``` console
267
+ $ docker run -d --name redis example/redis --bind 127.0.0.1
268
+ $ docker run --rm -it --network container:redis example/redis-cli -h 127.0.0.1
269
+ ```
0 commit comments