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
mywebapp
tomcat 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
View metadata container
docker inspect bf7b05ce9a06
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
inmywebapp
the implementation of the containerping 172.17.0.3
normal access todatabase
the container, indicating that access to natural IP interworking
But
ping database
that does not make sense, because we do not have to configure the network aspects
- Exit container, remove
mywebapp
docker rm 7e631f5cf4b3 -f
- Add a parameter to restart the container mywebapp -Link launch container
docker run -d --name mywebapp --link database tomcat
- Into the interior of the container
docker ps
docker exec -it 0c435a5112c4 /bin/bash
> 执行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
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
- View docker care network service details
docker network ls
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
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
[X] to enter the ping command mywebapp
docker exec -it 16fbe8f31ce5 /bin/bash
ping database
[X] into the database, the ping command
docker exec -it 15e732e718c2 /bin/bash
ping mywebapp
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
- 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 !