简体   繁体   English

有关docker --link参数的问题

[英]questions about docker --link parameter

As we know, in one host with docker daemon, containers connect to the docker0 bridge, and so containers can access each other by default. 众所周知,在一台带有docker0 daemon的主机中,容器连接到docker0桥,因此默认情况下容器可以相互访问。

Then what's the use of --link option? 那么--link选项的用途是--link Is it any different with the direct access by ip way? direct access by ip方式direct access by ip有什么不同吗?

What does it actually do? 它实际上是做什么的?

From the Docker docs : Docker文档中

When you set up a link, you create a conduit between a source container and a recipient container. 设置链接时,将在源容器和收件人容器之间创建管道。 The recipient can then access select data about the source 然后,收件人可以访问有关源的选择数据

When two containers are linked, Docker will set some environment variables in the target container to enable programmatic discovery of information related to the source container. 当两个容器链接在一起时,Docker将在目标容器中设置一些环境变量,以启用以编程方式发现与源容器相关的信息。

And some more: 还有更多:

In addition to the environment variables, Docker adds a host entry for the source container to the /etc/hosts file. 除了环境变量外,Docker还将源容器的主机条目添加到/etc/hosts文件中。 Here's an entry for the web container: 这是Web容器的条目:

So, basically --link creates a set of environment variables and adds some entries to the /etc/hosts file in order to ease communication. 因此,-- --link基本上会创建一组环境变量,并将一些条目添加到/etc/hosts文件中,以简化通信。 But, the containers are still directly accessed via IP. 但是,仍然可以通过IP直接访问容器。

When you create a container using --link option, Docker exposes the linked container into the new one in two ways: 使用--link选项创建容器时,Docker通过两种方式将链接的容器公开到新容器中:

  • It creates a entry in /etc/hosts with the IP of the linked container and the alias given when creating the link. 它在/etc/hosts创建一个条目,其中包含链接容器的IP和创建链接时给出的别名。
  • It exposes some information as environmental variables about the linked container. 它将某些信息作为有关链接容器的环境变量公开。 As Docker documentation shows: Docker文档所示:

Docker will then also define a set of environment variables for each port that is exposed by the source container. Docker然后还将为源容器公开的每个端口定义一组环境变量。 The pattern followed is: 遵循的模式是:

<name>_PORT_<port>_<protocol> will contain a URL reference to the port. Where <name> is the alias name specified in the --link parameter (e.g. webdb), <port> is the port number being exposed, and <protocol> is either TCP or UDP. The format of the URL will be: <protocol>://<container_ip_address>:<port> (e.g. tcp://172.17.0.82:8080). This URL will then be split into the following 3 environment variables for convenience:
<name>_PORT_<port>_<protocol>_ADDR will contain just the IP address from the URL (e.g. WEBDB_PORT_8080_TCP_ADDR=172.17.0.82).
<name>_PORT_<port>_<protocol>_PORT will contain just the port number from the URL (e.g. WEBDB_PORT_8080_TCP_PORT=8080).
<name>_PORT_<port>_<protocol>_PROTO will contain just the protocol from the URL (e.g. WEBDB_PORT_8080_TCP_PROTO=tcp).

It is not differences if you access via IP, but using links let setting the container ignoring the ip that will be assigned by Docker Daemon. 通过IP访问并没有什么区别,但是使用链接可以设置容器,而忽略将由Docker Daemon分配的ip。 Check Docker documentation for further information. 查看Docker文档以获取更多信息。

If you start the docker using --icc=false option, the container can't communicate to each other by default. 如果使用--icc=false选项启动--icc=false ,则默认情况下容器无法相互通信。 You must use --link to connect two containers. 您必须使用--link连接两个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM