Differences between revisions 6 and 10 (spanning 4 versions)
Revision 6 as of 2023-04-04 19:54:01
Size: 1382
Comment:
Revision 10 as of 2023-04-04 23:03:31
Size: 3023
Comment:
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
== Creating Networks == == Custom Networks ==
Line 25: Line 25:
Custom bridge networks can be created and destroyed like: Custom bridge networks can be created like:
Line 29: Line 29:
docker network rm my-net
Line 31: Line 30:

To enable IPv6 on a custom network, add the `--ipv6` option.



=== Attaching Containers ===
Line 40: Line 45:
A running container can be attached to a custom network like:

{{{
docker network connect my-net my-nginx
}}}
Line 43: Line 54:
$ docker run --interactive --tty --name my-busybox \ $ docker run --interactive --tty --name my-alpine \
Line 45: Line 56:
    busybox:latest
$ curl http://my-nginx
    alpine:latest
# apk add curl
[ ... ]
# curl http://my-nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
[ ... ]
Line 50: Line 68:

----



== Destroying Networks ==

Custom networks persist even when a container is stopped and destroyed.

To list all networks, try:

{{{
docker network ls
}}}

To delete a network, try:

{{{
docker network rm my-net
}}}

Note that a network cannot be destroyed if any containers are attached to it. They must be explicitly detached first.

{{{
docker network disconnect my-net my-nginx
}}}

----



== Docker Compose ==

By default, [[Docker/Compose|Docker Compose]] creates a bridge network for all services defined in a compose file.

Given this compose file:

{{{
services:
  web:
    image: nginx
    ports:
      - "8000:80"
  db:
    image: postgres
    ports:
      - "8001:5432"
}}}

 1. A bridge network named like `myapp-default` would be created
 2. A container named `web` would be attached to that network and would be reachable within the network at `http://web:80` or from the host at port 8000
 3. A container named `db` would be attached to that network and would be reachable within the network at `postgres://db:5432` or from the host at port 8001

Note that all ports are exposed within a bridge network.

This can create a need to review and manually delete networks on a recurring basis. See above for details.

----



== See also ==

[[Docker/Networking|Docker Networking]]

Docker Bridge Networks

Bridge networks are a type of networks used by dockerd(8).


Default Network

By default, containers attach to bridge networks. See here to change that.

If a bridge network is not specified, the default network is used. This network is special in that there is no name resolution; the only way to communicate between containers is their ephemeral IP addresses.

Furthermore, the default network's use of IPv6 mandatorily matches the dockerd(8) configuration. See here to enable it.


Custom Networks

Custom bridge networks can be created like:

docker network create my-net

To enable IPv6 on a custom network, add the --ipv6 option.

Attaching Containers

A container can be created on a custom network by specifying the --network option.

docker run --detach --name my-nginx \
  --network=my-net \
  nginx:latest

A running container can be attached to a custom network like:

docker network connect my-net my-nginx

Containers on a custom bridge network can communicate with each other by addressing the containers' names. For example, the above container would be accessible at http://my-nginx.

$ docker run --interactive --tty --name my-alpine \
    --network=my-net \
    alpine:latest
# apk add curl
[ ... ]
# curl http://my-nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
[ ... ]

Note that all ports are exposed.


Destroying Networks

Custom networks persist even when a container is stopped and destroyed.

To list all networks, try:

docker network ls

To delete a network, try:

docker network rm my-net

Note that a network cannot be destroyed if any containers are attached to it. They must be explicitly detached first.

docker network disconnect my-net my-nginx


Docker Compose

By default, Docker Compose creates a bridge network for all services defined in a compose file.

Given this compose file:

services:
  web:
    image: nginx
    ports:
      - "8000:80"
  db:
    image: postgres
    ports:
      - "8001:5432"
  1. A bridge network named like myapp-default would be created

  2. A container named web would be attached to that network and would be reachable within the network at http://web:80 or from the host at port 8000

  3. A container named db would be attached to that network and would be reachable within the network at postgres://db:5432 or from the host at port 8001

Note that all ports are exposed within a bridge network.

This can create a need to review and manually delete networks on a recurring basis. See above for details.


See also

Docker Networking


CategoryRicottone

Docker/BridgeNetworks (last edited 2023-04-04 23:03:31 by DominicRicottone)