Docker learning (VIII) one way communication between containers

Time:2020-1-29

Preface

The content introduced in the previous article is container image and container operation, which does not involve communication before multiple containers. If multiple containers need to call each other, how to communicate?

Docker maintains the meta information of each container and can communicate through IP. However, after each container is restarted in the docker environment, the virtual IP of the container will change. In this case, what should we do? Let’s take these two questions and look at today’s content

scene

If we deploy a Tomcat application, the application needs to connect to MySQL database. In this scenario, we need to let Tomcat access MySQL database, which requires communication between two containers

Methods of communication

  • [x] When the container is started, each container is assigned a name, and the container name is used for communication to identify the other container
  • [x] Two way communication between containers based on Bridge

Environmental preparation

  • Tomcat container

    • App name mywebapp
  • Database container (just for testing, we use a CentOS container to simulate MySQL)

    • Application name database

Create directory/usr/local/docker/communication

Target: the Tomcat container can access the database container

One way communication between containers

  • EstablishmywebappTomcat container
    docker run -d --name mywebapp tomcat
  • Establishdatabase
    docker run -d --name database -it centos7 /bin/bash
  • View container operation
    docker ps

    Docker learning (VIII) one way communication between containers

  • View container metadatadocker inspect bf7b05ce9a06

Docker learning (VIII) one way communication between containers

stayNetworkSettingsProperties inIPAddressYou can see the virtual IP address of the container172.17.0.3

  • Enter mywebapp container

docker exec -it 7e631f5cf4b3 /bin/bash
staymywebappExecution in containerping 172.17.0.3Can be accessed normallydatabaseContainer, indicating IP access to natural interworking

Docker learning (VIII) one way communication between containers

howeverping databaseIt doesn’t work because we didn’t configure the network

Docker learning (VIII) one way communication between containers

  • Exit container, removemywebapp

docker rm 7e631f5cf4b3 -f

Docker learning (VIII) one way communication between containers

  • Add a parameter when restarting the container mywebapp-linkStarting container

docker run -d --name mywebapp --link database tomcat

Docker learning (VIII) one way communication between containers

  • Enter the inside of the container

docker ps
docker exec -it 0c435a5112c4 /bin/bash

Docker learning (VIII) one way communication between containers

>It is accessible to Ping database. In this way, one-way communication between containers is realized through link. When connecting to database in mywebapp, the container name of database can be used. As long as the container name is not changed, you can access it

Using bridge to realize two-way communication

Through link, one-way or two-way traffic can be realized, but configuration will be troublesome. When the application cluster is too large, the workload is too large. In fact, we can use the bridge to realize the communication between containers

Docker learning (VIII) one way communication between containers

The network bridge is a virtual network bridge, which is used to group containers from the network layer, bind the specified containers to the same bridge, and these containers can communicate with each other

Experiment begins

  • Let’s remove the two containers, recreate them, and check 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

    Docker learning (VIII) one way communication between containers

  • View the details of docker’s prudent network services
    docker network ls

    Docker learning (VIII) one way communication between containers

    By default, each bottom layer of docker will provide a default bridge, which is responsible for the communication between the container and the external

  • If you want to realize interconnection and interworking among multiple containers, you need to build a new bridge

docker network create -d bridge my-bridge
docker network ls

Docker learning (VIII) one way communication between containers

In this way, all containers bound to my bridge can communicate with each other

  • Binding containers and bridges

docker network connect my-bridge mywebapp
docker network connect my-bridge database

  • Let’s verify
  • [x] docker ps

    Docker learning (VIII) one way communication between containers

  • [x] Enter mywebapp to Ping
    docker exec -it 16fbe8f31ce5 /bin/bash
    ping database

    Docker learning (VIII) one way communication between containers

  • [x] Enter database and execute ping command
    docker exec -it 15e732e718c2 /bin/bash
    ping mywebapp

    ! [enter description of picture here] [15]

    So far, we have realized the interconnection of two containers

summary

As the container interworking, the core point is to create a new bridge, and then bind the container to the bridge

Why can bridge realize interconnection

Every time a bridge is created, a virtual network card will be created on the host computer to take on the role of a gateway and realize the intercommunication of two containers
Docker learning (VIII) one way communication between containers

  • The virtual network card communicates with the physical network card of the host computer, and transmits the data packet to the physical network card
  • On the contrary, the packets received by the physical network card will also be converted into the packets of the virtual network card, which will be forwarded to the virtual network card and then to the container

Written in the end

Related articles will start public numbers, you can pay attention to the public number.albkAfter you pay attention, you can get a lot of learning videos

If you want to love technology, communicate with one another, get the latest information and learning resources, you can pay attention to the public number ALBK, personal blog!

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]