Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing connection to docker daemon

Tags:

linux

docker

I am writing a script that will boot docker-compose automatically.

However, sometimes, doing eval "$(docker-machine env default)" doesn't cause the docker daemon to be connected immediatly and when the next line comes (docker-compose up) I get Cannot connect to the Docker daemon. Is the docker daemon running on this host?

If I use sleep for a few seconds the issue resolves.

Is there a way to test the connect to daemon via some system tool (checking if a process exists, if a network connect was made, port listened to, etc)? I want to test the docker daemon externally and not use docker cli

like image 406
Nick Ginanto Avatar asked Oct 19 '22 14:10

Nick Ginanto


1 Answers

The Docker Remote API has a PING endpoint. You can use the endpoint to check whether you can successfully connect to the Docker daemon. docker-machine env sets the environment variable DOCKER_HOST, so you can use DOCKER_HOST as host to ping. Using nc, you can ping the host as follows:

$ eval "$(docker-machine env default)"
$ echo -e "GET /_ping HTTP/1.1\r\n" | nc $DOCKER_HOST
HTTP/1.1 200 OK
Server: Docker/1.10.2 (linux)
Date: Thu, 03 Mar 2016 07:05:58 GMT
Content-Length: 2
Content-Type: text/plain; charset=utf-8

OK

You will need to check the return value. If it returns 'OK', the connection was successful. A simple check could look as follows (this probably needs more refinement):

#!/bin/bash
if [ "$(echo -e "GET /_ping HTTP/1.1\r\n" | nc $DOCKER_HOST | tail -n 1)" == 'OK' ] ; then
  echo "You are connected"
fi
like image 168
morxa Avatar answered Nov 04 '22 20:11

morxa