Docker notes-container communication and data mounting

One-way communication between containers

In the docker environment, communication is often required between containers. For example, a tomcat container deploys a web service. This web service needs to access the mysql service, and mysql is deployed in another container. At this time, communication between containers is required. This example is one-way communication between containers.

So what is one-way communication? That is, the tomcat container unilaterally accesses the MySQL service for data access, and MySQL does not need to know the existence of Tomcat. That is, Tomcat has perception of MySQL, and MySQL has no perception of Tomcat, which is called one-way.

In the Docker environment, each time a container is created, the container is assigned a virtual IP. This IP is only valid inside the Docker environment and cannot be accessed from the outside. Inside the Docker environment, each container can communicate with each other through a virtual IP. Communication.

How to check the virtual IP address of the container

docker insepct 容器id

The figure below is the virtual IP of the tomcat container

image-20200523211822372

# 进入tomcat容器
docker exec -it 容器id /bin/bash
# 进去后
ping 172.17.0.3  # 172.17.0.3 是mysql容器的ip
# 发现能够ping通

But you cannot rely on the virtual IP of the container to communicate between containers. (If you rely on a virtual IP for communication, that is, in the Tomcat container, save the virtual IP address of the MySQL container. When you need to access the MySQL container, you can directly access it based on this virtual IP). But because the virtual IP of the container is dynamically allocated when docker creates the container. If a MySQL container is restarted, its virtual IP is likely to change, so it is necessary to modify the IP address of the MySQL container it wants to access in the tomcat container, which is extremely troublesome.

A better way is to name the container and complete the one-way communication between containers by specifying the name of the container to be accessed. In this way, as long as the container name does not change, docker will automatically help us find the container with the corresponding name, no matter how the virtual IP of the container changes.

When starting the container, you can --namespecify the name of the container through the parameter, if not specified, one will be automatically generated randomly.

docker run -d --name database mysql
# 通过 --link 命令,指定mysql容器的名称,将tomcat和mysql进行关联
docker run -p 7000:8080 -d --link database --name web tomcat

In this way, you can databaseaccess the mysql container by this name

note

#若直接使用如下命令创建一个centos容器,容器运行后会自动退出
docker run centos
#若要centos容器一直保持运行,可以用如下命令
# 用交互模式运行/bin/bash,并让他在后台运行
docker run -d -it centos /bin/bash

Summary :

When starting the container, give each container a name, and then if container A wants to communicate with container B one-way, when starting container A, add parameters --link 容器B的名字, you can

Two-way communication between containers

It can be used twice when starting two containers --linkto complete the two-way communication of the containers. However, in addition to this, you can also use the Bridge bridge to more easily group containers, and two-way communication can be carried out between containers in the same group

=> What is a bridge?

The bridge is a component of the docker environment to communicate with the outside world, and it is a virtual bridge. Its main purpose is to connect the docker environment and the external host environment so that the docker environment can communicate with the outside. You can try to ping Baidu's address inside a certain container, and it can be pinged. In this process, docker's bridge bridge is involved. The bridge can transmit the data packets inside the docker to the physical network card of the host machine, or can receive the data packets from the physical network card of the host machine, and then forward them to the docker.
image-20200524003642767

The docker bridge can also group containers at the network level. Containers belonging to the same group can communicate with each other in two directions. That is, each container can be bound to a specific bridge, and all containers bound to the same bridge can communicate in two directions.

First of all, use docker network lsto view the docker network configuration

image-20200523220930880
According to the above figure, docker will provide a default bridge, which is responsible for the communication between docker and the outside world.

To achieve two-way communication between certain containers, we can create a new bridge named my_bridge through the following command

docker network create -d bridge my_bridge

image-20200523221137373

Then, bind the container that needs to communicate with this bridge

docker network connect my_bridge web
docker network connect my_bridge database

This realization of the web and database and my_bridge two containers bound
but I want to be able to get a command, the name behind the increase in multiple containers, has been tested and it seems not
to see docker network connect --help, according to the syntax, you can only find a command to a The container and the bridge are connected
image-20200523222232729

In this way, the two containers web and database belong to the group my_bridge, and they can communicate with each other.

Summary :

By creating a new bridge type network connection, and then binding the containers that need to be interconnected with each other in turn with this bridge connection. Containers bound to the same bridge connection can be regarded as belonging to the same network group.

Data sharing between containers

If two tomcat containers are deployed with the same set of web files, if the web files are changed, the two containers must be changed in turn, which is very troublesome.

image-20200523223023655

Therefore, a Volume space can be created on the host, and multiple containers can share the volume. The container no longer holds specific web files, and the web files are stored in the volume on the host. As long as the volume on the host is updated, all containers will take effect.

image-20200523224149832

How to set up Volume?

Two ways

  1. By -vmounting the host directory

    docker run --name 容器名 -v 宿主机路径:容器内挂载路径 镜像名
    # 例子如下
    docker run --name web -v /usr/local/webapps:/usr/local/tomcat/webapps tomcat
    

    In this way, wrong path writing will cause the mount to fail. And if multiple containers need to mount the same host directory, each container has to write the directory that needs to be mounted when it starts, which is troublesome and error-prone. So can the host directory to be mounted be declared as a shared variable, and other containers can complete the mounting by referencing this variable? Look at the way 2 ==>

  2. --volumes-fromMount point via shared container

    # 1. 先创建共享容器
    # 这个容器只是为了声明要挂载的宿主机目录,从而能够被其他容器引用
    docker create --name webpage -v /webapps:/tomcat/webapps tomcat
    # 2. 共享容器挂载点
    # 通过 --volumes-from 指定从webpage容器读取挂载点
    docker run --volumes-from webpage --name web -d tomcat
    
    

    The so-called webpage container just defines a mount point, and other containers can reuse this mount point.
    This facilitates unified management of all mounts

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/106309098