简体   繁体   English

如何将主机名添加到同一 docker 网络上的容器?

[英]How can I add hostnames to a container on the same docker network?

Suppose I have a docker compose file with two containers.假设我有一个带有两个容器的 docker compose 文件。 Both reference each other in their /etc/hosts file.两者都在其 /etc/hosts 文件中相互引用。 Container A has a reference for container B and vice versa.容器 A 具有容器 B 的引用,反之亦然。 And all of this happens automatically.而这一切都是自动发生的。 Now I want to add one or more hostnames to B in A's hosts file.现在我想在 A 的主机文件中向 B 添加一个或多个主机名。 How can I go about doing this?我该怎么做呢? Is there a special way I can achieve this in Docker Compose?在 Docker Compose 中有没有一种特殊的方法可以实现这一点?

Example:例子:

172.0.10.166 service-b my-custom-hostname 172.0.10.166 服务-b 我的自定义主机名

Yes.是的。 In your compose file, you can specify network aliases .在您的撰写文件中,您可以指定网络别名

services:
  db:
    networks:
      default:
        aliases:
          - database
          - postgres

In this example, the db service could be reached by other containers on the default network using db , database , or postgres .在此示例中,默认网络上的其他容器可以使用dbdatabasepostgres访问db服务。

You can also add aliases to running containers using the docker network connect command with the --alias= option.您还可以使用带有--alias=选项的--alias= docker network connect命令为正在运行的容器添加别名

Docker compose has an extra_hosts feature that allows additional entries to be added to the container's host file. Docker compose 有一个extra_hosts功能,允许将其他条目添加到容器的主机文件中。

Example例子

docker-compose.yml docker-compose.yml

web1:
  image: tomcat:8.0
  ports:
    - 8081:8080
  extra_hosts:
    - "somehost:162.242.195.82"
    - "otherhost:50.31.209.229"
web2:
  image: tomcat:8.0
  ports:
    - 8082:8080
web3:
  image: tomcat:8.0
  ports:
    - 8083:8080

Demonstrate host file entries演示主机文件条目

Run docker compose with the new docker 1.9 networking feature :使用新的docker 1.9 网络功能运行 docker compose :

$ docker-compose --x-networking up -d
Starting tmp_web1_1
Starting tmp_web2_1
Starting tmp_web3_1

and look at the hosts file in the first container.并查看第一个容器中的主机文件。 Shows the other containers, plus the additional custom entries:显示其他容器,以及额外的自定义条目:

$ docker exec tmp_web1_1 cat /etc/hosts 
..
172.18.0.4  web1
172.18.0.2  tmp_web2_1
172.18.0.3  tmp_web3_1
50.31.209.229   otherhost
162.242.195.82  somehost

If I understand your question correctly, you can pass a host name referenced in your host's /etc/hosts file via --add-host flag :如果我正确理解您的问题,您可以通过 --add-host 标志传递在您的主机的 /etc/hosts 文件中引用的主机名:

$ docker run ... --add-host="droid" 

Your host's /etc/hosts would need the following entry: xx.xx.xx.xx droid您的主机的 /etc/hosts 需要以下条目:xx.xx.xx.xx droid

Of course, xx.xx.xx.xx will need to be reachable from inside the container you just started using the 'docker run' command.当然,xx.xx.xx.xx 需要可以从您刚开始使用“docker run”命令的容器内部访问。 You can have one or more --add-host="xyz".你可以有一个或多个 --add-host="xyz"。

More details about --add-host here:有关 --add-host 的更多详细信息,请访问:

http://docs.docker.com/v1.8/reference/commandline/run/ http://docs.docker.com/v1.8/reference/commandline/run/

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

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