2016-11-16 80 views
0

我正在使用Docker version 1.12.3docker-compose version 1.8.1。我有一些服务,其中包含例如elasticsearch,rabbitmq和webappdocker-compose - 网络 -/etc/hosts未更新

我的问题是服务无法通过其主机访问其他服务因为docker-compose不会将所有服务热点放在/etc/hosts文件中。我不知道他们的IP,因为它是在docker-compose up阶段定义的。

我使用networks功能,因为它描述在https://docs.docker.com/compose/networking/而不是links,因为我做循环引用和links不支持它。但使用networks不会将所有服务主机放到每个服务节点/etc/hosts文件中。我设置了container_name,我设置了主机名但没有发生任何事情。我缺少的东西;

这是我的docker-compose.yml;

version: '2' 

services: 
    elasticsearch1: 
    image: elasticsearch:5.0 
    container_name: "elasticsearch1" 
    hostname: "elasticsearch1" 
    command: "elasticsearch -E cluster.name=GameOfThrones -E node.name='Ned Stark' -E discovery.zen.ping.unicast.hosts=elasticsearch1,elasticsearch2,elasticsearch3" 
    volumes: 
     - "/opt/elasticsearch/data" 
    ports: 
     - "9200:9200" 
     - "9300:9300" 
    networks: 
     - webapp 

    elasticsearch2: 
    image: elasticsearch:5.0 
    container_name: "elasticsearch2" 
    hostname: "elasticsearch2" 
    command: "elasticsearch -E cluster.name=GameOfThrones -E node.name='Daenerys Targaryen' -E discovery.zen.ping.unicast.hosts=elasticsearch1,elasticsearch2,elasticsearch3" 
    volumes: 
     - "/opt/elasticsearch/data" 
    networks: 
     - webapp 

    elasticsearch3: 
    image: elasticsearch:5.0 
    container_name: "elasticsearch3" 
    hostname: "elasticsearch3" 
    command: "elasticsearch -E cluster.name=GameOfThrones -E node.name='John Snow' -E discovery.zen.ping.unicast.hosts=elasticsearch1,elasticsearch2,elasticsearch3" 
    volumes: 
     - "/opt/elasticsearch/data" 
    networks: 
     - webapp 


    rabbit1: 
    image: harbur/rabbitmq-cluster 
    container_name: "rabbit1" 
    hostname: "rabbit1" 
    environment: 
     - ERLANG_COOKIE=abcdefg 
    networks: 
     - webapp 

    rabbit2: 
    image: harbur/rabbitmq-cluster 
    container_name: "rabbit2" 
    hostname: "rabbit2" 
    environment: 
     - ERLANG_COOKIE=abcdefg 
     - CLUSTER_WITH=rabbit1 
     - ENABLE_RAM=true 
    networks: 
     - webapp 

    rabbit3: 
    image: harbur/rabbitmq-cluster 
    container_name: "rabbit3" 
    hostname: "rabbit3" 
    environment: 
     - ERLANG_COOKIE=abcdefg 
     - CLUSTER_WITH=rabbit1 
    networks: 
     - webapp 




    my_webapp: 
    image: my_webapp:0.2.0 
    container_name: "my_webapp" 
    hostname: "my_webapp" 
    command: "supervisord -c /etc/supervisor/supervisord.conf -n" 
    environment: 
     - DYNACONF_SETTINGS=settings.prod 
    ports: 
     - "8000:8000" 
    tty: true 
    networks: 
     - webapp 


networks: 
    webapp: 
    driver: bridge 

这就是我的理解,他们不能相互交流;

在elasticserach集群初始化时出现此错误;

Caused by: java.net.UnknownHostException: elasticsearch3

而且这是我的泊坞窗,构成

docker-compose up

+1

看起来对我来说很好。 Docker不再使用'/ etc/hosts',一切都通过内部DNS完成。你是说你的服务不能相互沟通?你打算怎么打电话给他们?应该只能使用服务名称。 – johnharris85

+0

@ johnharris85,我更新了这个问题。我在elasticsearch集群初始化中看到'由java.net.UnknownHostException:elasticsearch3引起,我只在我的本地Mac OS计算机上使用'docker-compose up'。 –

回答

0

如果容器期望当容器开始很可能为什么它失败的主机可用立竿见影。

直到其他容器启动后,主机名才会存在。您可以使用入口点脚本来等待,直到所有主机名都可用,然后exec elasticsearch ...