关于docker的问题 – 链接参数

正如我们所知道的,在一个使用docker0守护进程的主机中,容器连接到docker0桥,因此默认情况下容器可以互相访问。

那么有什么用 – --link选项? direct access by ip方式direct access by ip有什么不同?

它究竟做了什么?

从Docker文档 :

当您设置链接时,您会在源容器和收件人容器之间创buildpipe道。 接收者可以访问关于源的select数据

当两个容器连接在一起时,Docker将在目标容器中设置一些环境variables,以启用与源容器相关的信息的编程发现。

还有一些:

除了环境variables之外,Docker还将源容器的主机条目添加到/etc/hosts文件。 这里是一个Web容器的条目:

因此,基本上--link创build了一组环境variables,并在/etc/hosts文件中添加了一些条目以便于通信。 但是,容器仍然通过IP直接访问。

当你使用--link选项创build一个容器时,Docker通过两种方式将链接的容器公开为新的容器:

  • 它使用链接容器的IP和创build链接时给定的别名在/etc/hosts创build一个条目。
  • 它将一些信息公开为链接容器的环境variables。 正如Docker文档所示:

然后,Docker将为源容器公开的每个端口定义一组环境variables。 接下来的模式是:

 <name>_PORT_<port>_<protocol> will contain a URL reference to the port. Where <name> is the alias name specified in the --link parameter (eg 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> (eg 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 (eg WEBDB_PORT_8080_TCP_ADDR=172.17.0.82). <name>_PORT_<port>_<protocol>_PORT will contain just the port number from the URL (eg WEBDB_PORT_8080_TCP_PORT=8080). <name>_PORT_<port>_<protocol>_PROTO will contain just the protocol from the URL (eg WEBDB_PORT_8080_TCP_PROTO=tcp). 

如果您通过IP访问,则不会有区别,但是使用链接可以让容器忽略将由Docker Daemon分配的IP。 查看Docker文档以获取更多信息。

如果使用--icc=false选项启动--icc=false ,默认情况下容器不能相互通信。 您必须使用 – --link来连接两个容器。