0%

Docker-创建网络 create network

自建bridge网络

查看local的网络信息:
1
2
3
4
5
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
5133ec415c3c bridge bridge local
f359ca4e2d39 host host local
8d68673c045c none null local
创建网络

现在创建一个网络名为my_net且driver为bridge的网络:(默认创建的就是bridge)

1
2
3
4
5
6
7
8
9
10
[root@docker ~]# docker network create my_net
67e29f0e4a77c79144efc337a081a889188b5b8e289968f22be6e4ddd9b80610
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
5133ec415c3c bridge bridge local
f359ca4e2d39 host host local
67e29f0e4a77 my_net bridge local
8d68673c045c none null local
#创建network,指定网段
[root@docker ~]# docker network create --driver bridge --subnet=172.21.0.0/16 my-network01

利用–network启动容器提供服务:

1
2
3
4
[root@docker ~]# docker run -d --name=php --network my_net --network-alias php -v /www:/usr/local/nginx/html php
6b493cbe8207dee4cb4d5945cfce305dba96914083bd7f46841b0b42376bcb99
[root@docker ~]# docker run -d --name=nginx --network my_net --network-alias nginx -v /www:/usr/local/nginx/html -p 80:80 nginx
5ab220196b52bb768bef508433f0b920eecee70c3ee47880ebc5e2a74b5ee254

通过选项–network-alias将取名的my_net起了一个别名

1
2
3
4
[root@docker ~]# docker exec -it nginx ping php
PING php (172.18.0.2) 56(84) bytes of data.
64 bytes from php.my_net (172.18.0.2): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from php.my_net (172.18.0.2): icmp_seq=2 ttl=64 time=0.090 ms
创建指定ip的网络
1
[root@docker ~]# docker run -itd --name test01 --network my-network1 --ip 172.21.0.20 local/emqttd:v1
docker network命令详解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[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.

docker network command –help 查看具体命令的详细参数和用法

1
2
3
4
5
6
7
8
9
10
11
12
13
[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

创建网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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
segment

比如 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
macvlan与bridge模式区别

在实践开发中经常使用macvlan与bridge模式,他俩的区别如下:

macvlan模式可以将各个容器的ip地址设置为与宿主机同一个网段的地址,这样每个容器就是独立的ip,其他宿主机或其他宿主机里的容器都可以通过ip进行互访,但是宿主机不能同该宿主机里的容器进行访问。

bridge模式可以将各个容器的端口设为对立的,这样外部宿主机或者容器需要通过该容器所在ip+该容器的端口进行访问,并且本宿主机可以访问本宿主机中的容器。

总结:macvlan模式通过独立的ip进行区分访问,bridge模式通过独立的端口进行区分访问。