I have a script I'm using to help with docker-compose orchestration called wait_to_start.sh that I obtained from here:
#!/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
However, I'm having trouble getting it working. I'd like to use netcat to test the service is running.
couchbase:
container_name: couchbase
image: couchbase/server:community-3.0.1
volumes:
- /opt/couchbase/var:/opt/couchbase/var
ports:
- "8091:8091"
myapp:
container_name: myapp
image: myapp
command: wait_to_start.sh
volumes_from:
- couchbase
links:
- couchbase:couchbase
environment:
- WAIT_COMMAND=[ `nc -z -w3 localhost 8091` -eq 0 ]
- WAIT_START_CMD=mycmd
- WAIT_SLEEP=2
- WAIT_LOOPS=10
However it doesn't work.
When I run netcat directly on the command line it works. It returns 0 on success. The above doesn't work.
I was getting:
bash: [: =: unary operator expected
So I played around and found that echoing WAIT_COMMAND produces
[ -eq 0 ]
What am I doing wrong? I'm a bit of a bash noob. It must be something simple.
WAIT_COMMAND="nc -z -w3 couchbase 11211".eval(which is a horrible thing) you can put an arbitrarily complicated command in there. And just sticking an!at the start of thenccommand should work.