我正在使用Docker version 1.12.3
和docker-compose version 1.8.1
。我有一些服务,包括例如elasticsearch,rabbitmq和webapp
我的问题是服务无法通过其主机访问其他服务,因为docker-compose不会将所有服务热点放在/etc/hosts
文件中。我不知道他们的IP,因为它是在docker-compose up
阶段定义的。
我使用https://docs.docker.com/compose/networking/而不是networks
描述的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
答案 0 :(得分:0)
如果容器期望主机名在容器启动时立即可用,这可能是它失败的原因。
在其他容器启动之前,主机名不会存在。您可以使用入口点脚本等待所有主机名都可用,然后exec elasticsearch ...