Docker容器

docker网络

本文主要是介绍docker网络,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

[root@mcw1 ~]$ docker network  ls  #docker安装时,自动创建了三个网络
NETWORK ID     NAME      DRIVER    SCOPE
494faac7d060   bridge    bridge    local
cd41eede7725   host      host      local
f1ca2d33644e   none      null      local

指定none或者host网络运行容器

指定none网络运行容器
docker run -it --network=none busybox
[root@mcw1 ~]$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED      STATUS          PORTS                                       NAMES
5f32a681a40e   registry:2   "/entrypoint.sh /etc…"   4 days ago   Up 16 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   vigorous_golick
[root@mcw1 ~]$ 
[root@mcw1 ~]$ docker run -it --network=none busybox
/ # ifconfig
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 
/ # ping 10.0.0.131
PING 10.0.0.131 (10.0.0.131): 56 data bytes
ping: sendto: Network is unreachable
/ # ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.172 ms

 

桥接网络

[root@mcw1 ~]$ brctl #没有查看命令
-bash: brctl: command not found

解决方法:
[root@mcw1 ~]$ yum install bridge-utils

[root@mcw1 ~]$ brctl  show #查看桥接网络
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024297f6f9f5    no        veth1e4bc85
                            veth43e82ce
[root@mcw1 ~]$ docker run -d httpd  #运行一个容器后,再看桥接网络
2f2c3267c3fea477975160249c4854190693dda7df2fbe8ea556dc96a971a1d3
[root@mcw1 ~]$ brctl show  #发现运行一个容器后,docker0多个个接口
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024297f6f9f5    no        veth1e4bc85
                                        veth43e82ce
                                        vethcde8cf7

 

 

[root@mcw1 ~]$ docker ps  #查看容器
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                       NAMES
2f2c3267c3fe   httpd        "httpd-foreground"       6 minutes ago    Up 6 minutes    80/tcp                                      gifted_franklin
86193a633fa7   centos       "/bin/bash"              11 minutes ago   Up 11 minutes                                               laughing_liskov
5f32a681a40e   registry:2   "/entrypoint.sh /etc…"   4 days ago       Up 39 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   vigorous_golick
[root@mcw1 ~]$ docker exec -it 861 /bin/bash  #进入容器
[root@86193a633fa7 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@86193a633fa7 /]# ip a  #查看容器网卡信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
21: eth0@if22: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@86193a633fa7 /]# exit
[root@mcw1 ~]$ brctl show
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024297f6f9f5    no        veth1e4bc85
                            veth43e82ce
                            vethcde8cf7
[root@mcw1 ~]$ 
[root@mcw1 ~]$ docker network inspect bridge  #查看桥接网络
[
    {
        "Name": "bridge",
        "Id": "494faac7d0608e95196457a5c9fbadd888ad69db2db3ef567965dd86ae9456a6",
        "Created": "2021-12-31T09:13:37.431413692+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "2f2c3267c3fea477975160249c4854190693dda7df2fbe8ea556dc96a971a1d3": {
                "Name": "gifted_franklin",
                "EndpointID": "478447d435355c772111ae2e72c0f18248eb25dc1345a68bb31e3b138471cc0c",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "5f32a681a40e44455a9016cb7bc8a365aaaee83470c75988399d93fe7db93a81": {
                "Name": "vigorous_golick",
                "EndpointID": "de6459bd4d725fe5a2c0808d42e93f68fc563883a6af7dc72b10ba4feed7c1a2",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "86193a633fa74957ef4f09b3abbe6bf8c19f6139824247f4b2ee3aa1b04202be": {
                "Name": "laughing_liskov",
                "EndpointID": "fc15ac2a2296481a56047d08aa1f0f39df28225d0a6229c040d7cd3552fba05a",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]
[root@mcw1 ~]$ ip a  #查看宿主机网卡信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d3:09:d5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.131/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed3:9d5/64 scope link 
       valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d3:09:df brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.131/24 brd 172.16.1.255 scope global ens37
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed3:9df/64 scope link 
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:97:f6:f9:f5 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:97ff:fef6:f9f5/64 scope link 
       valid_lft forever preferred_lft forever
14: veth43e82ce@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether 1e:02:6d:67:dc:b1 brd ff:ff:ff:ff:ff:ff link-netnsid 2
    inet6 fe80::1c02:6dff:fe67:dcb1/64 scope link 
       valid_lft forever preferred_lft forever
22: veth1e4bc85@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether be:ba:17:78:b7:85 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::bcba:17ff:fe78:b785/64 scope link 
       valid_lft forever preferred_lft forever
24: vethcde8cf7@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether e2:ab:b3:bd:ea:44 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::e0ab:b3ff:febd:ea44/64 scope link 
       valid_lft forever preferred_lft forever
[root@mcw1 ~]$ brctl show
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024297f6f9f5    no        veth1e4bc85
                            veth43e82ce
                            vethcde8cf7
[root@mcw1 ~]$ docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                       NAMES
2f2c3267c3fe   httpd        "httpd-foreground"       10 minutes ago   Up 10 minutes   80/tcp                                      gifted_franklin
86193a633fa7   centos       "/bin/bash"              16 minutes ago   Up 16 minutes                                               laughing_liskov
5f32a681a40e   registry:2   "/entrypoint.sh /etc…"   4 days ago       Up 43 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   vigorous_golick
[root@mcw1 ~]$ 

由图可知,创建一个docker容器,默认在桥接docker0网卡上创建一个接口。接口名称和网卡@前面的值一样,。@后面的f 然后接网卡前面的数字,

 

 

 

[root@mcw1 ~]$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:97ff:fef6:f9f5  prefixlen 64  scopeid 0x20<link>
        ether 02:42:97:f6:f9:f5  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 438 (438.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.131  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fed3:9d5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d3:09:d5  txqueuelen 1000  (Ethernet)
        RX packets 14427  bytes 9387349 (8.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7727  bytes 774908 (756.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.131  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fed3:9df  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d3:09:df  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 768 (768.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        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

veth1e4bc85: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::bcba:17ff:fe78:b785  prefixlen 64  scopeid 0x20<link>
        ether be:ba:17:78:b7:85  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth43e82ce: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::1c02:6dff:fe67:dcb1  prefixlen 64  scopeid 0x20<link>
        ether 1e:02:6d:67:dc:b1  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethcde8cf7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::e0ab:b3ff:febd:ea44  prefixlen 64  scopeid 0x20<link>
        ether e2:ab:b3:bd:ea:44  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@mcw1 ~]$ 

 

这篇关于docker网络的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!