[英]questions about docker --link parameter
众所周知,在一台带有docker0
daemon的主机中,容器连接到docker0
桥,因此默认情况下容器可以相互访问。
那么--link
选项的用途是--link
? direct access by ip
方式direct access by ip
有什么不同吗?
它实际上是做什么的?
从Docker文档中 :
设置链接时,将在源容器和收件人容器之间创建管道。 然后,收件人可以访问有关源的选择数据
当两个容器链接在一起时,Docker将在目标容器中设置一些环境变量,以启用以编程方式发现与源容器相关的信息。
还有更多:
除了环境变量外,Docker还将源容器的主机条目添加到
/etc/hosts
文件中。 这是Web容器的条目:
因此,-- --link
基本上会创建一组环境变量,并将一些条目添加到/etc/hosts
文件中,以简化通信。 但是,仍然可以通过IP直接访问容器。
使用--link
选项创建容器时,Docker通过两种方式将链接的容器公开到新容器中:
/etc/hosts
创建一个条目,其中包含链接容器的IP和创建链接时给出的别名。 Docker然后还将为源容器公开的每个端口定义一组环境变量。 遵循的模式是:
<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).
通过IP访问并没有什么区别,但是使用链接可以设置容器,而忽略将由Docker Daemon分配的ip。 查看Docker文档以获取更多信息。
如果使用--icc=false
选项启动--icc=false
,则默认情况下容器无法相互通信。 您必须使用--link
连接两个容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.