我正在尝试使用从脚本调用的 Python 客户端https://github.com/elastic/elasticsearch-py(也在容器中运行)来索引容器化的 Elasticsearch 数据库。
通过查看现有的代码,这似乎docker-compose
是一个有用的工具,可以用于我的目的。我的目录结构是
docker-compose.yml
indexer/
- Dockerfile
- indexer.py
- requirements.txt
elasticsearch/
- Dockerfile
我的docker-compose.yml
阅读
version: '3'
services:
elasticsearch:
build: elasticsearch/
ports:
- 9200:9200
networks:
- deploy_network
container_name: elasticsearch
indexer:
build: indexer/
depends_on:
- elasticsearch
networks:
- deploy_network
container_name: indexer
networks:
deploy_network:
driver: bridge
indexer.py
读
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
es = Elasticsearch(hosts=[{"host":'elasticsearch'}]) # what should I put here?
actions = [
{
'_index' : 'test',
'_type' : 'content',
'_id' : str(item['id']),
'_source' : item,
}
for item in [{'id': 1, 'foo': 'bar'}, {'id': 2, 'foo': 'spam'}]
]
# create index
print("Indexing Elasticsearch db... (please hold on)")
bulk(es, actions)
print("...done indexing :-)")
elasticsearch 服务的 Dockerfile 是
FROM docker.elastic.co/elasticsearch/elasticsearch-oss:6.1.3
EXPOSE 9200
EXPOSE 9300
而对于索引器是
FROM python:3.6-slim
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
ENTRYPOINT [ "python" ]
CMD [ "indexer.py" ]
包含requirements.txt
只能elasticsearch
用 pip 下载。
运行在https://pastebin.com/6U8maxGX ( )docker-compose run indexer
处给我错误消息。就我所见,elasticsearch 已启动或运行.ConnectionRefusedError: [Errno 111] Connection refused
curl -XGET 'http://localhost:9200/'
docker ps -a
如何修改我的docker-compose.yml
或indexer.py
解决问题?
为了完整起见,可以在此处找到代码的 PS A(工作)版本(由以下答案通知):https ://github.com/davidefiocco/dockerized-elasticsearch-indexer 。