一、简介
Docker有以下网络类型:
bridge:多由于独立container之间的通信
host: 直接使用宿主机的网络,端口也使用宿主机的
overlay:当有多个docker主机时,跨主机的container通信
macvlan:每个container都有一个虚拟的MAC地址
none: 禁用网络
默认网络
Docker在默认情况下,分别会建立一个bridge、一个host和一个none的网络:
CNyteng:docker-volume yteng$ docker network ls
NETWORK ID NAME DRIVER SCOPE
557d70cd18ab bridge bridge local
27015fb1d01c host host local
d7bdd36df894 none null local
可以看到,driver类型为bridge的网络的名字也为bridge。在默认情况下,container都是使用的这个bridge的网络,此时container是可以访问外网和其他container的(需要通过IP地址)。
默认的名为bridge的网络是有很多限制的,为此,我们可以自行创建bridge类型的网络。默认的bridge网络与自建bridge网络有以下区别:
端口不会自行发布,必须使用-p参数才能为外界访问,而使用自建的bridge网络时,container的端口可直接被相同网络下的其他container访问。
container之间的如果需要通过名字访问,需要–link参数,而如果使用自建的bridge网络,container之间可以通过名字互访。
查看网络详情
docker network inspect bridge
自建bridge网络
当有多个独立的container之间需要彼此访问时,推荐使用自建bridge网络,而不是–link。
创建bridge网络
docker network create --driver bridge my-network
启动两个container,同时加入my-network:
docker run -dit --name alpine1 --network my-network alpine
docker run -dit --name alpine2 --network my-network alpine
container之间互访
CNyteng:~ yteng$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3ff8569849a alpine "/bin/sh" 48 seconds ago Up 48 seconds alpine2
50093c07f9d0 alpine "/bin/sh" 2 minutes ago Up 2 minutes alpine1
docker exec -it alpine1 sh
/ # cat /etc/hosts
172.18.0.2 50093c07f9d0 // 这是alpine1自己
/ # ping alpine2
PING alpine2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.172 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.154 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.154 ms
二、docker network命令详解
[root@]# docker network --help
Usage: docker network COMMAND
Manage networks
Options:
--help Print usage
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.
[root@]# docker network connect --help
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
Connect a container to a network
Options:
--alias stringSlice Add network-scoped alias for the container
--help Print usage
--ip string IP Address
--ip6 string IPv6 Address
--link list Add link to another container (default [])
--link-local-ip stringSlice Add a link-local address for the container
[root@izwz9fnxall7j07rfgkvwqz seafile]# 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[])
-d, --driver string Driver to manage the Network (default "bridge")
--gateway stringSlice IPv4 or IPv6 Gateway for the master subnet
--help Print usage
--internal Restrict external access to the network
--ip-range stringSlice 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 (default [])
-o, --opt map Set driver specific options (default map[])
--subnet stringSlice Subnet in CIDR format that represents a network segment
[root@izwz9fnxall7j07rfgkvwqz seafile]#
比如 docker network create lzwj 网络默认采用桥接网络
docker network ls
[root@seafile]# docker network ls
NETWORK ID NAME DRIVER SCOPE
62180a9e2a6f bridge bridge local
d80fef8de8ba host host local
6a5ad5bc599c lzwj bridge local
[root@seafile]#