测试与docker守护程序的连接 [英] Testing connection to docker daemon

查看:172
本文介绍了测试与docker守护程序的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是,有时候,执行 eval$(docker-machine env默认)不会导致docker守护程序立即连接,当下一行到达时( docker-compise up )我得到code>无法连接到Docker守护程序。 Docker守护程序是否在此主机上运行?



如果我使用 sleep 有几个问题解决了几秒。



有没有办法通过一些系统工具测试连接到守护进程(检查进程是否存在,如果已经建立了网络连接,端口听,等)?我想从外部测试docker守护程序,不使用 docker cli

解决方案

Docker Remote API 具有 PING终结点。您可以使用端点来检查是否可以成功连接到Docker守护程序。 docker-machine env 设置环境变量 DOCKER_HOST ,所以你可以使用 DOCKER_HOST 作为主机ping。使用 nc ,您可以按以下方式ping主机:

  $ eval $(docker-machine env default)
$ echo -eGET / _ping HTTP / 1.1\r\\\
| nc $ DOCKER_HOST
HTTP / 1.1 200 OK
服务器:Docker / 1.10.2(linux)
日期:星期三,03三月2016 07:05:58 GMT
内容长度:2
Content-Type:text / plain; charset = utf-8

OK

您将需要检查退货值。如果返回确定,则连接成功。一个简单的检查可能如下所示(这可能需要更多的细化):

 #!/ bin / bash 
如果[$(echo -eGET / _ping HTTP / 1.1\r\\\
| nc $ DOCKER_HOST | tail -n 1)=='OK'];然后
echo你连接
fi


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

解决方案

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

这篇关于测试与docker守护程序的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆