在链接的Docker容器中发出http请求时出错

我正在用Angular.js 2,Elasticsearch和Docker构build一个Web应用程序。

我试图通过我的Angular应用程序使用链接的URL“ http:// search:9200 / _cat / health ”来访问Elasticsearch,但我得到一个GET http://search:9200/_cat/health net::ERR_NAME_NOT_RESOLVED错误。

奇怪的是,当我从web应用程序的容器中curl -XGET "http://search:9200/_cat/health" ,一切都很好,我得到了正确的回应。

这是我各自docker-compose.yml文件

 version: '2' services: search: image: elasticsearch:latest command: elasticsearch -Des.network.host=0.0.0.0 ports: - "9200:9200" - "9300:9300" web: build: . command: npm start volumes: - .:/visfx ports: - "3000:3000" links: - search 

这里也是对elasticsearch服务的调用

 @Injectable() export class ElasticsearchService { private _esUrl = 'http://search:9200'; constructor(private http: Http) { } getHealthString() { let healthUrl = '/_cat/health'; return this.http.get(this._esUrl + healthUrl) .map(res => <string>res.text()) .do(data => console.log(data)) .catch(this.handleError); } ... } 

如果这个JavaScript运行在客户端,你将不得不使用一个真正的DNS名称路由到docker主机,因为docker的名字只有使用docker的内部parsing器解决。