@@ -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 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:
79
+
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. |
54
88
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. |
89
+ More information can be found in the network driver specific pages, including
90
+ their configuration options and details about their functionality.
63
91
64
- For more information about the different drivers, see [ Network drivers
65
- overview ] ( ./drivers/_index.md ) .
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 host. 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,84 +131,20 @@ $ 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
- This creates a firewall rule in the host,
137
- mapping a container port to a port on the Docker host to the outside world.
138
- Here are some examples:
139
-
140
- | Flag value | Description |
141
- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
142
- | ` -p 8080:80 ` | Map port ` 8080 ` on the Docker host to TCP port ` 80 ` in the container. |
143
- | ` -p 192.168.1.100:8080:80 ` | Map port ` 8080 ` on the Docker host IP ` 192.168.1.100 ` to TCP port ` 80 ` in the container. |
144
- | ` -p 8080:80/udp ` | Map port ` 8080 ` on the Docker host to UDP port ` 80 ` in the container. |
145
- | ` -p 8080:80/tcp -p 8080:80/udp ` | Map TCP port ` 8080 ` on the Docker host to TCP port ` 80 ` in the container, and map UDP port ` 8080 ` on the Docker host to UDP port ` 80 ` in the container. |
146
-
147
- > [ !IMPORTANT]
148
- >
149
- > Publishing container ports is insecure by default. Meaning, when you publish
150
- > a container's ports it becomes available not only to the Docker host, but to
151
- > the outside world as well.
152
- >
153
- > If you include the localhost IP address (` 127.0.0.1 ` , or ` ::1 ` ) with the
154
- > publish flag, only the Docker host and its containers can access the
155
- > published container port.
156
- >
157
- > ``` console
158
- > $ docker run -p 127.0.0.1:8080:80 -p ' [::1]:8080:80' nginx
159
- > ` ` `
160
- >
161
- > > [! WARNING]
162
- > >
163
- > > In releases older than 28.0.0, hosts within the same L2 segment (for example,
164
- > > hosts connected to the same network switch) can reach ports published to localhost.
165
- > > For more information, see
166
- > > [moby/moby#45610](https://github.com/moby/moby/issues/45610)
167
-
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
- Ports on the host's IPv6 addresses will map to the container's IPv4 address
174
- if no host IP is given in a port mapping, the bridge network is IPv4-only,
175
- and `--userland-proxy=true` (default).
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.
176
144
177
145
For more information about port mapping, including how to disable it and use
178
146
direct routing to containers, see
179
- [packet filtering and firewalls ](./packet-filtering-firewalls .md).
147
+ [ port publishing ] ( ./port-publishing .md ) .
180
148
181
149
## IP address and hostname
182
150
@@ -237,7 +205,30 @@ containers. To pass additional hosts into a container, refer to [add entries to
237
205
container hosts file] ( /reference/cli/docker/container/run.md#add-host ) in the
238
206
` docker run ` reference documentation.
239
207
240
- ## Proxy server
208
+ ## Container networks
209
+
210
+ In addition to user-defined networks, you can attach a container to another
211
+ container's networking stack directly, using the `--network
212
+ container:<name|id>` flag format.
213
+
214
+ The following flags aren't supported for containers using the ` container: `
215
+ networking mode:
241
216
242
- If your container needs to use a proxy server, see
243
- [ Use a proxy server] ( /manuals/engine/daemon/proxy.md ) .
217
+ - ` --add-host `
218
+ - ` --hostname `
219
+ - ` --dns `
220
+ - ` --dns-search `
221
+ - ` --dns-option `
222
+ - ` --mac-address `
223
+ - ` --publish `
224
+ - ` --publish-all `
225
+ - ` --expose `
226
+
227
+ The following example runs a Redis container, with Redis binding to
228
+ 127.0.0.1, then running the ` redis-cli ` command and connecting to the Redis
229
+ server over 127.0.0.1.
230
+
231
+ ``` console
232
+ $ docker run -d --name redis redis --bind 127.0.0.1
233
+ $ docker run --rm -it --network container:redis redis redis-cli -h 127.0.0.1
234
+ ```
0 commit comments