Search code examples
djangopostgresqldockerelasticsearchdocker-compose

Getting ConnectionError while running elastic search using docker in django project


I want to run elasticsearch using docker. It works fine when I only add code to run elastic search in docker-compose.yml file, but I am getting:

Connection error caused by: ConnectionError(Connection error caused by: NewConnectionError

when I update docker-compose.yml file to start server, postgresql db and elastic search.

This is the complete error log when I try to use search feature in my project:

ConnectionError at / Connection error caused by: ConnectionError(Connection error caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f4935a81fa0>: Failed to establish a new connection: [Errno 111] Connection refused))

Below is the code for dockerfile and docker-compose.yml file.

Dockerfile

FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . /code/

docker-compose.yml

version: '3'

services:
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: myproject
      POSTGRES_USER: myprojectuser
      POSTGRES_PASSWORD: password

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
      - es
    environment:
      - DATABASE_URL=postgres://myprojectuser:password@db:5432/myproject

  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
    environment:
      - discovery.type=single-node
    ports:
      - "9200:9200"
    volumes:
      - es_data:/usr/share/elasticsearch/data

volumes:
  postgres_data:
  es_data:

settings.py

ELASTICSEARCH_DSL = {
    'default': {
        'hosts': 'http://localhost:9200'
    }
}

I tried to run only elastic search container, and it works fine. However, when I update docker-compose.yml file to start django server, pg db, and elastic search, I get the error.


Solution

  • By default, elasticsearch is listening only on localhost, that in your docker setup means it will not be accessible outside of the docker container. So, you need to add

        environment:
          - discovery.type=single-node
          - network.host=0.0.0.0
    

    This should make Elasticsearch available on http://es:9200/, if I read your configuration correctly it should be:

    settings.py

    ELASTICSEARCH_DSL = {
        'default': {
            'hosts': 'http://es:9200'
        }
    }