16-docker Network Second lecture-Container default Network Bridge (Docker series)

Source: Internet
Author: User
Tags centos docker run

This article is from the "Knowledge Forest"

Three networks (Docker network LS) are default when the Docker service is successfully installed:

C:\users\zsl-pc>docker Network LS
network ID          NAME                DRIVER              SCOPE
992344cd89fe        Bridge Bridge local
06445f4f5774        host                host                local
2d964ece79b9        none                null Local                

Bridge: The default network of containers that can be configured to communicate with host hosts to interconnect with the Internet.

Host and none: Belong to No network (you can use the Docker network inspect host view, where there are no subnets and gateways), the container is added to both networks to add the container to a particular container network stack and is not able to communicate with the outside network.

whenever you start a container with Docker run, the container has a default network, bridge.

You can view the containers in each network through the inspect command (see Bridge Network):

C:\users\zsl-pc>docker Network Inspect bridge [{' Name ': ' Bridge ', ' Id ': ' 992344cd89fe90aa240651e 7497131e5dc40539d3f1ad540f973feab6b345b3c "Scope": "Local", "Driver": "Bridge", "ENABLEIPV6": FAL
                SE, "IPAM": {"Driver": "Default", "Options": null, "Config": [
            {"Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1"} ]}, "Internal": false, "containers": {}, "options": {"com.docker.network. Bridge.default_bridge ": True", "COM.DOCKER.NETWORK.BRIDGE.ENABLE_ICC": "true", "Com.docker.networ
            K.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"}, "Label S ': {}}]

Seen in containers is empty, indicating that no container has been added to the network at this time.

Start container :

Docker run-d-it--name C1 centos/bin/bash-c "while true; do echo hello; Sleep 1; Done

Description: launches a CentOS container named C1, which outputs hello every second, and can view output information through Docker logs C1.

Then look at Bridge Network (Docker Network inspect bridge):

C:\users\zsl-pc>docker Network Inspect Bridge .......
Containers ": {" 7883225f9192cb4eb68283fd47bc9435ab70dd76803ede1d926f30db3bb1101f ":
    {"
        Name ":" C1 ",
        " EndpointId ":" e784f093b0c810f5c5112235e2e8502f6193077fa5da44a9a527aa9086894382 ","
        MacAddress ":" 02:42:ac : 11:00:02 ",
        " ipv4address ":" 172.17.0.2/16 ",
        " ipv6address ":" "
    }
}
...

Note: you can see that there is already a container C1 in the bridge network and the IP address is 172.17.0.2.

This example shows that the bridge is used by default when the Docker run startup container is not specified in the network. Viewing the network in a container

C:\users\zsl-pc>docker exec-it c1/bin/bash
[root@7883225f9192/]# more/etc/hosts       localhost
:: 1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      7883225f9192
[root@7883225f9192/ ]#

You can see that the host name 7883225f9192 corresponds to the IP address mentioned above 172.17.0.2. You can also view the network information inside the container through ifconfig, if prompted: Bash:ifconfig:command not found can install Network Tools first: Yum install-y net-tools, after the installation is complete and then enter: Ifconfig

[root@7883225f9192/]# ifconfig eth0:flags=4163<up,broadcast,running,multicast> MTU 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:2 Prefixlen Peid 0x20<link> ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 2057 bytes 7829768 (7. 4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1873 bytes 105536 (103.0 KiB) TX er Rors 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

Eth0 is this container's network card information, you can also see that the IP address is 172.17.0.2. then start a container to communicate with other containers

Docker run-d-it--name C2 centos/bin/bash-c "while true; do echo hello; Sleep 1; Done

Description: also started a container, the name is C2, and C1 do the same thing. Then use Docker network inspect bridge to see:

C:\users\zsl-pc>docker Network Inspect Bridge .......
Containers ": {" 7117f84edc269c5f61052a136791e775e26372c2b977db77081cd78533e5721e ":
    {"
        Name ":" C2 ",
        " EndpointId ":" 78be5b7b0737d23082607fd016664017df77ceb1d469d9a3972033796ac8d761 ","
        MacAddress ":" 02:42:ac : 11:00:03 ",
        " ipv4address ":" 172.17.0.3/16 ",
        " ipv6address ":" "
    },
    " 7883225f9192cb4eb68283fd47bc9435ab70dd76803ede1d926f30db3bb1101f ": {
        " Name ":" C1 ",
        " EndpointId ":" 859ff1ec55e136c5ec431df7baaf42af8f2a3e872a7472e16e392a9fa68d797d ","
        MacAddress ":" 02:42:ac:11:00:02 ",
        "ipv4address": "172.17.0.2/16",
        "ipv6address": ""
    }
}
.....

Can be seen: C1 and C2 is in a local area network, C2 IP address is 172.17.0.3, you can try to ping each other to see if Ping Pass:

C:\users\zsl-pc>docker exec-it C2/bin/bash [root@7117f84edc26/]# ping-w 4 172.17.0.2 ping 172.17.0.2 (172.17.0.2) # 5
6 (bytes) of data. Bytes from 172.17.0.2:icmp_seq=1 ttl=64 time=0.044 ms-bytes from 172.17.0.2:icmp_seq=2 ttl=64 time=0.114 Ms Byt Es from 172.17.0.2:icmp_seq=3 ttl=64 time=0.120 ms-bytes from 172.17.0.2:icmp_seq=4 ttl=64 time=0.121 ms bytes Fro M 172.17.0.2:icmp_seq=5 ttl=64 time=0.115 ms---172.17.0.2 PING statistics---5 packets transmitted, 5 received, 0% PA Cket loss, time 3996ms rtt Min/avg/max/mdev = 0.044/0.102/0.121/0.032 ms [Root@7117f84edc26/]# exit C:\users\zsl-pc>d 
Ocker exec-it C1/bin/bash [root@7883225f9192/]# ping-w 4 172.17.0.3 ping 172.17.0.3 (172.17.0.3) bytes of data. Bytes from 172.17.0.3:icmp_seq=1 ttl=64 time=0.048 ms-bytes from 172.17.0.3:icmp_seq=2 ttl=64 time=0.107 ms TES from 172.17.0.3:icmp_seq=3 ttl=64 time=0.089 ms-bytes from 172.17.0.3:icmp_seq=4 ttl=64 time=0.113 ms bytes fr Om172.17.0.3:icmp_seq=5 ttl=64 time=0.114 ms---172.17.0.3 PING statistics---5 packets transmitted, 5 received, 0% pack
 Et loss, time 3996ms rtt Min/avg/max/mdev = 0.048/0.094/0.114/0.025 ms [root@7883225f9192/]#

in C2 inside is can ping pass C1 IP address, in C1 inside also can ping pass C2 IP address, explain in a network of containers can communicate with each other.

This article is from the "Knowledge Forest"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.