当前位置:   article > 正文

docker 网络配置详解_docker 网卡配置

docker 网卡配置

目录

1、docker网络模式

2、容器和容器之间是如何互通

3、容器之间互通 --link

3、自定义网络

4、不通网段的容器进行网络互通

1、docker网络模式

docker 网络模式采用的是桥接模式,当我们创建了一个容器后docker网络就会帮我们创建一个虚拟网卡,这个虚拟网卡和我们的容器网络是一一对应(成对出现)。默认创建的容器都是docker0网卡。

2、容器和容器之间是如何互通

当我们创建一个容器时没有指定容器网络时,docker会默认走docker0网络,docker会默认在虚拟机上创建一个虚拟网卡这个网卡地址会和我们容器地址对应。此时会出现我们可以通过互ping容器的ip 地址可ping通容器,但是通过容器名互ping是无法ping通,因技术要求需要我们自定义网络ip达到互通;默认创建的容器都是docker0网卡。

##查看Tomcat01 的ip地址
docker  exec -it   tomcat01  ip addr
  1. [root@localhost ~]# docker exec -it tomcat01 ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3.   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.   inet 127.0.0.1/8 scope host lo
  5.       valid_lft forever preferred_lft forever
  6. 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  7.   link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  8.   inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  9.       valid_lft forever preferred_lft forever
​##查看Tomcat02 的ip地址
docker  exec -it   tomcat02  ip addr
  1. [root@localhost ~]# docker exec -it tomcat02 ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3.   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.   inet 127.0.0.1/8 scope host lo
  5.       valid_lft forever preferred_lft forever
  6. 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  7.   link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
  8.   inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
  9.       valid_lft forever preferred_lft forever
##用Tomcat01 的ip ping  Tomcat02 的ip 
docker exec -it tomcat01 ping 172.17.0.2
  1. [root@localhost ~]# docker exec -it tomcat01 ping -c 3 172.17.0.3
  2. PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
  3. 64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.071 ms
  4. 64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.051 ms
  5. 64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.074 ms
  6. --- 172.17.0.3 ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss, time 1999ms
  8. rtt min/avg/max/mdev = 0.051/0.065/0.074/0.012 ms
####用Tomcat01 的ip ping  Tomcat02名称(不通)
docker exec -it tomcat01 ping    Tomcat02 

  1. [root@localhost ~]# docker exec -it tomcat01 ping tomcat02
  2. ping: unknown host tomcat02

 

我们可以使用link来达到容器之间的互通

##使用link来实现Tomca01   可以ping通 Tomcat02 
docker  run  -d   --name   tomcat01  --link tomcat02   tomcat:8.0
​
  1. [root@localhost ~]# docker run -d --name tomcat02 tomcat:8.0
  2. 863a3c34388d560406716f8ff46ffc94dec44fb2e42b129bc5bd505d878fc736
  3. [root@localhost ~]# docker run -d --name tomcat01 --link tomcat02 tomcat:8.0
  4. ea2bebceb939c0c6dac31b475045c4a1dcde9511df9e2948665ac45bd003abd4
##用Tomcat01 ping tomcat02
docker  exec   -it   tomcat01 ping tomcat02
​
  1. [root@localhost ~]# docker exec -it tomcat01 ping -c 3 tomcat02
  2. PING tomcat02 (172.17.0.2) 56(84) bytes of data.
  3. 64 bytes from tomcat02 (172.17.0.2): icmp_seq=1 ttl=64 time=0.093 ms
  4. 64 bytes from tomcat02 (172.17.0.2): icmp_seq=2 ttl=64 time=0.054 ms
  5. 64 bytes from tomcat02 (172.17.0.2): icmp_seq=3 ttl=64 time=0.054 ms
  6. --- tomcat02 ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss, time 2000ms
  8. rtt min/avg/max/mdev = 0.054/0.067/0.093/0.018 ms
##使用Tomcat02来ping  tomcat01  (ping不通)
docker  exec   -it   tomcat02   ping   tomcat01
​
  1. [root@localhost ~]# docker exec -it tomcat02 ping -c 3 tomcat01
  2. ping: unknown host tomcat01

使用link其实是在容器内部 /etc/hosts 中添加了对应的映射关系。如果删除了就不能互通。

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID   IMAGE       COMMAND             CREATED         STATUS         PORTS     NAMES
  3. ea2bebceb939   tomcat:8.0   "catalina.sh run"   5 minutes ago   Up 5 minutes   8080/tcp   tomcat01
  4. 863a3c34388d   tomcat:8.0   "catalina.sh run"   6 minutes ago   Up 6 minutes   8080/tcp   tomcat02
  5. [root@localhost ~]# docker exec -it tomcat01 /bin/bash
  6. root@ea2bebceb939:/usr/local/tomcat# cd /etc
  7. root@ea2bebceb939:/etc# cat hosts
  8. 127.0.0.1 localhost
  9. ::1 localhost ip6-localhost ip6-loopback
  10. fe00::0 ip6-localnet
  11. ff00::0 ip6-mcastprefix
  12. ff02::1 ip6-allnodes
  13. ff02::2 ip6-allrouters
  14. 172.17.0.2 tomcat02 863a3c34388d
  15. 172.17.0.3 ea2bebceb939

3、自定义网络

  1. ##查看帮助文档
  2. [root@localhost ~]# docker network --help
  3. Usage: docker network COMMAND
  4. Manage networks
  5. Commands:
  6. connect     Connect a container to a network
  7. create     Create a network
  8. disconnect Disconnect a container from a network
  9. inspect     Display detailed information on one or more networks
  10. ls         List networks
  11. prune       Remove all unused networks
  12. rm         Remove one or more networks
  13. Run 'docker network COMMAND --help' for more information on a command.
  14. [root@localhost ~]# docker network create --help
  15. Usage: docker network create [OPTIONS] NETWORK
  16. Create a network
  17. Options:
  18.     --attachable           Enable manual container attachment
  19.     --aux-address map     Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
  20.     --config-from string   The network from which to copy the configuration
  21.     --config-only         Create a configuration only network
  22. -d, --driver string       Driver to manage the Network (default "bridge")
  23.     --gateway strings     IPv4 or IPv6 Gateway for the master subnet
  24.     --ingress             Create swarm routing-mesh network
  25.     --internal             Restrict external access to the network
  26.     --ip-range strings     Allocate container ip from a sub-range
  27.     --ipam-driver string   IP Address Management Driver (default "default")
  28.     --ipam-opt map         Set IPAM driver specific options (default map[])
  29.     --ipv6                 Enable IPv6 networking
  30.     --label list           Set metadata on a network
  31. -o, --opt map             Set driver specific options (default map[])
  32.     --scope string         Control the network's scope
  33.     --subnet strings       Subnet in CIDR format that represents a network segment
##自定义网络NTtomcat
  1. [root@localhost ~]# docker network create --driver bridge --subnet 192.168.123.1/16 --gateway 192.168.123.1 NTtomcat
  2. 388c65d9e6dc3a685d9dfc07e6cd77b7df753b9790ececd2a4917c486c999fd5
  3. [root@localhost ~]# docker network ls
  4. NETWORK ID     NAME       DRIVER   SCOPE
  5. 388c65d9e6dc   NTtomcat   bridge   local
  6. a468667c9afc   bridge     bridge   local
  7. 9ed8bb46f222   host       host     local
  8. cf4382a12125   none       null     local
  9. ##--driver bridge   桥接模式   如果不指定默认为bridge
  10. ##--subnet 192.168.0.1/16   子网范围内的。0~16,也就是 255*255,去掉0个255,我们有65534可以分配的ip
  11. ##gateway 192.168.0.1 指定网关
  12. ##mynetwork   网络名称(路由名称)
##创建自定义容器tomcat01-01 和tomcat02-02
  1. [root@localhost ~]# docker run -d --name tomcat01-01 --ip 192.168.123.10 --network NTtomcat --hostname tomcat01-01 tomcat:8.0
  2. 710087d898e573015590421594d468e6f7b86cee81badcb76d3370f4decd113d
  3. [root@localhost ~]# docker run -d --name tomcat02-02 --ip 192.168.123.11 --network NTtomcat --hostname tomcat02-02 tomcat:8.0
  4. e7804543a17de40136c883c1ae767d185fd033c0503751449f4b04a7bcc2b479
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID   IMAGE       COMMAND             CREATED             STATUS             PORTS     NAMES
  7. e7804543a17d   tomcat:8.0   "catalina.sh run"   7 seconds ago       Up 7 seconds       8080/tcp   tomcat02-02
  8. 710087d898e5   tomcat:8.0   "catalina.sh run"   About a minute ago   Up About a minute   8080/tcp   tomcat01-01
  9. ea2bebceb939   tomcat:8.0   "catalina.sh run"   25 minutes ago       Up 25 minutes       8080/tcp   tomcat01
  10. 863a3c34388d   tomcat:8.0   "catalina.sh run"   26 minutes ago       Up 26 minutes       8080/tcp   tomcat02
  11. [root@localhost ~]# docker network ls
  12. NETWORK ID     NAME       DRIVER   SCOPE
  13. 388c65d9e6dc   NTtomcat   bridge   local
  14. a468667c9afc   bridge     bridge   local
  15. 9ed8bb46f222   host       host     local
  16. cf4382a12125   none       null     local
##查看NTnetwork 路由下的容器,下面已经多出来Tomcat01-01和Tomcat02-02容器的网络地址

  1. [root@localhost ~]# docker network inspect NTtomcat
  2. [
  3.   {
  4.       "Name": "NTtomcat",
  5.       "Id": "388c65d9e6dc3a685d9dfc07e6cd77b7df753b9790ececd2a4917c486c999fd5",
  6.       "Created": "2023-07-20T09:11:11.409974483+08:00",
  7.       "Scope": "local",
  8.       "Driver": "bridge",
  9.       "EnableIPv6": false,
  10.       "IPAM": {
  11.           "Driver": "default",
  12.           "Options": {},
  13.           "Config": [
  14.               {
  15.                   "Subnet": "192.168.123.1/16",
  16.                   "Gateway": "192.168.123.1"
  17.               }
  18.           ]
  19.       },
  20.       "Internal": false,
  21.       "Attachable": false,
  22.       "Ingress": false,
  23.       "ConfigFrom": {
  24.           "Network": ""
  25.       },
  26.       "ConfigOnly": false,
  27.       "Containers": {
  28.           "710087d898e573015590421594d468e6f7b86cee81badcb76d3370f4decd113d": {
  29.               "Name": "tomcat01-01",
  30.               "EndpointID": "f068d52745d677dd2b67c4e091f8e7bb4364688a6047cb47bd69d2e95f7f5267",
  31.               "MacAddress": "02:42:c0:a8:7b:0a",
  32.               "IPv4Address": "192.168.123.10/16",
  33.               "IPv6Address": ""
  34.           },
  35.           "e7804543a17de40136c883c1ae767d185fd033c0503751449f4b04a7bcc2b479": {
  36.               "Name": "tomcat02-02",
  37.               "EndpointID": "1261a1c9ecf877597564f3c76ce1b73da82533361f8bf0887c46c3b377fe7647",
  38.               "MacAddress": "02:42:c0:a8:7b:0b",
  39.               "IPv4Address": "192.168.123.11/16",
  40.               "IPv6Address": ""
  41.           }
  42.       },
  43.       "Options": {},
  44.       "Labels": {}
  45.   }
  46. ]
##查看Tomcat01-01 ip地址
  1. [root@localhost ~]# docker exec -it tomcat01-01 ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3.   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.   inet 127.0.0.1/8 scope host lo
  5.       valid_lft forever preferred_lft forever
  6. 24: eth0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  7.   link/ether 02:42:c0:a8:7b:0a brd ff:ff:ff:ff:ff:ff
  8.   inet 192.168.123.10/16 brd 192.168.255.255 scope global eth0
  9.       valid_lft forever preferred_lft forever
##查看Tomcat01-01 ip地址
  1. [root@localhost ~]# docker exec -it tomcat02-02 ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3.   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.   inet 127.0.0.1/8 scope host lo
  5.       valid_lft forever preferred_lft forever
  6. 26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  7.   link/ether 02:42:c0:a8:7b:0b brd ff:ff:ff:ff:ff:ff
  8.   inet 192.168.123.11/16 brd 192.168.255.255 scope global eth0
  9.       valid_lft forever preferred_lft forever
##使用Tomcat01-01 ping   Tomcat02-02
  1. [root@localhost ~]# docker exec -it tomcat01-01 ping -c 2 tomcat02-02
  2. PING tomcat02-02 (192.168.123.11) 56(84) bytes of data.
  3. 64 bytes from tomcat02-02.NTtomcat (192.168.123.11): icmp_seq=1 ttl=64 time=0.124 ms
  4. 64 bytes from tomcat02-02.NTtomcat (192.168.123.11): icmp_seq=2 ttl=64 time=0.054 ms
  5. --- tomcat02-02 ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  7. rtt min/avg/max/mdev = 0.054/0.089/0.124/0.035 ms
##使用Tomcat02-02  ping  Tomcat01-01
  1. [root@localhost ~]# docker exec -it tomcat02-02 ping -c 2 tomcat01-01
  2. PING tomcat01-01 (192.168.123.10) 56(84) bytes of data.
  3. 64 bytes from tomcat01-01.NTtomcat (192.168.123.10): icmp_seq=1 ttl=64 time=0.058 ms
  4. 64 bytes from tomcat01-01.NTtomcat (192.168.123.10): icmp_seq=2 ttl=64 time=0.052 ms
  5. --- tomcat01-01 ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 999ms
  7. rtt min/avg/max/mdev = 0.052/0.055/0.058/0.003 ms

4、不通网段的容器进行网络互通

tomcat01 网络ip 为172.17.0.2

 ##用Tomcat01 ping   tomcat01-01

  1. [root@localhost ~]# docker exec -it tomcat01 ping tomcat01-01
  2. ping: unknown host tomcat01-01

##使用docker  network connect 将容器Tomcat01 加入到NTtomcat

  1. [root@localhost ~]# docker  network connect  NTtomcat taomcat01
  2. [root@localhost ~]# docker  exec -it taomcat01  ping -c 2 tomcat01-01
  3. PING tomcat01-01 (192.168.123.10) 56(84) bytes of data.
  4. 64 bytes from tomcat01-01.NTtomcat (192.168.123.10): icmp_seq=1 ttl=64 time=0.080 ms
  5. 64 bytes from tomcat01-01.NTtomcat (192.168.123.10): icmp_seq=2 ttl=64 time=0.055 ms
  6. ^C
  7. --- tomcat01-01 ping statistics ---
  8. 4 packets transmitted, 4 received, 0% packet loss, time 2999ms
  9. rtt min/avg/max/mdev = 0.055/0.061/0.080/0.013 ms

 ##查看NTtomcat 中添加的容器

  1. [root@localhost ~]# docker network inspect NTtomcat
  2. [
  3. {
  4. "Name": "NTtomcat",
  5. "Id": "388c65d9e6dc3a685d9dfc07e6cd77b7df753b9790ececd2a4917c486c999fd5",
  6. "Created": "2023-07-20T09:11:11.409974483+08:00",
  7. "Scope": "local",
  8. "Driver": "bridge",
  9. "EnableIPv6": false,
  10. "IPAM": {
  11. "Driver": "default",
  12. "Options": {},
  13. "Config": [
  14. {
  15. "Subnet": "192.168.123.1/16",
  16. "Gateway": "192.168.123.1"
  17. }
  18. ]
  19. },
  20. "Internal": false,
  21. "Attachable": false,
  22. "Ingress": false,
  23. "ConfigFrom": {
  24. "Network": ""
  25. },
  26. "ConfigOnly": false,
  27. "Containers": {
  28. "42b2bd9190d5b7de7e2e60b5eab41d6878f91abc517d25380af22ccc4ba05528": {
  29. "Name": "taomcat01",
  30. "EndpointID": "3e728f5b90751d45f0e00e69268d2bc9966b15c9578230fd5ab1c179551f6d23",
  31. "MacAddress": "02:42:c0:a8:00:01",
  32. "IPv4Address": "192.168.0.1/16",
  33. "IPv6Address": ""
  34. },
  35. "710087d898e573015590421594d468e6f7b86cee81badcb76d3370f4decd113d": {
  36. "Name": "tomcat01-01",
  37. "EndpointID": "f068d52745d677dd2b67c4e091f8e7bb4364688a6047cb47bd69d2e95f7f5267",
  38. "MacAddress": "02:42:c0:a8:7b:0a",
  39. "IPv4Address": "192.168.123.10/16",
  40. "IPv6Address": ""
  41. },
  42. "e7804543a17de40136c883c1ae767d185fd033c0503751449f4b04a7bcc2b479": {
  43. "Name": "tomcat02-02",
  44. "EndpointID": "1261a1c9ecf877597564f3c76ce1b73da82533361f8bf0887c46c3b377fe7647",
  45. "MacAddress": "02:42:c0:a8:7b:0b",
  46. "IPv4Address": "192.168.123.11/16",
  47. "IPv6Address": ""
  48. }
  49. },
  50. "Options": {},
  51. "Labels": {}
  52. }
  53. ]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/936960
推荐阅读
相关标签
  

闽ICP备14008679号