Docker 网络之 bridge 模式使用介绍。
介绍
Linux bridge 是具有交换机功能的二层设备,Linux 中可以创建多个 bridge,连接同一 bridge 的容器可以相互通信。
区别
自定义 bridge 和 docker 默认 bridge(docker0
) 的区别:
- 自定义 bridge 网络提供 DNS 功能,默认 bridge 不提供。即自定义网络可以直接使用容器名通信
- 运行中的容器可以随时离开、加入用户自定义 bridge 网络,默认 bridge 网络必须停止容器后重新创建才行
- 用户自定义bridge网络可以灵活创建、配置
命令
$ docker network --help
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
- docker network create –help
$ docker network create --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
--config-from string The network from which to copy the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a network segment
示例
$ docker network create br-net
4c7eccde2715537d3069e19765ba69351c96d08d65a57722b25b6a9e436a589b
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
4c7eccde2715 br-net bridge local
$ brctl show
bridge name bridge id STP enabled interfaces
br-4c7eccde2715 8000.02426bef0d73 no
$ docker network rm br-net
docker run -it -d --name n1 \
--network br-net \
--publish 8080:80 \
nginx:latest
docker network disconnect br-net n1
docker network connect br-net n1
- 配置 bridge 网络,需要在
daemon.json
配置