Docker Multi container connections (take Tomcat+Mysql as an example)
- 2020-06-12 11:10:23
- OfStack
Docker provides direct access to multiple containers, the simplest of which is to specify the mapped port directly using the port mapping-ES2en parameter or to map all ports with -ES3en, which multiple containers access directly through the network port.
However, the network port mapping method is not the only way to connect multiple containers in Docker. A more secure method is to use the connection system of Docker (--link) to connect multiple containers. When the container is connected to 1, the receiver container can see the information of the source container.
Take Tomcat + Mysql as an example to establish connections between containers
To establish a connection directly in the container, use the --link option
--link < name or id > :alias
Here we set up an Tomcat + Mysql service to create a connection between two or more containers under example 1.
To establish a container connection, rely on the container name, using --name to specify the source container name as mysql
docker run --name mysql -d gsoft/mysql:5.6
Next, create the tomcat container and attach it to the mysql container
docker run --name tomcat -d -p 80:8080 --link mysql:mysql gsoft/tomcat:7.0
The --link option specifies that the container to connect to is mysql.
Container interchange information
After establishing the connection between the two containers, access to the resources of the source container (Source) is required in the receiving container (Recipient). When we establish the connection for the container, the source container was created without specifying the port to be exposed using -ES50en / -ES51en. So how do we access the information of the source container?
To enable the receiving container to access the information of the source container, Docker provides two ways:
The environment variable / etc/hosts filesThe environment variable
When Docker connects to the container, it automatically creates 1 environment variable in the receiver container according to the parameters provided by --link, including the environment variable set by the source container's Dockerfile command using ENV and the environment variable specified by the source container's startup (docker run) using -ES67en or the --env, -- env-ES70en parameter.
Mainly contains the following environment variables, here assume alias=mysql.
<alias>_PORT
<alias>_PORT_<port>_<protocol>
<alias>_PORT_<port>_<protocol>_ADDR
<alias>_PORT_<port>_<protocol>_PORT
<alias>_PORT_<port>_<protocol>_PROTO
<alias>_NAME
Such as:
#docker run -i -t --rm --link mysql:mysql ubuntu:14.04 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=9c74aa611463
TERM=xterm
MYSQL_PORT=tcp://172.17.0.3:3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.3:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.3
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_NAME=/desperate_ritchie/mysql
HOME=/root
In the example above, the alias of the container is specified as msyql, so all environment variables start with MYSQL_.
Note that the environment variable information in the receiving container is not automatically updated if the source container is restarted, so to use the IP address of the source container, use the host information configured in /etc/hosts.
/ etc/hosts files
In addition to environment variables, Docker also updates hosts information in the receiving container's /etc/hosts file.
# docker run -i -t --rm --link mysql:mysql ubuntu:14.04 /bin/bash
# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 mysql 115346bdb403
172.17.0.5 09bdf7805133
As you can see above, two additional pieces of information have been added to the hosts file of the receiving container, the native IP and alias, and the IP and alias (mysql) of the source container.
Unlike environment variables, if the source container restarts, the information in the receiving container /etc/hosts is automatically updated.