현재 Elasticsearch를 사용하여 데이터베이스에 쓰기를 포함하는 애플리케이션을 컨테이너화하려고 시도하고 있지만 연결 문제가 발생합니다. docker-compose.yml
파일은 현재 다음과 같은 :
version: "3"
services:
es:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
container_name: es
environment:
- node.name=es
- cluster.name=es-docker-cluster
- cluster.initial_master_nodes=es
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
ports:
- 9200:9200
volumes:
- esdata:/usr/share/elasticsearch/data
networks:
- dash-network
dash:
build: .
depends_on:
- es
ports:
- 5000:5000
volumes:
- .:/usr/src/dash
networks:
- dash-network
volumes:
esdata:
driver: local
networks:
dash-network:
driver: bridge
내 Python 코드에는 다음이 포함됩니다.
import elasticsearch
es = elasticsearch.Elasticsearch([{'host': 'es', 'port': 9200}])
es.index(index="spam", body={'eggs': 11})
docker-compose up
그러나 실행하면 해당 코드 세그먼트의 마지막 줄에 다음 오류가 발생합니다.
esmqtt_1 | elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f2f60b53dd0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f2f60b53dd0>: Failed to establish a new connection: [Errno 111] Connection refused)
실험으로이 코드를 실행하기 전에 오랫동안 스크립트를 중지하고 컨테이너에 쉘링 한 다음이 세 명령을 각각 순차적으로 실행했습니다. 작동했습니다.
elliot@elliot-VirtualBox:~/path/to/dash$ sudo docker exec -it dash_dash_1 sh
# python
Python 3.7.6 (default, Jan 3 2020, 23:24:26)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import elasticsearch
>>> es = elasticsearch.Elasticsearch([{'host': 'es', 'port': 9200}])
>>> es.index(index="spam", body={'eggs': 11})
{'_index': 'spam', '_type': '_doc', '_id': '3Ey5zm8B7hzXos4SVZsF', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
Dockerfile에 의해 실행될 때 내 코드가 실패하는 이유를 모르겠지만 쉘 인하면 작동합니다. 이전 인스턴스에서는 연결에 실패하고 후자의 인스턴스에서는 성공하는 이유는 무엇입니까? 모든 팁이나 조언을 환영합니다!
다른 컨테이너를 동시에 실행 dash
하려고했기 때문에 Elasticsearch 설정이 완료되기 전에 컨테이너가 연결을 시도했습니다. 진행하기 전에 준비가되었는지 확인하기 위해 다음 루프를 사용합니다.
while True:
try:
es.search(index="")
break
except (
elasticsearch.exceptions.ConnectionError,
elasticsearch.exceptions.TransportError
):
time.sleep(1)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다