我正在运行“elasticsearch”容器。我可以 curl 容器并获得结果,但是当我尝试从“Web”容器中与容器进行通信时,它将拒绝连接。
docker-compose up
curl localhost:9200 // works.
curl docker-compose run web curl localhost:9200 // connection refused.
docker-compose.yml
version: '2'
services:
web:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/src
ports:
- "5000:5000"
depends_on:
- elasticsearch
elasticsearch:
image: elasticsearch:5.1.2
ports:
- "9200:9200"
Dockerfile
FROM python:3.5
ADD . /src
WORKDIR /src
RUN pip install -r requirements.txt
CMD python project/wsgi.py
最佳答案
您不能使用localhost:9200
容器中的web
连接到elasticsearch
容器。您可以定义一个链接,也可以只使用服务名称(默认情况下已映射):
curl elasticsearch:9200
Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
另请参阅Docker Compose Links
关于elasticsearch - Docker容器之间的通讯出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41808909/