專一於大數據及容器雲核心技術解密,可提供全棧的大數據+雲原平生臺諮詢方案,請持續關注本套博客。若有任何學術交流,可隨時聯繫。更多內容請關注《數據雲技術社區》公衆號。 html
root@ubuntu:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
fe6e820d51e2 bridge bridge local
e91fa0de345b host host local
9fea16767e7a none null local
複製代碼
docker run -it --network=none busybox
複製代碼
docker run -it --network=host busybox
複製代碼
root@ubuntu:~# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242f47922c9 no
複製代碼
root@ubuntu:~# docker network create -d bridge my_bridge
423b660fbdbd5cfacc4cbf591bdf2bc977e7865261349efb6482dbdadxcda3
root@ubuntu:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
fe6e820d51e2 bridge bridge local
e91fa0de345b host host local
423b660fbdbd my_bridge bridge local
9fea16767e7a none null local
查看一下當前 host 的網絡結構變化,新增了一個網橋 br-423b660fbdbd
root@ubuntu:~# brctl show
bridge name bridge id STP enabled interfaces
br-423b660fbdbd 8000.0242cb3347fd no
docker0 8000.0242f47922c9 no vethcd8f150
複製代碼
root@ubuntu:~# docker network create -d bridge --subnet 192.168.31.0/24 --gateway 192.168.31.1 my_bridge2
43043f6bbc1a74106bef92e158daec3ea376748de2f8695541c8e93964303b5b
容器要使用新的網絡,須要在啓動時經過--network指定:
root@ubuntu:~# docker run -it --network=my_bridge2 --name busybox1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
42: eth0@if43: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:1f:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.2/24 scope global eth0
valid_lft forever preferred_lft forever
複製代碼
root@ubuntu:~# docker run -it --network=my_bridge2 --ip 192.168.31.25 --name busybox2 busybox
複製代碼
查看 httpd 容器的 ID。
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
488e6019f780 busybox "sh" About an hour ago Up 31 minutes busybox2
7bf37788f011 busybox "sh" About an hour ago Up 36 minutes busybox1
c14ec27277f6 httpd "httpd-foreground" 3 hours ago Up 3 hours
docker network connect my_bridge2 c14ec27277f6
參考:https://blog.51cto.com/wzlinux/2047243,感謝原做者
參考:https://www.techgrow.cn/posts/e6921476.html,感謝原做者
複製代碼
專一於大數據及容器雲核心技術解密,可提供全棧的大數據+雲原平生臺諮詢方案,請持續關注本套博客。若有任何學術交流,可隨時聯繫。更多內容請關注《數據雲技術社區》公衆號。 linux