18-docker Network Fourth Lecture-Network alias (Docker series)

Source: Internet
Author: User
Tags aliases docker run

This article is from the "Knowledge Forest"

In the previous example, the communication between the container and the container is done through the IP address in the network, which is obviously unreasonable, because the IP address may change when the container is started, and it is also more difficult to remember.

The solution to this problem is to use a network alias, the container is allowed to be alias on the network, and the alias can be directly accessed in the network, which is similar to the local area network in the host name of each physical machine. Start container Specify network and network alias

Docker run-d-it--name c4--network test-net--network-alias c4-alias centos/bin/bash-c "while true; do echo hello; Sleep 1; Done "
E51B0CCD566709595C136D3EC41A72652843880184052324E5702BBDC82E0C22

Description: use--network to specify the network, use--network-alias to specify the network alias, that is to say C4 the alias of this container in test-net This network is C4-alias, Other containers in this network can use this alias to access the container. To view a container in your network

C:\users\zsl-pc>docker Network Inspect Test-net ...... "Containers": {"0968be0ab042ec35121774227bbecf5c5fd8a1faf0f7fd3b5ac0069af64e7709": {"Name": "C3", "E Ndpointid ":" 1a2062123d61774cd49d074f5dc8705fd0189c24657c7eb2002de4ad908810f0 "," MacAddress ":" 02:42:ac:12:00:03 " , "ipv4address": "172.18.0.3/16", "ipv6address": ""}, "7117f84edc269c5f61052a136791e775e26372c2b9 77db77081cd78533e5721e ": {" Name ":" C2 "," EndpointId ":" F3c3a919d92c1213da1a13879e75e4e2fe09ed619c65abad07  51f8e76f19c2d9 "," MacAddress ":" 02:42:ac:12:00:02 "," ipv4address ":" 172.18.0.2/16 "," ipv6address ": ""}, "E51b0ccd566709595c136d3ec41a72652843880184052324e5702bbdc82e0c22": {"Name": "C4", "Endpoi
        Ntid ":" Ab60608e5f26d370b71eb3f4e051f133649de35dff1bae0dcbec17684dca7cb5 "," MacAddress ":" 02:42:ac:12:00:04 ", "IPv4Address": "172.18.0.4/16", "ipv6address": ""}} .....

Description: It can be seen that there are already three containers in the test-net. View Container Detail Information

C:\users\zsl-pc>docker inspect C4 .......
Networks ": {" test-net ": {"
        ipamconfig ": null,
        " Links ": null,
        " aliases ": [
            " C4-alias ",
            " e51b0ccd5667 "
        ],
        " Networkid ":" 39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50 ", "
        EndpointId": "Ab60608e5f26d370b71eb3f4e051f133649de35dff1bae0dcbec17684dca7cb5", "
        Gateway": " 172.18.0.1 ","
        IPAddress ":" 172.18.0.4 ",
        " Ipprefixlen ":" Ipv6gateway ":" "
        ,
        " Globalipv6address ":" ",
        " Globalipv6prefixlen ": 0,
        " MacAddress ":" 02:42:ac:12:00:04 "
    }
............

Note: by looking at the details of C4, you can see in networks that the network owned by the container is test-net (this is specified using--network). Inside the test-net you can see a c4-alias in the aliases (this is specified using the--network-alias), indicating that the C4 network alias is already set. Communicating with other containers through a network alias

Enter any container in the Test-net network (select C3 here) to test and C4 communication:

C:\users\zsl-pc>docker exec-it C3/bin/bash [root@0968be0ab042/]# ping-w 4 172.18.0.4 ping 172.18.0.4 (172.18.0.4) # 5
6 (bytes) of data. Bytes from 172.18.0.4:icmp_seq=1 ttl=64 time=0.071 ms-bytes from 172.18.0.4:icmp_seq=2 ttl=64 time=0.074 Ms Byt Es from 172.18.0.4:icmp_seq=3 ttl=64 time=0.122 ms-bytes from 172.18.0.4:icmp_seq=4 ttl=64-time=0.079 ms---172.18. 0.4 Ping Statistics---4 packets transmitted, 4 received, 0% packet, loss, time 3001ms rtt Min/avg/max/mdev = 0.071/0.086/
0.122/0.022 ms [root@0968be0ab042/]# ping-w 4 c4-alias ping c4-alias (172.18.0.4) bytes of data.  Bytes from c4.test-net (172.18.0.4): icmp_seq=1 ttl=64 time=0.040 ms-bytes from c4.test-net (172.18.0.4): icmp_seq=2 Ttl=64 time=0.060 ms bytes from C4.test-net (172.18.0.4): icmp_seq=3 ttl=64 time=0.117 ms-Bytes from c4.test-net (1  72.18.0.4): icmp_seq=4 ttl=64 time=0.084 ms---c4-alias PING statistics---4 packets transmitted, 4 received, 0% packet
Loss, Time 3004msRTT Min/avg/max/mdev = 0.040/0.075/0.117/0.029 ms [root@0968be0ab042/]# 

Note: you can see that in the C3 container you can communicate with the C4 IP address or network alias. Connect to specify the network alias

Docker Network Connect--alias C1-alias test-net C1

Note: use Docker Network Connect to add the container to the specified network, and--alias to specify the alias of the container in the specified network.

Enter C2 for testing:

C:\users\zsl-pc>docker exec-it c2/bin/bash
[root@7117f84edc26/]# ping-w 2 c1-alias
ping C1-alias (172.18.0 .5) bytes of data.
Bytes from c1.test-net (172.18.0.5): icmp_seq=1 ttl=64 time=0.056 ms-bytes from
c1.test-net (172.18.0.5): icmp_s  eq=2 ttl=64 time=0.297 ms

---c1-alias ping statistics---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
RTT Min/avg/max/mdev = 0.056/0.176/0.297/0.121 ms
[Root@7117f84edc26/]# ping-w 2 c4-alias
Ping c4-a Lias (172.18.0.4) bytes of data.
Bytes from c4.test-net (172.18.0.4): icmp_seq=1 ttl=64 time=0.066 ms-bytes from
c4.test-net (172.18.0.4): Icmp_ seq=2 ttl=64 time=0.060 ms

---c4-alias ping statistics---
2 packets transmitted, 2 received, 0% packet loss, t IME 1002ms
RTT min/avg/max/mdev = 0.060/0.063/0.066/0.003 ms

The network alias is valid throughout the network, that is, any container in the same network can access the corresponding container through the network alias.

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.