Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

use docker healthcheck #27

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build/elasticsearch/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ COPY elasticsearch.yml config/
COPY log4j2.properties config/
COPY bin/es-docker bin/es-docker

# Healthcheck. Copied from
# https://github.com/docker-library/healthcheck/blob/master/elasticsearch/Dockerfile
# Set to yellow for the case of running 1 node, like note in
# tests/test_basic_index_crud.py # test_cluster_health_after_crud
# Reference: https://docs.docker.com/engine/reference/builder/#/healthcheck
#
# TODO How to detect that 2 nodes are running and wait for "green"?
COPY docker-healthcheck /usr/local/bin/
HEALTHCHECK CMD ["/bin/bash","docker-healthcheck","yellow"]

USER root
RUN chown elasticsearch:elasticsearch config/elasticsearch.yml config/log4j2.properties bin/es-docker && \
chmod 0750 bin/es-docker
Expand Down
31 changes: 31 additions & 0 deletions build/elasticsearch/docker-healthcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# Copied from https://github.com/docker-library/healthcheck/tree/master/elasticsearch/docker-healthcheck
# Usage: docker-healthcheck # by default, waits for "green"
# docker-healthcheck yellow # to wait for "yellow" instead

target='green'
if [ ! -z "$1" ]; then
target="$1"
fi

set -eo pipefail

# Use -i instead of --ip-address in case hostname was just a symlink to busybox
# The busybox version of hostname supports -i and not --ip-address
# Both busybox/hostname and the GNU hostname support -i
# References:
# https://busybox.net/downloads/BusyBox.html#hostname
# https://linux.die.net/man/1/hostname
# http://manpages.ubuntu.com/manpages/precise/man1/hostname.1.html
host="$(hostname -i || echo '127.0.0.1')"

# TODO with xpack, the below curl will fail with "unauthorized" error if xpack is enabled
if health="$(curl -fsSL "http://$host:9200/_cat/health?h=status")"; then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if you get a 401/403 response code, this means container is healthy
you could call curl -I -X "http://$host:9200" and parse response code

health="$(echo "$health" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g')" # trim whitespace (otherwise we'll have "green ")
if [ "$health" = "$target" ]; then
exit 0
fi
echo >&2 "unexpected health status: $health"
fi

exit 1
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
version: '2'
version: '2.1'
services:
# Can't use service elasticsearch=<num> for now due to named volumes
elasticsearch1:
Expand Down Expand Up @@ -66,6 +66,13 @@ services:
- elasticsearch1:elasticsearch
volumes:
- $PWD/tests:/home/testuser/tests:ro
# Despite the python tester having a built-in healthcheck at tests/es_acceptance.py#wait_for_cluster_health
# Using the dockerfile healthcheck extends this to other services without having to re-code a wait for health status in each
# Note that this requires docker-compose.yml version 2.1, docker-compose>=1.11, docker>=1.13
depends_on:
elasticsearch1:
condition: service_healthy

volumes:
esdata1:
driver: local
Expand Down