When unsing docker compose you can have a problem with the order of dependent linked containers
The solution is to start a script which tries to access a service and waits until it gets ready before loading your program
When unsing docker compose you can have a problem with the order of dependent linked containers
The solution is to start a script which tries to access a service and waits until it gets ready before loading your program
| # This code needs to access elastic search! | |
| from datetime import datetime | |
| from elasticsearch import Elasticsearch | |
| es = Elasticsearch('elastic') | |
| es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()}) | |
| print es.get(index="my-index", doc_type="test-type", id=42) |
| elastic: | |
| image: elasticsearch | |
| command: elasticsearch -Des.node.name="YourNodeName" | |
| ports: | |
| - "9200:9200" | |
| app: | |
| image: ubuntu:14.04 | |
| command: sh wait_to_start.sh | |
| working_dir: /src | |
| links: | |
| - elastic:elastic | |
| volumes: | |
| - .:/src | |
| environment: | |
| - WAIT_COMMAND=[ $(curl --write-out %{http_code} --silent --output /dev/null http://elastic:9200/_cat/health?h=st) = 200 ] | |
| - WAIT_START_CMD=python access_elastic_search.py | |
| - WAIT_SLEEP=2 | |
| - WAIT_LOOPS=10 |
| #!/bin/bash | |
| echo $WAIT_COMMAND | |
| echo $WAIT_START_CMD | |
| is_ready() { | |
| eval "$WAIT_COMMAND" | |
| } | |
| # wait until is ready | |
| i=0 | |
| while ! is_ready; do | |
| i=`expr $i + 1` | |
| if [ $i -ge $WAIT_LOOPS ]; then | |
| echo "$(date) - still not ready, giving up" | |
| exit 1 | |
| fi | |
| echo "$(date) - waiting to be ready" | |
| sleep $WAIT_SLEEP | |
| done | |
| #start the script | |
| exec $WAIT_START_CMD |
If you come across an error "Invalid interpolation format for "environment" option in service", you need to escape the $
environment:
- WAIT_COMMAND=[ $$(curl --write-out %{http_code} --silent --output /dev/null http://elastic:9200/_cat/health?h=st) = 200 ]
Inspired by the above I have created this oneliner:
ESHOST="elastic"
timeout 300 bash -c "until curl --silent --output /dev/null http://$ESHOST:9200/_cat/health?h=st; do printf '.'; sleep 5; done; printf '\n'"
Just thought I'd mention that you might be able to make this shorter by using
timeoutanduntil, something like: