2025年4月2日 星期三 乙巳(蛇)年 正月初三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 云技术 > Docker

Docker容器学习与分享08

时间:12-20来源:作者:点击数:41

Docker容器网络

Docker除了默认创建的三种网络外,还可以自定义网络。

首先创建一个bridge类型的网络,使用docker network create命令。

  • [root@promote ~]# docker network create --driver bridge new_net
  • 8f0d50950bb7b86f01f5a62d6a322ede46fa893c515acf10d0e335fa28ccf234
  • [root@promote ~]# docker network ls
  • NETWORK ID NAME DRIVER SCOPE
  • b6a32ec430e9 bridge bridge local
  • 9ab80c94885b host host local
  • 8f0d50950bb7 new_net bridge local
  • c42335728d98 none null local

查看一下当前的网络变化。

  • [root@promote ~]# brctl show
  • bridge name bridge id STP enabled interfaces
  • br-8f0d50950bb7 8000.024233fc2bd6 no
  • docker0 8000.0242f338d5bb no

发现新增了一个网桥,这个网桥就是我们刚刚创建好的网络,可以发现网桥的名字就是刚刚创建的网络的短ID。

既然是我们自己创建的网络那么就可以自定义这个网络,接着重新创建一个网络并自定义这个网络的网段和网关。

  • [root@promote ~]# docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 new2_net
  • 44b6f73a2cd3fd0d5d17124ddb87c5f3dfca1a8ca2c90af1570b1dd4eaa71d67
  • [root@promote ~]# docker network ls
  • NETWORK ID NAME DRIVER SCOPE
  • b6a32ec430e9 bridge bridge local
  • 9ab80c94885b host host local
  • 44b6f73a2cd3 new2_net bridge local
  • 8f0d50950bb7 new_net bridge local
  • c42335728d98 none null local

使用docker inspect可以看到new2_net网段的详细信息,可以注意到网段和网关都是我们自定义的。

  • [root@promote ~]# docker network inspect new2_net
  • [
  • {
  • "Name": "new2_net",
  • "Id": "44b6f73a2cd3fd0d5d17124ddb87c5f3dfca1a8ca2c90af1570b1dd4eaa71d67",
  • "Created": "2018-07-10T01:56:21.418800602-04:00",
  • "Scope": "local",
  • "Driver": "bridge",
  • "EnableIPv6": false,
  • "IPAM": {
  • "Driver": "default",
  • "Options": {},
  • "Config": [
  • {
  • "Subnet": "192.168.1.0/24",
  • "Gateway": "192.168.1.1"
  • }
  • ]
  • },
  • "Internal": false,
  • "Attachable": false,
  • "Containers": {},
  • "Options": {},
  • "Labels": {}
  • }
  • ]

再查看一下网桥

  • [root@promote ~]# brctl show
  • bridge name bridge id STP enabled interfaces
  • br-44b6f73a2cd3 8000.02421dea9d8a no
  • br-8f0d50950bb7 8000.024233fc2bd6 no
  • docker0 8000.0242f338d5bb no
  • [root@promote ~]# ifconfig br-44b6f73a2cd3
  • br-44b6f73a2cd3: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
  • inet 192.168.1.1 netmask 255.255.255.0 broadcast 0.0.0.0
  • ether 02:42:1d:ea:9d:8a txqueuelen 0 (Ethernet)
  • RX packets 0 bytes 0 (0.0 B)
  • RX errors 0 dropped 0 overruns 0 frame 0
  • TX packets 0 bytes 0 (0.0 B)
  • TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

可以发现网桥上的IP就是刚刚创建的网络的网关。

接下来就是使用自己创建的网络,使用--network选项即可。

  • [root@promote ~]# docker run -it --name busybox --network new2_net docker.io/busybox
  • / # ifconfig
  • eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:01:02
  • inet addr:192.168.1.2 Bcast:0.0.0.0 Mask:255.255.255.0
  • inet6 addr: fe80::42:c0ff:fea8:102/64 Scope:Link
  • UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  • RX packets:14 errors:0 dropped:0 overruns:0 frame:0
  • TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
  • collisions:0 txqueuelen:0
  • RX bytes:1156 (1.1 KiB) TX bytes:578 (578.0 B)
  • lo Link encap:Local Loopback
  • inet addr:127.0.0.1 Mask:255.0.0.0
  • inet6 addr: ::1/128 Scope:Host
  • UP LOOPBACK RUNNING MTU:65536 Metric:1
  • RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  • TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  • collisions:0 txqueuelen:1000
  • RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

可以看到这个容器的IP地址就是我刚刚创建的网络分配的。当然,这个IP地址还可以自己设置静态IP,当然不能设置为new2_net网络网段之外的IP,否则会报错。

使用--ip选项就可以了。

  • [root@promote ~]# docker run -it --name busybox --network new2_net --ip 192.168.1.5 docker.io/busybox
  • / # ifconfig
  • eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:01:05
  • inet addr:192.168.1.5 Bcast:0.0.0.0 Mask:255.255.255.0
  • inet6 addr: fe80::42:c0ff:fea8:105/64 Scope:Link
  • UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  • RX packets:7 errors:0 dropped:0 overruns:0 frame:0
  • TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
  • collisions:0 txqueuelen:0
  • RX bytes:578 (578.0 B) TX bytes:578 (578.0 B)
  • lo Link encap:Local Loopback
  • inet addr:127.0.0.1 Mask:255.0.0.0
  • inet6 addr: ::1/128 Scope:Host
  • UP LOOPBACK RUNNING MTU:65536 Metric:1
  • RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  • TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  • collisions:0 txqueuelen:1000
  • RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

可以看到Ip为自己设置的静态IP。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门