Docker learning (eight) one-way communication between the container

Docker learning (eight) one-way communication between the container

Foreword

Introduction In the previous article are mirrored container and container operation, not related to the communication, if you need to call each other between a plurality of containers before multiple containers, how to communicate it?

After docker maintains meta information of each container, you can communicate through IP, but each container restart docker environment, container virtual IP will change under such circumstances how do we deal with? We took a look at these two questions at today's content

Scenes

If we deployed a tomcat application, the application needs to connect mysql database, under this scenario, we need to let tomcat can access the mysql database, which needs to be communication between the two containers

Implementation of Communication

  • [X] at the time of startup of the container, to specify a name for each container, both the communication using the other container to identify the name of the container
  • [X] on the container bridge achieve bidirectional communication between

Preparing the Environment

  • tomcat container
    • Application name mywebapp
  • Database container (just to test, we used a simulation centos container mysql)
    • Application name database

Create a directory/usr/local/docker/communication

Goal: tomcat container can access the database container

One-way communication between the container

  • Create a mywebapptomcat container
    docker run -d --name mywebapp tomcat

  • create database
    docker run -d --name database -it centos7 /bin/bash

  • Check the operation of the vessel
    docker ps

    Enter a description of the picture here

  • View metadata container docker inspect bf7b05ce9a06

Enter a description of the picture here

In NetworkSettings attribute IPAddress can see the IP address of the virtual container172.17.0.3

  • Mywebapp into the container
    docker exec -it 7e631f5cf4b3 /bin/bash
    in mywebapp the implementation of the container ping 172.17.0.3normal access to databasethe container, indicating that access to natural IP interworking

Enter a description of the picture here

But ping databasethat does not make sense, because we do not have to configure the network aspects

Enter a description of the picture here

  • Exit container, removemywebapp
    docker rm 7e631f5cf4b3 -f

Enter a description of the picture here

  • Add a parameter to restart the container mywebapp -Link launch container
    docker run -d --name mywebapp --link database tomcat

Enter a description of the picture here

  • Into the interior of the container
    docker ps
    docker exec -it 0c435a5112c4 /bin/bash

Enter a description of the picture here

> 执行ping database 是可以访问通的,这样就通过link 实现了容器间的单向通信,在mywebapp中连接数据库时,使用database这个容器名即可,只要容器名称不变,就可以访问到

The use of two-way communication bridge

Can be achieved through a one-way link, of course, it can be achieved through two-way, but the configuration would be more trouble, when Application Clusters too much time, too much work, in fact, we can use the bridge to achieve, communicate with each other between the container

Enter a description of the picture here

Bridge is a virtual network bridge, the role of the containers from the packet network level, the specified container is bound to the same bridge, these are bound containers communicate with each other can be achieved

Beginning of the experiment

  • Our first two containers just remove and re-create what the two containers, and look at the status of the two containers

docker rm -f 0c435a5112c4
docker rm -f bf7b05ce9a06
docker run -d --name mywebapp tomcat
docker run -d -it --name database centos /bin/bash
docker ps

Enter a description of the picture here

  • View docker care network service details
    docker network ls

Enter a description of the picture here

Each default docker bottom bridge will provide a default, assume responsibility for communication with the outside of the container

  • To achieve interconnection among the plurality of containers, need to create a bridge
    docker network create -d bridge my-bridge
    docker network ls

Enter a description of the picture here

Bound to the container so that all the my-bridge, may communicate with each other

  • Bound containers and bridges
    docker network connect my-bridge mywebapp
    docker network connect my-bridge database

  • We verify
  • [x] docker ps

    Enter a description of the picture here

  • [X] to enter the ping command mywebapp
    docker exec -it 16fbe8f31ce5 /bin/bash
    ping database

Enter a description of the picture here

  • [X] into the database, the ping command
    docker exec -it 15e732e718c2 /bin/bash
    ping mywebapp

    Enter a description of the picture here

    Here we have achieved the interconnection of the two containers

to sum up

作为容器的互通,最核心的点就是创建一个新的网桥,然后将容器与这个网桥绑定是即可

Why can achieve interoperability bridge

Whenever you create a bridge, it will create a virtual NIC on the host, assume the role of a gateway to achieve interoperability of the two containers
Enter a description of the picture here

  • Physical and virtual network adapter card to communicate with the host, forwards the packet to the packet sent to the physical NIC
  • Conversely physical NIC receives a packet, the packet will be converted into virtual NIC forwarded to virtual network adapter, and then transfer to a virtual network adapter container

Written in the last

Related Articles will be starting public number, you can focus on the public number albk , you can get a lot of attention after learning video

Hope you love technology together to exchange and obtain the latest information and learning resources can focus on public number albk, personal blog !

Guess you like

Origin www.cnblogs.com/albk/p/12219964.html