如何从容器(API)连接到容器(elasticsearch)? [英] How can I connect from container(API) to container(elasticsearch)?
问题描述
我有2个容器,其中一个是Web API,另一个是elasticsearch.当我在本地计算机(没有容器)中打开API并我可以为docker容器中的Elasticsearch编制索引.我可以为数据建立索引以对Elasticsearch的runnig good和连接URL进行索引,例如 http://localhost:9200 没问题.
I have 2 container.One of them is web API and other is elasticsearch. When I up API in local machine(without container) and up elasticsearch in docker container.I can index data to elasticsearch its runnig good and connection url to elasticsearch such http://localhost:9200 there is no problem.
但是当我想使用docker容器和用docker容器的elasticsearch来建立API时,存在一些问题,我无法使用相同的连接URL将数据索引到elasticsearch,例如
But When I want to up API with docker container and elasticsearch with docker container there is some problem I cant index data to elasticsearch with same connection url such http://localhost:9200
所以我认为问题出在连接网址,但是我不确定.我的docker如下所示.
so I think problem is about connection url, But I m not sure .My docker compose like below.
version: '3'
services:
web:
build: .
ports:
- "8080:80"
network :
- host
container_name : DataCollectorAPI
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0
ports:
- "9200:9200"
- "9300:9300"
environment:
- discovery.type=single-node
推荐答案
您应该像这样链接容器
web:
....
links:
- elasticsearch
depends_on:
- elasticsearch
然后从您的Web容器中使用 elasticsearch
作为主机名.
Then from your web container use elasticsearch
as a hostname.
有关详细信息,请参见 docker-compose.yml 文档.
See the docker-compose.yml documentation for details.
您也可以直接使用其他容器名称作为主机名
Also you can just directly use the other container name as hostname
不需要链接即可使服务进行通信-默认情况下,任何服务都可以使用该服务的名称访问任何其他服务.
Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
这篇关于如何从容器(API)连接到容器(elasticsearch)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!