创建docker network
为了使用自定义网络,或者使一个容器有多个网络接口,可以使用docker network命令:
1
sudo docker network create -d bridge --subnet 10.10.0.0/24 net1
Copied!
然后在主机上会生成一个新的网络接口:
1
[email protected]:~$ sudo docker inspect net1
2
[
3
{
4
"Name": "net1",
5
"Id": "ba6e4cae74208858d2bfafbe52da00c8b8177b5948fb027fa990861f8060ae97",
6
"Created": "2020-04-24T18:18:14.621826632+08:00",
7
"Scope": "local",
8
"Driver": "bridge",
9
"EnableIPv6": false,
10
"IPAM": {
11
"Driver": "default",
12
"Options": {},
13
"Config": [
14
{
15
"Subnet": "10.10.0.0/24"
16
}
17
]
18
},
19
"Internal": false,
20
"Attachable": false,
21
"Ingress": false,
22
"ConfigFrom": {
23
"Network": ""
24
},
25
"ConfigOnly": false,
26
"Containers": {},
27
"Options": {},
28
"Labels": {}
29
}
30
]
31
[email protected]:~$ ifconfig
32
br-ba6e4cae7420: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
33
inet 10.10.0.1 netmask 255.255.255.0 broadcast 10.10.0.255
34
ether 02:42:ef:de:5d:93 txqueuelen 0 (Ethernet)
35
RX packets 0 bytes 0 (0.0 B)
36
RX errors 0 dropped 0 overruns 0 frame 0
37
TX packets 0 bytes 0 (0.0 B)
38
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Copied!
使用docker network ls查看:
1
[email protected]:~$ sudo docker network ls
2
NETWORK ID NAME DRIVER SCOPE
3
9045dd7942c2 bridge bridge local
4
e3630a6f8b64 host host local
5
ba6e4cae7420 net1 bridge local
Copied!

测试:把容器加入这个网络并增加一个域名解释

1
docker run --name testbox -it --net net1 --add-host "testbox.maxidea.com:10.10.0.2" busybox /bin/sh
2
3
/ # more /etc/hosts
4
127.0.0.1 localhost
5
::1 localhost ip6-localhost ip6-loopback
6
fe00::0 ip6-localnet
7
ff00::0 ip6-mcastprefix
8
ff02::1 ip6-allnodes
9
ff02::2 ip6-allrouters
10
10.10.0.2 testbox.maxidea.com
11
10.10.0.2 0c8fdfe89b4f
12
13
/ # ping testbox.maxidea.com
14
PING testbox.maxidea.com (10.10.0.2): 56 data bytes
15
64 bytes from 10.10.0.2: seq=0 ttl=64 time=0.061 ms
16
64 bytes from 10.10.0.2: seq=1 ttl=64 time=0.032 ms
17
64 bytes from 10.10.0.2: seq=2 ttl=64 time=0.030 ms
Copied!
Last modified 1yr ago