Wait for Elasticsearch to be ready in Docker/ComposeΒΆ

The Django service dependencies set with depends_on in Docker compose do not wait for the elasticsearch service to be ready, they only wait until the service is started.

To guarantee that elasticsearch is ready add the following snippet to the Django entrypoint script:

if [ -z "${ELASTICSEARCH_HOST:-}" ]; then
    export ELASTICSEARCH_HOST="elasticsearch:9200"
fi

elasticsearch_ready() {
python << END
import sys

from elasticsearch_dsl import connections
from elasticsearch.exceptions import ConnectionError

try:
    c = connections.create_connection(hosts=["${ELASTICSEARCH_HOST}"], timeout=10)
    c.info()
except (ConnectionError, ConnectionRefusedError) as e:
    sys.exit(-1)

sys.exit(0)
END
}
until elasticsearch_ready; do
  >&2 echo 'Waiting for Elasticsearch to become available...'
  sleep 1
done
>&2 echo 'Elasticsearch is available'