Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've had that problem too, and found that you can implement a "wait" command.

If you're using Docker Compose, add this to your environment: environment: - WAIT_COMMAND=[ $(curl --write-out %{http_code} --silent --output /dev/null http://elastic:9200/_cat/health?h=st) = 200 ] - WAIT_START_CMD=python /code/lytten/main.py - WAIT_SLEEP=2 - WAIT_LOOPS=10

Then, create a 'wait' bash script in your app's source code that looks like this: !/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 10
    done

    #start the script
    exec $WAIT_START_CMD
Then, finally, for your nginx container, add: command: sh /code/wait_to_start.sh

to its specification



Just in case a shorter version appeals to you, I think you could rewrite it like this:

  timeout 60 bash -c 'until is_ready; do sleep 10; done'
You'll need to `export -f is_ready` if it's a shell function. That's assuming you can use a timeout instead of number-of-retries.

http://man7.org/linux/man-pages/man1/timeout.1.html


I was trying to find the source of where I got that bash script from, but couldn't, and then forgot about it within the editing window.

I'll try to find it and provide that feedback. Thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: