Skip to content

Commit 22e4d2b

Browse files
committed
docs: simplify container networking intro
Create an easier to digest introduction to container networking, move the bulk of information to the networking overview page. Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
1 parent bcbe10a commit 22e4d2b

File tree

1 file changed

+21
-184
lines changed

1 file changed

+21
-184
lines changed

docs/reference/run.md

Lines changed: 21 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -161,196 +161,33 @@ $ docker ps -q --filter ancestor=nginx:alpine
161161
For more information about using filters, see
162162
[Filtering](https://docs.docker.com/config/filter/).
163163

164-
## Network settings
165-
166-
--dns=[] : Set custom dns servers for the container
167-
--network="bridge" : Connect a container to a network
168-
'bridge': create a network stack on the default Docker bridge
169-
'none': no networking
170-
'container:<name|id>': reuse another container's network stack
171-
'host': use the Docker host network stack
172-
'<network-name>|<network-id>': connect to a user-defined network
173-
--network-alias=[] : Add network-scoped alias for the container
174-
--add-host="" : Add a line to /etc/hosts (host:IP)
175-
--mac-address="" : Sets the container's Ethernet device's MAC address
176-
--ip="" : Sets the container's Ethernet device's IPv4 address
177-
--ip6="" : Sets the container's Ethernet device's IPv6 address
178-
--link-local-ip=[] : Sets one or more container's Ethernet device's link local IPv4/IPv6 addresses
179-
180-
By default, all containers have networking enabled and they can make any
181-
outgoing connections. The operator can completely disable networking
182-
with `docker run --network none` which disables all incoming and outgoing
183-
networking. In cases like this, you would perform I/O through files or
184-
`STDIN` and `STDOUT` only.
185-
186-
Publishing ports and linking to other containers only works with the default (bridge). The linking feature is a legacy feature. You should always prefer using Docker network drivers over linking.
187-
188-
Your container will use the same DNS servers as the host by default, but
189-
you can override this with `--dns`.
190-
191-
By default, the MAC address is generated using the IP address allocated to the
192-
container. You can set the container's MAC address explicitly by providing a
193-
MAC address via the `--mac-address` parameter (format:`12:34:56:78:9a:bc`).Be
194-
aware that Docker does not check if manually specified MAC addresses are unique.
195-
196-
Supported networks :
164+
## Container networking
197165

198-
<table>
199-
<thead>
200-
<tr>
201-
<th class="no-wrap">Network</th>
202-
<th>Description</th>
203-
</tr>
204-
</thead>
205-
<tbody>
206-
<tr>
207-
<td class="no-wrap"><strong>none</strong></td>
208-
<td>
209-
No networking in the container.
210-
</td>
211-
</tr>
212-
<tr>
213-
<td class="no-wrap"><strong>bridge</strong> (default)</td>
214-
<td>
215-
Connect the container to the bridge via veth interfaces.
216-
</td>
217-
</tr>
218-
<tr>
219-
<td class="no-wrap"><strong>host</strong></td>
220-
<td>
221-
Use the host's network stack inside the container.
222-
</td>
223-
</tr>
224-
<tr>
225-
<td class="no-wrap"><strong>container</strong>:&lt;name|id&gt;</td>
226-
<td>
227-
Use the network stack of another container, specified via
228-
its <i>name</i> or <i>id</i>.
229-
</td>
230-
</tr>
231-
<tr>
232-
<td class="no-wrap"><strong>NETWORK</strong></td>
233-
<td>
234-
Connects the container to a user created network (using <code>docker network create</code> command)
235-
</td>
236-
</tr>
237-
</tbody>
238-
</table>
239-
240-
#### Network: none
241-
242-
With the network is `none` a container will not have
243-
access to any external routes. The container will still have a
244-
`loopback` interface enabled in the container but it does not have any
245-
routes to external traffic.
246-
247-
#### Network: bridge
248-
249-
With the network set to `bridge` a container will use docker's
250-
default networking setup. A bridge is setup on the host, commonly named
251-
`docker0`, and a pair of `veth` interfaces will be created for the
252-
container. One side of the `veth` pair will remain on the host attached
253-
to the bridge while the other side of the pair will be placed inside the
254-
container's namespaces in addition to the `loopback` interface. An IP
255-
address will be allocated for containers on the bridge's network and
256-
traffic will be routed though this bridge to the container.
257-
258-
Containers can communicate via their IP addresses by default. To communicate by
259-
name, they must be linked.
260-
261-
#### Network: host
262-
263-
With the network set to `host` a container will share the host's
264-
network stack and all interfaces from the host will be available to the
265-
container. The container's hostname will match the hostname on the host
266-
system. Note that `--mac-address` is invalid in `host` netmode. Even in `host`
267-
network mode a container has its own UTS namespace by default. As such
268-
`--hostname` and `--domainname` are allowed in `host` network mode and will
269-
only change the hostname and domain name inside the container.
270-
Similar to `--hostname`, the `--add-host`, `--dns`, `--dns-search`, and
271-
`--dns-option` options can be used in `host` network mode. These options update
272-
`/etc/hosts` or `/etc/resolv.conf` inside the container. No change are made to
273-
`/etc/hosts` and `/etc/resolv.conf` on the host.
274-
275-
Compared to the default `bridge` mode, the `host` mode gives *significantly*
276-
better networking performance since it uses the host's native networking stack
277-
whereas the bridge has to go through one level of virtualization through the
278-
docker daemon. It is recommended to run containers in this mode when their
279-
networking performance is critical, for example, a production Load Balancer
280-
or a High Performance Web Server.
281-
282-
> **Note**
283-
>
284-
> `--network="host"` gives the container full access to local system services
285-
> such as D-bus and is therefore considered insecure.
166+
Containers have networking enabled by default, and they can make outgoing
167+
connections. If you're running multiple containers that need to communicate
168+
with each other, you can create a custom network and attach the containers to
169+
the network.
286170

287-
#### Network: container
288-
289-
With the network set to `container` a container will share the
290-
network stack of another container. The other container's name must be
291-
provided in the format of `--network container:<name|id>`. Note that `--add-host`
292-
`--hostname` `--dns` `--dns-search` `--dns-option` and `--mac-address` are
293-
invalid in `container` netmode, and `--publish` `--publish-all` `--expose` are
294-
also invalid in `container` netmode.
295-
296-
Example running a Redis container with Redis binding to `localhost` then
297-
running the `redis-cli` command and connecting to the Redis server over the
298-
`localhost` interface.
171+
When multiple containers are attached to the same custom network, they can
172+
communicate with each other using the container names as a DNS hostname:
299173

300174
```console
301-
$ docker run -d --name redis example/redis --bind 127.0.0.1
302-
$ # use the redis container's network stack to access localhost
303-
$ docker run --rm -it --network container:redis example/redis-cli -h 127.0.0.1
304-
```
305-
306-
#### User-defined network
307-
308-
You can create a network using a Docker network driver or an external network
309-
driver plugin. You can connect multiple containers to the same network. Once
310-
connected to a user-defined network, the containers can communicate easily using
311-
only another container's IP address or name.
312-
313-
For `overlay` networks or custom plugins that support multi-host connectivity,
314-
containers connected to the same multi-host network but launched from different
315-
Engines can also communicate in this way.
316-
317-
The following example creates a network using the built-in `bridge` network
318-
driver and running a container in the created network
319-
320-
```console
321-
$ docker network create -d bridge my-net
322-
$ docker run --network=my-net -itd --name=container3 busybox
323-
```
324-
325-
### Managing /etc/hosts
326-
327-
Your container will have lines in `/etc/hosts` which define the hostname of the
328-
container itself as well as `localhost` and a few other common things. The
329-
`--add-host` flag can be used to add additional lines to `/etc/hosts`.
330-
331-
```console
332-
$ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts
333-
334-
172.17.0.22 09d03f76bf2c
335-
fe00::0 ip6-localnet
336-
ff00::0 ip6-mcastprefix
337-
ff02::1 ip6-allnodes
338-
ff02::2 ip6-allrouters
339-
127.0.0.1 localhost
340-
::1 localhost ip6-localhost ip6-loopback
341-
86.75.30.9 db-static
175+
$ docker network create my-net
176+
$ docker run -d --name database --network my-net redis
177+
$ docker run --rm -it --network my-net busybox
178+
/ # ping database
179+
PING database (172.18.0.2): 56 data bytes
180+
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.326 ms
181+
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.257 ms
182+
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.281 ms
183+
^C
184+
--- database ping statistics ---
185+
3 packets transmitted, 3 packets received, 0% packet loss
186+
round-trip min/avg/max = 0.257/0.288/0.326 ms
342187
```
343188

344-
If a container is connected to the default bridge network and `linked`
345-
with other containers, then the container's `/etc/hosts` file is updated
346-
with the linked container's name.
347-
348-
> **Note**
349-
>
350-
> Since Docker may live update the container’s `/etc/hosts` file, there
351-
> may be situations when processes inside the container can end up reading an
352-
> empty or incomplete `/etc/hosts` file. In most cases, retrying the read again
353-
> should fix the problem.
189+
For more information about container networking, see [Networking
190+
overview](https://docs.docker.com/network/)
354191

355192
## Restart policies (--restart)
356193

0 commit comments

Comments
 (0)