Best way to verify if docker containers are responding via HTTP

note: This question was asked also on stack overflow - http://stackoverflow.com/questions/34467171/circleci-best-way-to-verify-if-docker-containers-are-responding-via-http

here is an example of part of my circle.yml how I am currently verifying if my mongodb docker container is responding to an http request, so that I can verify if the response is ok and that the server is up.

test:
  override:
    # RUN DOCKER CONTAINERS
    # MongoDB -------------
    - docker run --name MongoDB -p 27018:27018 -d mongo:3.0 mongod --port 27018 --replSet "rs"; sleep 10
    - curl --retry 10 --retry-delay 5 -v http://localhost:27018

with this curl it’s working fine, sometimes.
And sometimes it gives me this error:

* About to connect() to localhost port 27018 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0

How can I improve this, to make it more reliable so my tests won’t sometimes pass and sometimes fail, if I rebuild the same build?

Each line of circle.yml runs in a new shell, so I wonder if it’s a timing issue and it’s not waiting out the whole sleep.

Does putting those two lines in a shell script seem to help any?

In this case the curl command will not run until the docker run command together with sleep finish.

I wonder if in some cases the exit code of docker run is actually non-zero but having ; sleep 10 in the same command masks the real exit code. Could you please split sleep 10 into a separate command? This way a failure in docker run will be obvious, and you will be able to tell if the tests failed because of docker run not succeeding.

@alexey , thank you for reply!
I tried as you adviced and found out that the problem is not in docker run
it will be probably timing issue (but I think retrying 10 times with curl with 5 second retry delay even after sleep command is enough time to work always ok.)

Here is a screenshot how it failed on circle, and how I connected via SSH and run the the same command.

Please, do you have any idea how to improve this?

Sorry about that—looks like those has been intermittent connectivity issues. Would be great to collect some networking stats for debugging, could you please add something like this to your circle.yml instead of the current curl command?

- curl ... ; if [ $? != 0 ] ; then sudo apt-get install traceroute ; HOSTNAME=example.org; ping -c 10 $HOSTNAME > $CIRCLE_ARTIFACTS/network-report.log ; traceroute $HOSTNAME >> $CIRCLE_ARTIFACTS/network-report.log ; fi

This will collect some data with ping and traceroute and store it in the Artifacts tab in case the curl command fails. Thanks!

@alexey thank you very much, after I have tried that, I had no problems anymore now it is 100% success. I was even able to reduce the sleep command from 10 sec and 30 sec to 2 sec and it won’t fail anymore. I understand that traceroute is there to diagnose the network stats, but it just started to work…