打開2個會話,分別運行如下命令node
# docker run -it -P --name nginx2 nginx #-P 端口隨機映射nginx
再打開一個會話查看 運行中的容器docker
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e065a4861a7f nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:32769->80/tcp nginx2
101c87844e00 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:32768->80/tcp nginx網絡
能夠發現 使用了-P參數後 會從32768開始映射到容器的80端口,不會形成端口衝突,缺點是沒法知道每次映射的端口是多少tcp
# docker run -it -p 53:53/udp --name dhcp dhcp #能夠指定映射協議/udp
# docker run -it -p 192.168.56.11:80:80 --name ngxin3 nginx #假如宿主機有多個ip,能夠指定映射致哪一個ip 這個例子是將容器的80端口映射到本機的192.168.56.11的80端口上oop
容器之間的互聯性能
第一種方法 經過給容器命名別名的方式鏈接測試
打開第一個會話,運行如下
# docker run -it --name busybox1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
44: eth0@if45: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever3d
再打開第二個對話,運行如下
# docker run -it --link busybox1:busyboxbieming --name busybox2 busybox # --link起別名
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 busyboxbieming 6e6aca74d76c busybox1
172.17.0.3 35087ae7a3d4
/ # ping busyboxbieming
PING busyboxbieming (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.065 msrouter
查看hosts文件能夠發現多了一條別名的記錄,ping別名發現能夠解析到第一臺的ip,所以能夠經過這種別名的方式去鏈接
第二種方式
# docker network ls #查看網絡模式
NETWORK ID NAME DRIVER SCOPE
2e7f4890adbc bridge bridge local
cb1cfe757fe7 host host local
35bc4c0ccb9b none null local
host模式會使容器的網卡信息和宿主機保持一致
先打開一個會話,執行如下命令
#docker run -it --net=host nginx
再打開一個會話,繼續執行如下命令
#docker run -it --net=host nginx
此時會報錯,提示80端口被佔用
# docker run -it --net=host nginx
2020/02/21 16:34:54 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/02/21 16:34:54 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
再宿主機上執行netstat命令能夠發現 80端口已經被第一臺容器使用了
# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 959/master
tcp 0 0 192.168.56.11:2375 0.0.0.0:* LISTEN 878/dockerd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7306/nginx: master
這種模式的好處是,性能高,可是容易佔用本機端口,通常對網絡性能要求高時使用
下面建立一個屬於本身的網絡
# docker network create --driver bridge mydockernet
76d079e545f183fd799cebc203d4fb55f94110a0043da8377cbeb5009aa32af2
# docker network ls
NETWORK ID NAME DRIVER SCOPE
2e7f4890adbc bridge bridge local
cb1cfe757fe7 host host local
76d079e545f1 mydockernet bridge local
35bc4c0ccb9b none null local
使用剛剛建立的網絡來創建容器
# docker run -it --network=mydockernet busybox #--network指定使用的網絡
/ #
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
53: eth0@if54: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
能夠發現ip變成了172.18.x.0了
實際狀況中咱們要指定建立的網段,下面繼續建立一個指定的網段
#docker network create --driver bridge --subnet 172.22.16.0/24 --gateway 172.22.16.1 my_net2 #--subnet指定子網 --gateway指定網關2b3c58c61fb1bfa6f6959b7eb6b3b2d5c1c73f47b1f5663fcc601c31a93a1bdd
# docker run -it --rm --network=my_net2 --ip 172.22.16.88 busybox #能夠指定ip
/ #
/ #
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
5: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:16:10:58 brd ff:ff:ff:ff:ff:ff
inet 172.22.16.88/24 brd 172.22.16.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
退出以前建立的2臺,咱們再新建2臺容器,測試下互通
# docker run -it --name oldboy1 --rm --network=my_net2 --ip 172.22.16.88 busybox #新建第一臺
# docker run -it --name oldboy2 --rm --network=my_net2 --ip 172.22.16.99 busybox #新建第二臺
互ping發現能夠ping通,這是由於使用了docker自定義網絡中的dns,使得用戶名的已解析,這個解析僅對用戶自定義的網絡有效,默認的網絡是無效的
下面的這種默認方式建立的容器,經過容器名是ping不通的
# docker run -it --rm --name oldboy1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.065 ms
/ # ping oldboy2
ping: bad address 'oldboy2'
# docker run -it --rm --name oldboy2 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping oldboy1
ping: bad address 'oldboy1'
保留剛剛建立的oldboy1,刪除oldboy2,而後再新建一個oldboy2
這個oldboy2使用自定網絡
# docker run -it --name oldboy2 --rm --network=my_net2 --ip 172.22.16.99 busybox
此時oldboy2和oldboy1之間是經過容器名ping不通的
若是須要2臺能夠通,可使用下面的命令
# docker network connect my_net2 oldboy1 #把oldboy1容器加入my_net2網絡中
隨後便可互相經過容器名ping通
觀察oldboy1
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
17: eth1@if18: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:16:10:02 brd ff:ff:ff:ff:ff:ff
inet 172.22.16.2/24 brd 172.22.16.255 scope global eth1
valid_lft forever preferred_lft forever
發現多了一塊網卡
這樣就能夠解決不在同一網絡的容器互通的問題